The goal is to allow the user to select the quantity they want, hit Add to Cart, then have the script separate the Line Item into individual Line Items with the Quantity of 1. I have accomplished this using Rule which works great but we also use Commerce Pricing Attributes which allows you to alter the price of a product before adding to cart. The altered price does not carry through to the split Line Items using this Rule. My script so far does keep that price.
This works for the first time the cart is loaded. The second time the cart is loaded, the original price is not applied to the added line items. Instead they remain in tack but get their default price applied.
Here is the shopping cart when it's first loaded after clicking the Cart link in the popup message. Everything appears OK with price across all items.
Here is what it looks like when I refresh the page, update cart, proceed to next step - pretty much anything that will trigger the commerce_cart_order_refresh().
As you can see the original item retains it's altered price, but the others that were added now default back to their original price.
Here is my code:
function ces_line_items_commerce_cart_line_item_refresh($line_item, $order_wrapper){
// set the global user
global $user;
// load the entity wrapper of the line item
$line_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
// get the quantity of the line item
$quantity = (int)$line_wrapper->quantity->value();
// get the product id to use later
$product_id = $line_wrapper->commerce_product->product_id->value();
// set the altered price or current price of line item
$newPrice = $line_wrapper->commerce_unit_price->amount->value();
// get the original line item
$old_line_item = $line_wrapper->line_item_id->value();
// load the original line item -- need to do this for rewriting later
$original_line_item = commerce_line_item_load($old_line_item);
// set our index
$i = 1;
// only do the following if the original line item has a quantity above 1
if($quantity > 1){
// loop until we are 1 below quantity
while($i < $quantity) {
// it the product can be loaded
if ($product = commerce_product_load($product_id)) {
// create new line item
$new_line_item = commerce_product_line_item_new($product, 1, $order_wrapper->order_id, $original_line_item->data);
// make sure to include any additional price calculations
drupal_alter('commerce_product_calculate_sell_price_line_item', $new_line_item);
rules_invoke_event('commerce_product_calculate_sell_price', $new_line_item);
// reload the line item
$new_line_item = commerce_cart_product_add($user->uid, $new_line_item, FALSE);
// set unit price
$new_line_item->commerce_unit_price['und'][0]['amount'] = $newPrice;
// set base price
$new_line_item->commerce_unit_price['und'][0]['data']['components'][0]['price']['amount'] = $newPrice;
// set base total amount
$new_line_item->commerce_total['und'][0]['data']['components'][0]['price']['amount'] = $newPrice;
// save new line item
commerce_line_item_save($new_line_item);
// add line item to order wrapper
$order_wrapper->commerce_line_items[] = $new_line_item;
}
// increment our index
$i++;
}
}
// alter line item quantity back to 1
$original_line_item->quantity = "1.00";
// save line item
commerce_line_item_save($original_line_item);
}
So what I need is the ability to disable commerce_cart_order_refresh from trying to default everything back.