woocommerce-cart-stock-reducer
woocommerce-cart-stock-reducer copied to clipboard
How to get updated quantity?
Hello,
I installed your plugin and when I add an item to my cart, and I go back to the product page the product quantity is not up to date.
Is there a way to get the "updated" quantity from the $product in a product page so that it always reflects the reduced quantity?
Thanks!
Do you have 'Enable Cart Stock Reducer' checked in the plugin configuration? If so then the quantity reflected should reflect the "virtual" quantity
HI @jamesgol. I'm here because of the issue on WC issue tracker. Thank you so much on going after this solution.
I just installed your plugin and came across the same problem. I'm using a 'built from ground' custom theme and I'll keep investigating this issue. Hope I can help you solving this issue ;)
@bruno-rodrigues Can you supply some more information? Due to the way I'm hooking into the filters calling the usual WooCommerce functions should be returning the proper values. If you can give me some specifics so I can reproduce the issue I can look into solving the problem
@jamesgol I guess the problem is with my theme ;) When I hover over the item it still shows the max amount. Please see the screenshots below:
On load:
On hover:
I take a better look on how I am getting the max stock. I'll keep you updated.
@We solved it using $product->is_in_stock()
instead of $product->get_stock_quantity()
in our theme's conditional.
Let's say I have 2 items in stock, USER A puts 1 item in his cart.
When USER B loads the homepage, $product->get_stock_quantity()
still returns 2 items...
Is this a bug?
For cases when total is 2 and user A puts 1 item in cart, I changed the way to retrieve the total stock amount from
$stock = $product->get_stock_quantity();
to:
$reducer = new WC_Cart_Stock_Reducer();
$stock = $reducer->get_stock_available( $product->id );
It is not most pretty thing in world but it is working ;D
I tried to add add_filter( 'woocommerce_get_stock_quantity', array( $this, 'get_stock_available' ), 10, 1 );
to class-woocommerce-cart-stock-reducer.php:57
but I don't know how to handle the parameters...
Do you know how can we add this filter correctly? This would be THE PRETTY solution, at least for me... Greetings from 🇧🇷
_____ EDIT
Just saw that this is related to #14