WooCommerce: Display Variations’ Name & Stock @ Shop Page

Thanks to the various requests I get from Business Bloomer fans, this week I’m going to show you a simple PHP snippet to echo the variations’ name and stock quantity on the shop, categories and loop pages.

Display variation details @ WooCommerce shop/loop

PHP Snippet: Display Variations’ Name & Stock @ WooCommerce Loop Pages

/**
 * @snippet       Display Variations' Name & Stock @ WooCommerce Shop Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=22080
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 2.5.2
 */
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_echo_stock_variations_loop' );
function bbloomer_echo_stock_variations_loop(){
global $product;
    if ($product->product_type == 'variable') {
        foreach ($product->get_available_variations() as $key) {
        $attr_string = '';
            foreach ( $key['attributes'] as $attr_name => $attr_value) {
                                $attr_string[] = $attr_value;
            }
            if ( $key['max_qty'] > 0 ) { echo '<br/>' . implode(', ', $attr_string) . ': ' . $key['max_qty'] .' in stock'; } else { echo '<br/>' . implode(', ', $attr_string) . ': out of stock'; }
            }
    }
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (and in case of CSS, this goes at the bottom of the style.css file) – if you need more guidance, please take a look at my free

Mostakim Hossen

I am Mostakim Hossain, a Web designer and developer, 23 years of age. I have been working as a freelance (upwork) for almost 2 years now and I will be glad in helping you create your next awesome project.

I have created lots of public and private websites(some shown below) and always looking for more work to learn from. I am using PHP as my primary programming language. You can see my skill set beside this text.

I am a person who is willing to learn and adapts quickly. I am keen to design and security. I believe in the core principles of Open Source and support it. I have a minimalist approach to design and usability. Simplicity is beauty they say.
Masters of Accountancy : Asian University of Bangladesh
I’m here to answer any questions you might have about our products and services. Feel free to contact me using any of the following methods:

Email : support@smarterdevs.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe
SUBSCRIBE NOW

Coupon : happynewyear

60% OFF

Stay updated with all latest updates,upcoming plugins & much more.
WordPress Themes
Search