I want 'sell' a donation for custom donation amounts, and I know that to do this, I will need to use line items. I wrote the following code which I thought would do the trick, but when I go to /cart, I see that the price has remained the same.

global $user;
  if(user_is_logged_in()) {
    //$order_id = commerce_cart_order_id($user->uid);
    //$order = commerce_order_load($order_id);
    $order = commerce_cart_order_load($user->uid); 
  else {
    $order = commerce_cart_order_load();  //no uid since user is not logged in. attempts to load an anonymous order from the session.
  $lineItem = commerce_line_item_load($order->commerce_line_items['und'][0]['line_item_id']);
  if($form_state['values']['donation-custom-amount'] !== "") {  //if the user entered a custom amount
    $lineItem->commerce_unit_price['und'][0]['amount'] = $form_state['values']['donation-custom-amount'];
    $lineItem->commerce_total_price['und'][0]['amount'] = $form_state['values']['donation-custom-amount'];
  else {
    $lineItem->commerce_unit_price['und'][0]['amount'] = $form_state['values']['donation-amount'];

I checked that $form_state['values'] was giving me the form data, and there are no errors, just no price change.

2 Answers 2


Recently I've updated some other line item fields on commerce_add_to_cart_submit() via this code:

$line_item = commerce_line_item_load($lit_id);
$line_item->field_product_options[LANGUAGE_NONE][0]['value'] = $size;

I hope this will help someone.

  • I have moved off of this project for now, as I found a solution using Rules. However, if I have to do this again, I will definitely take a look at this. Thanks! Dec 2, 2014 at 14:44
  • Thanks for this one, I have been strugging for hours before I find your answer and the correct way to reset entity cache : drupal.org/node/2671936. :). Feb 29, 2016 at 15:37

The requirement which I see from your code is, you want to user's input for amount & update the cart. You can easily achieve this by using - Commerce Donate & Commerce Custom Product.

If it doesn't suit your requirement, please specify for further lookout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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