I've been trying to skin this cat for a long time so there are several related questions. Plain vanilla D7 Commerce shopping cart. HOWEVER, the line item pricing we want to use is grabbed from an external database via XML requests. It can never be determined from the Products table because there are all kinds of discounts that change all the time.

My -main- problem is that the Shopping Cart View in Commerce seems to automatically calculate and update the Unit Price and Extended Price for each line item before the shopping cart page is rendered. I've read the Commerce tutorials and apparently this is referred to as a 'Status'... calling commerce_cart_order_refresh().

I've tried using a pricing rule to get the price for each line item, but that means an http request for each line item which is dead slow.

So I added a function to hook into the Shopping Cart view BUT it seems to fire -after- the page is actually rendered (but before the visitor sees it on screen.) So the upshot is that the visitor has to hit refresh in order to get the correct pricing. IOW: when they first visit the Shopping Cart they get 0s for the pricing. They click again and they see the correct prices.

Two questions:

  1. How do I hook into the Shopping Cart View so that my function fires -before- the form is rendered?

  2. How do I DISABLE the automatic price update for each line item (since my function has already updated the line items and order totals.) I assume this is happening in commerce_cart_order_refresh(). Is there a way to force that calculation to FALSE?


  • It would be useful if you could post some code on the operations you're doing, in terms of discounts, or coupons, commerce_cart_order_refresh() fires when you load a order in cart status, check commerce_cart_commerce_order_load and that's what's invoking the rule event "commerce_product_calculate_sell_price" which basically means all the calculations over the prices. – pcambra Jun 19 '14 at 8:10
  • Your first solution by setting the price using a rule sounds like the way to go to me. But your function that fetches values from your external source should be cached statically so that it only fetches values once (one http request). Would that do it? lullabot.com/blog/article/beginners-guide-caching-data-drupal-7 – Pontus Nilsson Jun 19 '14 at 8:46
  • OK, so how do I hook into the shopping cart View -before- it displays? What is the hook name? – jchwebdev Jun 19 '14 at 16:35

Your Answer

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

Browse other questions tagged or ask your own question.