0

I've got a line item type called 'Donation' which has a Product reference field.

Additionally, I have an instance of a Product type called 'Donation' with the SKU of DONATION_ONEOFF (pid=1), which has a price field set to $0.

What I want to do is decide at form submit time what the value of the line item is before I add it to the cart.

I have a test form, that when submitted, runs the following

function jam_test_donation_form_submit($form, &$form_state) {
    global $user;

    // Load the product with SKU DONATION_ONEOFF
    $product = commerce_product_load(1);

    // Try and set the price of the product before passing it.
    $product->commerce_price['und'][0]['amount'] = 22;

    // Create the line item from the product.
    $line_item = commerce_product_line_item_new($product, 1, 0, array());

    // Add the line item to the cart.
    $line_item_added = commerce_cart_product_add($user->uid, $line_item);
}

I was hoping that the new value (22) would display in the Cart, but it's still at 0 - even though the $line_item and $line_item_added objects show the correct field values of 22 and even a calculated total if I add more.

I assume this could be because the cart is referencing the product node directly rather than using the value it was given at commerce_cart_product_add time - is there something I need to call to update this?

I could create a series of products at fixed amounts but I need the ability to specify an arbitrary amount for donation as well.

1 Answer 1

1

This is covered at length in a variety of other places. Your price is being lost due to the shopping cart refresh system and your avoidance of the core product pricing API.

You need a custom price field or something to store the price on the line item and then a pricing rule that swaps that custom price in for the unit price during the price calculation process.

Your Answer

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

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