Support » Plugin: WooCommerce Cart Stock Reducer » Hooks for when a product is reserved

  • Resolved ljackl

    (@ljackl)


    Hey!

    I’ve set my ‘Sold Out’ text to say ‘Reserved’ when a product is added to someones cart (I only have 1 of each product).
    After a product has actually been sold and is not just in someones cart, the label doesn’t change. This leads to products that are sold showing up as ‘Reserved’.

    The problem is that some customers who are familiar with the cart hold feature are waiting around for a product which will obviously never become available again unlike the others which are on a timer.

    Are there any hooks I can use which are triggered when a product is placed on hold? I’d like the ‘Sold Out’ text to say ‘Reserved’ while the item is on hold and ‘Sold’ once the item has been checked out.

    Hopefully I’ve made enough sense, any help would be appreciated!

Viewing 15 replies - 1 through 15 (of 20 total)
  • Plugin Author James Golovich

    (@jamesgol)

    How are you changing the existing text? I don’t believe I have a method to override the ‘Out of stock’ text setup but there should be some WooCommerce hooks to change the default text that is displayed or just override it before being displayed.

    In a pinch when I’ve worked with other plugins that didn’t have hooks to change some text but did have proper internationalization functions setup I would hook into those filters to adjust text.

    Thread Starter ljackl

    (@ljackl)

    Hi James,

    The theme I’m using has a field in the customiser to change the Out of Stock text. I’ll ask the devs what they’re using and override it if I can.

    What I need now is a way for determining whether an item is reserved by the cart stock reducer, or if it’s actually sold and no longer available. Do you have any idea on the best way to do that?

    Plugin Author James Golovich

    (@jamesgol)

    What is the theme you are using? You might have mentioned it before but I don’t recall.

    There isn’t a function to easily call to grab what you are looking for, it’s always been easiest to stick within the WooCommerce methodology. So depending on how the theme is handling it there might be a few different methods to do it.

    Thread Starter ljackl

    (@ljackl)

    Hi James,

    I’m using a theme called Woostify. I just spoke with their support team, who gave me the following code, which uses a built in WooCommerce filter:

    add_filter('woocommerce_get_availability', 'availability_filter_func');
    function availability_filter_func($availability)
    {
    $availability['availability'] = str_ireplace('Out of stock', 'Sold Out', $availability['availability']);
    return $availability;
    }

    So now that we’ve got the methodology down, what would you suggest is the best way to distinguish between a reserved item and a sold item?

    As a total aside if it’s not too much trouble, I never can tell how people seem to know what the data structures being returned by these hooks contain, what the secret? I’m using this site to look them up: http://hookr.io/filters/woocommerce_get_availability/ but I must be missing something, as all I can see from that page is that i’m getting an array back.

    Anyways, your help is as always appreciated!

    • This reply was modified 2 years, 5 months ago by ljackl.
    Plugin Author James Golovich

    (@jamesgol)

    I think I’m a little confused here. Why don’t you set the ‘Pending Order Text’ in the plugin config to ‘Reserved’ and then using your theme change the out of stock text to ‘Sold Out’? That seems like it should do what you need.

    I generally just dive right into the code to find out how something works, I don’t trust documentation to be accurate or up to date.

    Thread Starter ljackl

    (@ljackl)

    That is one option, however it looks confusing as the users would be told an item is “Sold Out” by the oos text but then that it is reserved but the pending order text.

    Check out the screenshot here for how I have it looking currently: https://snipboard.io/n7OES5.jpg

    The problem I have is that I want to leave products active on the site once they are sold. The reason being that if a customer wants return a product, they still have access to the product description so they can verify for themselves if the item was as described without needing to contact us. However, with the item still accessible on the site and listed as ‘Reserved’, other customers have waited extended periods for the items to come back in stock which is obviously quite annoying.

    The functionality i’d like to achieve is:
    1. A user adds something to cart and the item is marked as reserved.
    – OOS text: ‘Reserved’
    – Pending Order Text: On Hold
    2. Once the item is sold, the OOS text changes to ‘Sold Out’

    The solution I have in my head is to set the OOS text to “Sold Out” in the theme settings but then override it while it’s on hold to say ‘Reserved’ instead.
    Now that you mention the Pending Order Text though, is there perhaps something I can use there?

    Plugin Author James Golovich

    (@jamesgol)

    You are talking about the ‘Reserved’ text that is over the image right? This must be something the theme is doing?

    If that is the case then we need to see how they are determining that. Otherwise the default stock handling should be working here.

    Plugin Author James Golovich

    (@jamesgol)

    I see what is happening now. I didn’t realize this was a separate out of stock label that is handled by the theme.

    I’ve come up with a solution that solves the problem, I just need to add a filter to the plugin that allows adjusting what gets the real stock status/quantity and what gets the virtual.

    Thread Starter ljackl

    (@ljackl)

    Apologies, I hadn’t realised this was custom functionality provided by the theme. I just took the label that was in the top left corner and centred it with css, assuming the label was a normal WooCommerce thing.

    So am I understanding correctly when I say that this label is using default WooCommerce functionality but just being output over the top of the product image by the theme?

    Regarding the solution, how exactly would something like this be implemented to provide the functionality I’d like to achieve?

    Plugin Author James Golovich

    (@jamesgol)

    If you want to have the overlay text displayed differently you could replace the function they use to display the text. They don’t have a filter to use to adjust the text at all. If you look in woostify/inc/woocommerce/woostify-woocommerce-template-functions.php there is a ‘woostify_print_out_of_stock_label’ function and it checks if it exists before adding it, so you could replace it before that gets loaded.

    What I will do is add a filter to the plugin that allows for a whitelist of functions (in the stack) that get the real vs virtual stock. Then it will make sure it doesn’t display the out of stock text when the item is only virtually out of stock.

    Thread Starter ljackl

    (@ljackl)

    Oh okay yep I had a look at the function & replacing it seems like a fairly simple task.

    If I understand what you’re saying correctly, the process be something like:
    1. Use your new filter to add the ‘woostify_print_out_of_stock_label’ function to the whitelist.
    2. Override the ‘woostify_print_out_of_stock_label’ in functions.php and check if the item has any real or virtual stock and change what the function returns accordingly

    Once I’ve added the function to the whitelist, how do you go about retrieving the real and virtual stock levels in the new version of the ‘woostify_print_out_of_stock_label’ function?

    • This reply was modified 2 years, 5 months ago by ljackl.
    Plugin Author James Golovich

    (@jamesgol)

    I’m going to need to add a new function to return the actual stock without jumping through a bunch of hoops.

    Once I add the ‘get_actual_stock_available()’ function you could add something like this to a plugin (somewhere that gets loaded before the theme does)

    
    if ( ! function_exists( 'woostify_print_out_of_stock_label' ) ) {
    	/**
    	 * Print out of stock label
    	 */
    	function woostify_print_out_of_stock_label() {
    		global $product;
    		$out_of_stock = woostify_product_out_of_stock( $product );
    		$options      = woostify_options( false );
    
    		if ( ! $out_of_stock || 'none' === $options['shop_page_out_of_stock_position'] ) {
    			return;
    		}
    
    		$csr = WC()->integrations->get_integration('woocommerce-cart-stock-reducer');
    		$actual_stock = $csr->get_actual_stock_available( $product );
            if ( $actual_stock > 0 ) {
                $oos_text = 'On Hold';
            } else {
    	        $oos_text = $options['shop_page_out_of_stock_text'];
            }
    
    		$is_square = $options['shop_page_out_of_stock_square'] ? 'is-square' : '';
    		?>
    		<span class="woostify-out-of-stock-label position-<?php echo esc_attr( $options['shop_page_out_of_stock_position'] ); ?> <?php echo esc_attr( $is_square ); ?>"><?php echo esc_html( $oos_text ); ?></span>
    		<?php
    	}
    }
    Thread Starter ljackl

    (@ljackl)

    Oh wow, thank you very much for that, I didn’t realise you could retrieve a reference to a plugin/integration like that. Very cool.

    So I don’t need to touch the wc_csr_whitelist_get_stock_status or wc_csr_whitelist_get_stock_quantity functions?

    Thank you very much for the solution. I’ll update the plugin, implement it and report back.

    • This reply was modified 2 years, 5 months ago by ljackl.
    Plugin Author James Golovich

    (@jamesgol)

    No, you should not need to use the ‘wc_csr_whitelist_get_stock_status’ filters in your setup. I added them because I’m sure other people will run into similar issues with custom plugins/themes in the future.

    Pulling the object instance that way is how WooCommerce has their integrations setup, it’s not like that everywhere.

    Thread Starter ljackl

    (@ljackl)

    Thanks for everything James, the solution you provided above worked perfectly. I really appreciate your help on this one.
    I’ll go ahead and mark this as resolved.

    • This reply was modified 2 years, 5 months ago by ljackl.
Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Hooks for when a product is reserved’ is closed to new replies.