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.
Users can then return back the product page to still see '1' left in stock, add it to cart and do it over and over again.
Any thoughts on refreshing stock levels without resorting to disabling cache entirely or having to flush cache more often?
Comments
Comment #1
guy_schneerson commentedHi nicoz
Commerce sites don't work well with caching and drupal commerce is no exception.
If you have to use caching i would recommend not displaying the stock field and rely on the add to cart validation i.e. "The maximum quantity of 'product name' that can be purchased is 0."
If this is a common issue on your site, i.e. lots of product go out of stock you should consider disabling the rule that disables the add to cart. This will give users a consistent UI where stock is only checked at the point of the add to cart. You can update the rule to say "Sorry we just checked and this product is out of stock".
I cant see any other way around this as i assume you are using page caching (In the future drupal 8 will have better ways of caching)
hope this helps
Comment #2
Neo13 commentedHi,
I solved this by hacking function commerce_ss_stock_adjust($product, $qty). I just added cache_clear_all inside the function. So everytime it is called (stock is changed) the cache for the product will be cleared.
cache_clear_all($product->product_id, 'cache_entity_commerce_product', FALSE);Comment #3
bhavin.ahya commentedHello Neo13,
I am getting error while using cache function as this cache table is not available. Can you please share your commerce_ss_stock_adjust($product, $qty) function. Also share the Commerce_stock version you are using.
Comment #4
AusJohn commented#2 is on the right track with adding a cache clear, but clearing all caches is not a good idea.
For large sites the performance impact could be catastrophic.
We applied patch #5 here:
https://www.drupal.org/project/drupal/issues/2993976
The patch will no longer match up, so will need to apply manually.
This patch is needed for the next part. Otherwise you will get errors.
In commerce_stock/modules/comerce_ss/commerce_ss.rules.inc
function commerce_ss_stock_adjust($product, $qty) {Add entity_get_controller($product->type)->resetCache(array($product->product_id)); to the end before the closing }
This will reset the cache only for the product being updated, leaving all other products alone.
If you also have stock being stored in a custom field on the product display, you'll need to do a DB query to find the Node ID and do a specific cache clear for the node as well.
I.e.
This will clear both the specific product and node while leaving others untouched.
Comment #5
Neo13 commentedHi,
please note that I am passing a product id to the function so it's just clearing the product entity cache for that specific product.
cache_clear_all($product->product_id, 'cache_entity_commerce_product', FALSE);