0

Here is my code


function addToCart($product_id, $quantite){
    global $user;
    $product = commerce_product_load($product_id);
  $line_item = commerce_product_line_item_new($product, $quantite);
  $line_item->data['context']['product_ids'] = array($product_id);
  $line_item->commerce_unit_price->amount  = '900';
  commerce_cart_product_add($user->uid, $line_item);
}

The above code add the product to the cart with the correct quantity but the price isn't changed ... Any idea ?

Update (resolved thanks to scott's answer)


//Panier
function addToCartCallback()
{
    addToCart($_POST['product_id'], $_POST['quantite']);
}

function addToCart($product_id, $quantite){
  global $user;
  $product = commerce_product_load($product_id);
  $line_item = commerce_product_line_item_new($product, $quantite);
  $line_item->data['context']['product_ids'] = array($product_id);
  commerce_cart_product_add($user->uid, $line_item);
  $order = commerce_cart_order_load($user->uid);
  commerce_cart_order_refresh($order);
}


function groupes_views_commerce_cart_line_item_refresh($line_item, $order_wrapper) {
  global $user;
  $idProduit = $line_item->commerce_product['und'][0]['product_id'];
  $prix = getPrixClient(getIdClient($user->uid), $idProduit) * 100;
  $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['amount'] = $prix;
  $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['data']['components']['0']['price']['amount'] = $prix;
}
1

I thought of another way to do it.

Start with code that just adds the product to the cart without modifying.

function addToCart($product_id, $quantite) {
    commerce_cart_product_add_by_id($product_id, $quantite);
}

Then you can use a hook to modify the line item amount

//alter the price in cart & order
function my_module_commerce_cart_line_item_refresh($line_item, $order_wrapper) {

    $price = 100; //1 dollar
    $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['amount']=$price;
    //alter the base_price component
    $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['data']['components']['0']['price']['amount']=$price;

}

If this doesn't work try forcing the cart refresh e.g.

function addToCart($product_id, $quantite) {
    global $user;
    commerce_cart_product_add_by_id($product_id, $quantite);
    $order = commerce_cart_order_load($user->uid);
    commerce_cart_order_refresh($order);
}
| improve this answer | |
  • Thank you Scott I really appreciate your help :) ! But sadly, I'm still not able to update the price. I updated my first post with the code I'm using. I also added the modules that I'm using (maybe it will help). – Germain Jun 18 '15 at 9:39
  • I'm finally able to make it work with your latest code, thank you very much Scott ! – Germain Jun 18 '15 at 12:17
  • I added the final code on the main post for users that may have the same issue ;) – Germain Jun 18 '15 at 12:40
  • That's great... well done – Scott Anderson Jun 18 '15 at 22:48
0

Programmatically adding or modifying the price of a line item is quite tricky.

Here is an example of updating a line item amount

// Create a line item with quantity 1 and this product.
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);

// set up price data
$line_item_price = commerce_price_field_data_auto_creation();
$line_item_price['amount'] = 900.0 * 100; // DC stores in cents
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$line_item_wrapper->commerce_unit_price = $line_item_price;

// fix price data components
commerce_line_item_rebase_unit_price($line_item_wrapper->value());

// Save the line item to get its ID.
commerce_line_item_save($line_item);

Take a look at this article for a full example of adding a line item programmatically.

http://divingintodrupal8.com/#drupal-commerce-adding-line-items-programmatically

Edit:

Here is your code with a couple of changes. Note when we create the order now we set the status to 'cart'.

function addToCartTest($product_id , $quantite){
  global $user;

  // first create a new order
  $order = commerce_order_new ($user-uid, 'cart');
  // Save the order so its ID is assigned.
  commerce_order_save($order);
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  //Now we can add a line item - assuming $price is the amount without sales tax
  // get product by sku
  $product = commerce_product_load($product_id);
  // Create a line item with quantity 1 and this product.
  $line_item = commerce_product_line_item_new($product, $quantite, $order->order_id);
  // set up price data
  $line_item_price = commerce_price_field_data_auto_creation();
  $line_item_price['amount'] = 100 * 100; // DC stores in cents
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $line_item_wrapper->commerce_unit_price = $line_item_price;
  // fix price data components
  commerce_line_item_rebase_unit_price($line_item_wrapper->value());
  // apply sales tax
  //commerce_tax_rate_apply(my_module_tax_rate(), $line_item);
  // Save the line item to get its ID.
  commerce_line_item_save($line_item);
  // Add the line item to the order using the wrapper.
  $order_wrapper->commerce_line_items[] = $line_item;      
  commerce_order_save($order);
}

Also note the code above sets the amount to $100.

| improve this answer | |
  • The code above is for Drupal8, it doesn't works on my website with Drupal 7 ... – Germain Jun 17 '15 at 7:17
  • That code is actually from a Drupal 7 site. Check the first line of the article. "This is a Drupal 7 mini-post..." – Scott Anderson Jun 17 '15 at 7:25
  • That's strange, the code didn't worked on my website. When i clicked on my add to cart button it didn't added anything ... – Germain Jun 17 '15 at 7:28
  • You need to put the code in a custom module and implement a drupal commerce hook. – Scott Anderson Jun 17 '15 at 7:35
  • I'm able to make it works (like my older code) but the price is still not changed, can you tell me if you see an error ? (I pasted my code on main post above). – Germain Jun 17 '15 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.