Changing ‘Stock Availability’ Text for Product Variations

This documents one method to change the ‘Stock Availability’ text for Woocommerce Product Variations.

My requirement was that when a Product Variation was selected by the user on the frontend, in the following instances I wanted to show the corresponding availability text:

  • Available: ‘[number] units available for your purchase.’ instead of the default ‘Available’.
  • Out Of Stock: ‘Oops, we have no stock left.’ instead of the default ‘Out of stock’.

In order to do this, we need to override the template found in the plugin directory woocommerce/single-product/add-to-cart/variable.php. This is where the ‘Add to Cart’ code for the Variable Product’s page is found.

In this template, you’ll find the following:

<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $available_variations ) ) ?>">

json_encode( $available_variations ) provides the data which is used for updating (via javascript) the corresponding information such as minimum quantity, stock availability on the product page whenever the user selects a product variation on the dropdown select.

The variable $available_variations is an array passed into the page from a WooCommerce function woocommerce_variable_add_to_cart found in includes/wc-template-functions.php. Note that this variable is derived from $product->get_available_variations(), and it returns values (such as min and max quantity, stock availability) for all available Variations found under a Variable Product.

Therefore, we need to update the availability_html value to reflect what we want for each variation, and then output the updated $available_variations for j

So in the variable.php template, I added the following code before the <form> element:

foreach( $available_variations as $i => $variation ) {
    //check if variation has stock or not 
    if ( $variation['is_in_stock'] ) {
        // Get max qty that user can purchase
        $max_qty = $variation['max_qty'];

        // Prepare availability html for stock available instance
        $availability_html = '<p class="stock in-stock">' . $max_qty . ' units available for your purchase.' . '</p>';
    } else {
        // Prepare availability html for out of stock instance
        $availability_html = '<p class="stock out-of-stock">Oops, we have no stock left.</p>';
    }
    $available_variations[$i]['availability_html'] = $availability_html;
}      

And the json_encode found at the form’s data-product_variations attribute will create the updated data.

That’s it.

1 Comment

  1. It is worth noting that this variation information contains most of the product data, including total_sales, and any meta-data you might want to keep private.

    It can be scrubbed by overriding the add-to-cart/variable.php template and iterating through the variations in $available_variations, unsetting things that you do not want scraped/public.

Leave a Reply

Your email address will not be published.

*

© 2017 Bucket Press

Top