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 );
Code language: PHP (php)

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 ) { 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, 2 );
Code language: PHP (php)

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

Translating with WPML

Experimental: Translate text using the current WPML language code.

/** * Change the text on reserved products the cart. */ function puri_reserved_stock_pro_filter_reserved_until_message( $message, $product_id, $cart_item_key ) { $current_lang = apply_filters( 'wpml_current_language', null ); if ( $current_lang === 'en' ) { $message = 'Custom reserved until {expiration_date}'; } if ( $current_lang === 'pl' ) { $message = 'Custom reserved until {expiration_date}'; } return $message; } add_filter( 'reserved_stock_pro_filter_reserved_until_message', 'puri_reserved_stock_pro_filter_reserved_until_message', 10, 3 );
Code language: PHP (php)

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 Reserved Stock Pro 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 reservations for 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 );
Code language: PHP (php)

[Experimental] Enable WooCommerce subscription products types: subscription & variable-subscription

/** * Add or remove specific product types from stock reservations. */ function puri_custom_rsp_supported_product_types( $supported_array ) { array_push( $supported_array, 'subscription' ); // Simple Subscription Products. array_push( $supported_array, 'variable-subscription' ); // Variable Subscription products. return $supported_array; } add_filter( 'reserved_stock_pro_supported_product_types', 'puri_custom_rsp_supported_product_types', 10 );
Code language: PHP (php)

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 and

/** * Change the date format the expiration date appears. * Find examples of PHP date formats here: */ 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 );
Code language: PHP (php)

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' );
Code language: PHP (php)

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' );
Code language: PHP (php)

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 );
Code language: PHP (php)
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 (optional if used in a query loop)
  • 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="{available_quantity} is available"]
Code language: JSON / JSON with Comments (json)

Remove Frontend Visuals

We don’t recommend removing any of the fronted visuals added by Reserved Stock Pro, but if you want to here’s the snippet.

/** * Removes all visual changes used in Reserved Stock Pro. * The countdown will work as it's seperate. */ function custom_rsp_remove_frontend_hooks() { /** * Remove Front-end Visual changes. */ // Repleace the default amount of stock with our available stock. remove_filter( 'woocommerce_format_stock_quantity', array( 'Reserved_Stock_Pro_Frontend', 'format_stock_quantity' ), 50, 2 ); // Change the availability text when out of stock. remove_filter( 'woocommerce_get_availability_text', array( 'Reserved_Stock_Pro_Frontend', 'get_availability_text' ), 20, 2 ); // Change CSS Class when out of stock with our new stock. remove_filter( 'woocommerce_get_availability_class', array( 'Reserved_Stock_Pro_Frontend', 'get_availability_class' ), 20, 2 ); // Add reserved expiration message to the product on the cart page. remove_action( 'woocommerce_after_cart_item_name', array( 'Reserved_Stock_Pro_Frontend', 'after_cart_item_name' ), 10, 2 ); // Add reserved expiration message to the product in the mini-cart. remove_filter( 'woocommerce_widget_cart_item_quantity', array( 'Reserved_Stock_Pro_Frontend', 'widget_cart_item_quantity' ), 10, 3 ); // Add reserved expiration message to the product on the checkout page. remove_filter( 'woocommerce_checkout_cart_item_quantity', array( 'Reserved_Stock_Pro_Frontend', 'checkout_cart_item_quantity' ), 10, 3 ); // Add multiple messages about stock reservation on single product pages. remove_action( 'woocommerce_single_product_summary', array( 'Reserved_Stock_Pro_Frontend', 'single_product_summary' ), 35 ); } add_action( 'plugins_loaded', 'custom_rsp_remove_frontend_hooks' );
Code language: PHP (php)

Disable automatic page cache integrations

Reserved Stock Pro has automatic integrations with several caching plugins. The integration is on by default. This setting relates to page cache only!

What does it do?

The integration makes sure that the page cache of the product is purged every time the reservation quantity of a product changes. This is to make sure that customers see the correct stock quantities and not the old cache.

Why disable it?

  1. If you are not displaying the stock numbers or any related reservation quantity information on the frontend, then you can disable the integration.
  2. If you are running a large store where your customers are continuously adding the same products to their cart. Highly frequent reservation changes will result in the page cache being purged quickly without having time to rebuild. In this situation, you may want to disable this feature and remove related reservation quantity from the product page.

Note: The built-in countdown for stock reservations uses the WooCommerce cart fragments and object caching. It’s not affected by the page cache or this setting.

add_filter( 'rsp_enable_page_cache_plugin_intergrations', '__return_false' );
Code language: JavaScript (javascript)

Disable Object caching

Object caching is used to speed performance by reducing calls to the database.

Object Caching are used for the following:

  • The quantity of a product that is currently reserved.
  • The current customer reservation expiration timestamp.
  • Product IDs curently reserved by the customer.
add_filter( 'reserved_stock_pro_disable_object_cache', '__return_true' );
Code language: JavaScript (javascript)

Hook when reserved quantity changes

If you need to do something when the reserved quantity of a product changes then you can use the below hook.

/** * Reserved Stock Pro - Do something when stock reservations change per product. * * @param int $product_id Product ID. */ function custom_reservation_quantity_changed( $product_id ) { // Do something.. } add_action( 'reserved_stock_pro_reserved_stock_quantity_changed', 'custom_reservation_quantity_changed', 10, 1 );
Code language: PHP (php)

Disable Variation in Dropdown when fully reserved/out of stock.

Screenshot of disabled variation dropdown.
Single product variation not selectable.

On single product pages you can disable variations that are fully reserved or out of stock. Each of those variations will be disabled and appear greyed out. Customers will not be able to select the variations.

/** * Disables the variation in the single product dropdown if the variation is fully reserved or out of stock. * * @param bool $is_active should variation be active or disabled. * @param object $variation product object. * @return bool */ function rsp_maybe_disable_variation( $is_active, $variation ) { if ( ! $is_active ) { return $is_active; } if ( function_exists( 'rsp_get_stock_amounts' ) ) { $data = rsp_get_stock_amounts( $variation->get_id() ); if ( ! empty( $data ) ) { if ( $data['status'] === 'fully_reserved' || $data['status'] === 'out_of_stock' ) { return false; } } } return $is_active; } add_filter( 'woocommerce_variation_is_active', 'rsp_maybe_disable_variation', 10, 2 );
Code language: PHP (php)

Modify frontend variation display based on reservations.

The woocommerce_available_variation hook allows you to change various variation data used to create the frontend dropdown.
We recommend that you fully test this hook to make sure you don’t create conflicts with other plugins that could be using it as well.

The below snippet does a couple of thing. You should modify it to fit your needs.

  • Disable the add to cart button when a variation is “Out of stock” and “Fully Reserved”
  • Adds a custom message to variations that are “Fully Reserved”.
/** * Reserved Stock Pro - Apply various of things single product variations. * * Hook in use: * * @param array $variation_data data used to display the available variations. * @param object $product product object. * @param object $variation variation product. * @return bool */ function rsp_filter_variation_data_for_single( $variation_data, $product, $variation ) { if ( is_admin() || ! is_single() ) { return $variation_data; } if ( function_exists( 'rsp_get_stock_amounts' ) ) { $data = rsp_get_stock_amounts( $variation->get_id() ); if ( ! empty( $data ) ) { if ( $data['status'] === 'out_of_stock' ) { // Disable the variation "add to cart" button. $variation_data['is_purchasable'] = false; } if ( $data['status'] === 'fully_reserved' ) { // Disable the variation "add to cart" button. $variation_data['is_purchasable'] = false; // Add a custom message when fully reserved. $variation_data['variation_description'] = $variation_data['variation_description'] . 'All items reserved. Check back in a few minutes.'; } } } return $variation_data; } add_filter( 'woocommerce_available_variation', 'rsp_filter_variation_data_for_single', 10, 3 );
Code language: PHP (php)
Was this page helpful?