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

How to return product's virtual quantity?

Open quitequinn opened this issue 8 years ago • 13 comments

How do I return product's virtual quantity? Is the also possible to return the count down value?

quitequinn avatar Oct 22 '16 00:10 quitequinn

Can you explain what you want better? Return via PHP code?

jamesgol avatar Oct 22 '16 03:10 jamesgol

Yes, wow your fast. Basically I'm looking for something like this.

echo $product -> virtual_stock( ) 
# 7

echo $product -> hold_stock_timer_total( ) 
# 1800

echo $product -> hold_stock_timer( ) 
# 1243

quitequinn avatar Oct 22 '16 18:10 quitequinn

Nothing like this currently, but when I get a chance I'll add something in. Probably the easiest method will be to use a filter.

I am curious though, what is it you want to do that it doesn't currently do? It might be something that would benefit everyone.

jamesgol avatar Oct 23 '16 22:10 jamesgol

I wish I was more knowledgeable about wordpress filters. Do you have any hints how I would go about creating this filter?

My situation is such: each product is unique so there are only one of each in stock. There will be many people on the site at once. Your plugin successfully prevents multiple users from adding an item to the cart but I also want to display on the front end that that item is in someones cart — hence having a value for the virtual quantity to compare to the actual quantity. This will help me remove the possibility of someone trying to add the item to their cart in the first place.

I also want to have a count down for how long that item will be unavailable for. That way if someone really wants the item they can wait for the count down to end and add that item to their cart.

Thanks for closing the other issue, wasn't sure where it was best to reach you.

quitequinn avatar Oct 23 '16 22:10 quitequinn

Thanks for describing your situation, but it sounds like everything already implemented should cover your use case. Though there might not be enough documentation available that explains it :)

The filter is something that I will have to add to the codebase so you can then call on it, there is no way to currently get at that data since nobody has ever needed to get at it.

If you check the "Hide out of stock items from the catalog" in WooCommerce setting then items that are out of stock (even virtually) will not be shown.

Now if you don't have that checked, someone can see the item but will not be able to add it to their cart but by default a message will be displayed saying: "Check back in

jamesgol avatar Oct 23 '16 23:10 jamesgol

yes, I think all the data I need already exists in the plugin.. I just don't know how to access it.

So if I understand correctly, are you saying that :

  1. the virtually stock quantity is not available within a wordpress loop or via a filter in function.php of a custom theme? It needs to be created within the plugin itself? If that is the case... I think I'm going to have figure out how to edit the plugin to make that value available. I believe your license supports that.. right?
  2. there isn't a value for the time left from adding a product to the cart, or a time when the product was added from the cart that I can compare to the time remaining value?

I understand this wasn't intended as a solution but for clarity: "Hide out of stock items from the catalog" won't work because I still want to display those items.. I just want to label them as out of stock when I display them.

PS. I would be more than happy to help/submit a pull request for writing documentation if you could help me with understanding it.

quitequinn avatar Oct 23 '16 23:10 quitequinn

That is correct, those values aren't currently available to an end developer because nobody has ever needed to handle them directly. I don't have a problem making them available when I get a chance (or if you submit a PR I would consider adding it). I would never recommend someone make a change to a plugin and maintain their own fork of it unless absolutely necessary, even though the license does allow it.

Have you tried just using the code as is? It really sounds like everything you want to do is already handled. If an item is virtually out of stock, it will show the user that.

jamesgol avatar Oct 23 '16 23:10 jamesgol

Righton. I'll take a look at it.

I have.. the issue is that I have to display the unavailable items and differentiate between those out of stock and those in the process of being purchased. If it wasn't for that it would be perfect. Basically these are the three situations I have to display.

Item 1 Available

Item 2 Out of stock

Item 3 Being purchased

quitequinn avatar Oct 23 '16 23:10 quitequinn

If that's the case you might not need any modification to this plugin at all.

You basically want to look for a way to include the stock text on the shop page. I don't have a chance to dig into the code right now (maybe later tonight), but find a filter called on the shop page after the item is displayed. If you call the WooCommerce get_availability() function this plugin will hook in and change the text.

jamesgol avatar Oct 23 '16 23:10 jamesgol

ok thanks, I'll take a look at it!

quitequinn avatar Oct 23 '16 23:10 quitequinn

Figured someone might have tried to do something similar so I looked briefly and saw this: http://gasolicious.com/add-stock-status-woocommerce-shop-category-pages/

<?php
//add action give it the name of our function to run
add_action( 'woocommerce_after_shop_loop_item_title', 'wcs_stock_text_shop_page', 25 );
//create our function
function wcs_stock_text_shop_page() {
    //returns an array with 2 items availability and class for CSS
    global $product;
    $availability = $product->get_availability();
    //check if availability in the array = string 'Out of Stock'
    //if so display on page.//if you want to display the 'in stock' messages as well just leave out this, == 'Out of stock'
    if ( $availability['availability'] == 'Out of stock') {
        echo apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . esc_html( $availability['availability'] ) . '</p>', $availability['availability'] );
    }

}

Something similar should get you most of the way there

jamesgol avatar Oct 24 '16 00:10 jamesgol

EXCELLENT!

screen shot 2016-10-23 at 4 57 17 pm

I still think that if those values were more easily/cleanly available this would be a more powerful plugin but hell ya!

I was going to just store it in a variable on the cleint and edit the layout with javascript but your example shows a similar solution with filters... which I need to learn... so I'm going to try and write one.

Thanks for all your help.

quitequinn avatar Oct 24 '16 00:10 quitequinn

I'll definitely make them available in the future when I can find some free time to fit it in.

jamesgol avatar Oct 24 '16 00:10 jamesgol