5

I have a custom form which on submit creates a new order, loads a product and a custom line item type. I then save the information from the form in the line item. The line item price is updated via a rule which calls a custom PHP function. The problem is when I redirect to checkout/order_id the Order Total is not updated.

I've tried altering the order directly using my order_wrapper and i've also tried using commerce_price_component_add. I tried a rule which called "Save entity" again but nothing seems to be working.

Can somebody give me a point in the right direction on how to get the order total to re-calculate.

Also if anyone has any good examples of creating orders using custom line item types from a custom form that may be helpful

1 Answer 1

4

The solution was to use commerce_price_add_component()

    $line_item_wrapper = entity_metadata_wrapper("commerce_line_item", $commerce_line_item);
    $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
        $line_item_wrapper->commerce_unit_price->value(),
        'base_price',
        array(
                'amount' => $booking_price,
                'currency_code' => 'AUD',
                'data' => array(),
        ),
        TRUE
);
}
4
  • Then, inside commerce_cart_product_add(), do you insert the line item or the wrapper?
    – Strae
    Nov 14, 2012 at 14:13
  • @Strae I always use the line item you could also use wrapper->value()
    – benjy
    Nov 17, 2012 at 7:18
  • sorry but i dont get how to make it works. I have a sample code like that: pastebin.com/Fqn3GP5y the line item get inserted into the cart, but with the product original price and not with the manually one '12.34'! Any suggestion?
    – Strae
    Nov 19, 2012 at 11:58
  • @Strae This is only part of the issue. You still need to update the price inside the hook: hook_commerce_cart_line_item_refresh(), or via the proper RULE. Commerce needs to recalculate the price based upon the product before displaying the cart. So, unless you update it here, your price will simply be thrown out.
    – Atomox
    Jun 22, 2016 at 21:33

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.