Developer Hooks

Reserved Stock Pro for WooCommerce has plenty of developer hooks available for you to customize the features to your needs.

Keep in mind that these are examples only. You’ll need to customize the snippets to your own use case. You should add them to your child themes functions.php file or a custom plugin.

How do I limit what products are stock controlled in the cart?

/** * Skip specific products from stock resveration. $product is the entire product object. */ function puri_reserved_stock_pro_should_handle_product_stock( $should_handle, $product ) { if ( $product->get_id() === 100 ) { $should_handle = false; // Don't reserve stock for product 100. } return $should_handle; } add_filter( 'reserved_stock_pro_should_handle_product_stock', 'puri_reserved_stock_pro_should_handle_product_stock', 10, 2 );

You can use the reserved_stock_pro_should_handle_product_stock filter to specifically exclude products from Cart & Stock Control. The example below checks if the product has an ID of 100 and then skips it.

How do I customize the messages per product?

You can change the messages that appear in WooCommerce per product using the product id. You’ll need to hook into the cart_control_filter_reserved_until_message filter. Here’s an example.

/** * Change the text on reserved products the cart. */ function puri_reserved_stock_pro_filter_reserved_until_message( $message, $product_id, $cart_item_key ) { if ( $product_id === 100 ) { $message = 'Custom reserved until {expiration_date}'; // Only for the product with an ID of 100. } return $message; } add_filter( 'reserved_stock_pro_filter_reserved_until_message', 'puri_reserved_stock_pro_filter_reserved_until_message', 10, 3 );

These filters will allow you to change the other messsages dynamically in the same manner as above.

  • reserved_stock_pro_filter_added_to_cart_notice
  • reserved_stock_pro_filter_removed_from_cart_notice
  • reserved_stock_pro_filter_not_enough_stock_notice
  • reserved_stock_pro_filter_single_all_stock_reserved_message
  • reserved_stock_pro_filter_single_single_all_stock_available_message
  • reserved_stock_pro_filter_single_has_reserved_stock_message
  • reserved_stock_pro_filter_out_of_stock_text

How do I add support for a custom product type?

You can use the reserved_stock_pro_supported_product_types filter. We use this filter to make sure Cart & Stock Control doesn’t try to control the stock of product types that aren’t fully tested or supported yet. Make sure that you completely test that everything is working well with your custom product type before you enable Cart & Stock Control custom product types on a production site.

/** * Add or remove specific product types from stock reservations. */ function puri_reserved_stock_pro_supported_product_types( $supported_array ) { array_push( $supported_array, 'custom_product_type' ); // Replace with the name of your product type. return $supported_array; } add_filter( 'reserved_stock_pro_supported_product_types', 'puri_reserved_stock_pro_supported_product_types', 10 );

Change the date format

You can change the date format of the expiration date on the product in cart via this PHP snippet. The date format uses the standard PHP date format. You can reference the format options here https://www.php.net/manual/en/function.date.php

/** * Change the date format the expiration date appears. * Find examples of PHP date formats here: https://www.php.net/manual/en/function.date.php */ function custom_reserved_stock_pro_date_format( $format ) { $format = 'g:i a, F j, Y'; // New PHP Date format without the timezone. return $format; } add_filter( 'reserved_stock_pro_filter_date_format', 'custom_reserved_stock_pro_date_format', 10 );

Remove Defaults

Remove any defaults so you can provide your own if needed.

/** * Remove defaults in Reserved Stock Pro. * So you can safely add your own styles without overriding. */ // Remove the default CSS. add_filter( 'rsp_default_countdown_css', '__return_false' ); // Remove the default countdown location. add_filter( 'rsp_default_countdown_location', '__return_false' );

Custom Countdown Location

Reserved Stock Pro will display the countdown as a sticky popup timer on every page. We believe this is the best default to have for most sites. Don’t worry you can still do custom countdowns easily. Just make sure to remove the default countdown location and CSS if needed before creating a custom location.

Here is an example for making the RSP countdown into a top bar/banner across the site.

/** * Makes the Reserved Stock Pro countdown display as a top bar. * Add your own CSS inline or copy it into your themes custom CSS. * * Note: This method requires your theme to support 'wp_body_open'. */ function puri_rsp_countdown_bar() { // New custom CSS. Can be place here or in your theme. // Make sure you've removed the default styles first. $css = ' .rsp-countdown-wrapper { background: black; color: white; text-align: center; padding: 15px 0; width: 100%; } .rsp-countdown-wrapper.rsp-no-reservations { display: none; } .rsp-countdown-wrapper.rsp-is-counting { } .rsp-countdown-wrapper.rsp-is-expired { color:red; } '; echo '<style>' . $css . '</style>'; // End of custom css. // Output the Resvered Stock Pro Countdown. echo do_shortcode( '[rsp_countdown]' ); } add_action( 'wp_body_open', 'puri_rsp_countdown_bar' );

Change add to cart buttons based on stock

The add to cart button text on the shop page, may still displayed “add to cart” in your theme. if you click the button on a product that is out of stock, then WooCommerce will attempt to add the product to cart and display the notice stating thare is no stock left.

You can the button text based on stock availability using this snippet as an example. Note that changing the button with the woocommerce_loop_add_to_cart_link filter will also change the button html. Therefore the button will no longer add to cart when clicked. Customers will simply be redirected to the product page instead.

/** * Change Add to cart buttons when all stock is reserved. * Buttons will link to the product instead of trying to add to cart. * * @param [type] $add_to_cart_html * @param [type] $product * @return void */ function puri_custom_add_to_cart_button( $default_button_html, $product ) { // Make sure we have our function. if ( function_exists( 'rsp_get_stock_amounts' ) ) { $new_button_text = false; $stock_total = rsp_get_stock_amounts( $product->get_id() ); if ( $stock_total && ! empty( $stock_total['status'] ) ) { // if stock is zero or less then sold out. if ( $stock_total['status'] === 'out_of_stock' ) { $new_button_text = 'Sold Out'; // Change here. } if ( $stock_total['status'] === 'fully_reserved' ) { $new_button_text = 'Last Stock Reserved'; // Change here. } } if ( $new_button_text ) { // Product link without adding to cart. $button_html = '<a class="button" href="' . $product->get_permalink() . '">' . $new_button_text . '</a>'; return $button_html; } } return $default_button_html; } add_filter( 'woocommerce_loop_add_to_cart_link', 'puri_custom_add_to_cart_button', 10, 2 );
Example of add to cart buttons changed based on stock levels.

Display Product Stock Status via shortcode

We’ve made shortcode that will allow you display a dynamic messaged based on the product reserved stock levels. You can place this anywhere, even custom landing pages.

Make sure to set the following in the shortcode

  • product_id
  • out_of_stock_text
  • fully_reserved_text
  • available_text
[rsp_product_stock_status product_id="2362" out_of_stock_text="Currently out of stock" fully_reserved_text="All stock reserved - Check back in a few minutes" available_text="All {available_quantity} is available"]
Was this page helpful?