woocommerce-cart-stock-reducer icon indicating copy to clipboard operation
woocommerce-cart-stock-reducer copied to clipboard

How to get updated quantity?

Open vallieres opened this issue 8 years ago • 6 comments

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!

vallieres avatar Jan 21 '17 23:01 vallieres

Do you have 'Enable Cart Stock Reducer' checked in the plugin configuration? If so then the quantity reflected should reflect the "virtual" quantity

jamesgol avatar Jan 22 '17 00:01 jamesgol

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 avatar Mar 08 '17 09:03 bruno-rodrigues

@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 avatar Mar 08 '17 09:03 jamesgol

@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: screen shot 2017-03-08 at 7 54 05 am

On hover: screen shot 2017-03-08 at 7 54 20 am

I take a better look on how I am getting the max stock. I'll keep you updated.

bruno-rodrigues avatar Mar 08 '17 10:03 bruno-rodrigues

@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?

bruno-rodrigues avatar Mar 08 '17 11:03 bruno-rodrigues

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

bruno-rodrigues avatar Mar 08 '17 12:03 bruno-rodrigues