7

I ran into an interesting, but probably a common, problem. With caching turned on, stock is not updated immediately for anonymous users (which makes sense as that's the point of caching). However this results in a little bit of a UXWTF. When a product is out of stock, users are still shown that '1' is still available. It's only when they try to add it to the cart, they are met with a notice

The maximum quantity of 'product name' that can be purchased is 0.

I would still like to keep cache pretty much for everything else, but is there a way to disable caching of the available stock count?

6
  • 3
    Can you pull the stock value into a block? If so you could display it as a block and use Ajax Blocks: drupal.org/project/ajaxblocks
    – keva
    Sep 24, 2012 at 17:55
  • Unfortunately this approach would really only work with a single product display/single product entity (or even if there were two or three product variations). This approach can get unwieldily when there are more than a couple product variations for each product display. Sep 24, 2012 at 20:00
  • AJAX and/or ESI/SSI is one answer drupal.org/project/esi. Cache invalidation is the other answer drupal.org/project/cache_actions. Both of these are semi complex and thus not a drop in solution.
    – mikeytown2
    Sep 24, 2012 at 21:50
  • I just came across this issue drupal.org/node/1785158 and the module author quotes "Commerce sites don't work well with caching and drupal commerce is no exception.".
    – junedkazi
    Oct 17, 2012 at 22:22
  • Can't write worthy answer now, but take a look at Drupal 7 - Update node fields via ajax/frontpage - consider just updating that one field using AJAX and timer. That way even QTY changes after user opened page will get reflected.
    – Mołot
    Sep 26, 2013 at 13:49

2 Answers 2

1

Just an idea - in this case a workaround might be to hook a function after an order has been placed and then clean the cache programmatically.

You can use the hook_commerce_checkout_complete() and there call the cache_clear_all() function.

3
  • won't cache_clear_all() be a big performance hit as it is clearing the whole site cache. Also if the site is a busy one it might just choke up.
    – junedkazi
    Oct 17, 2012 at 22:18
  • True; I was thinking about the same and the solution would be to clean only cache from Commerce module. Not sure what exactly should be called or if there is a function for that. Oct 18, 2012 at 6:02
  • 2
    @junedkazi cache_clear_all is actually somewhat poorly named. It accepts a parameter, where you can specify individual pages/cached objects to clear.
    – jmking
    Mar 9, 2014 at 13:58
0

The caching mecanism in Drupal is very flexible. You could use a custom cache for the parts of a page you want to be "un-cached" on certain conditions, and when the condition is met (a checkout), just reset the cache for that particular $cid in the checkout's form submit.

See this post for a good overview of the caching api.

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.