Fetch Product information in WooCommerce

Product Attributes without Variation Don’t Display in Shop page & Product Page

Add Attribute and Variations in Product ( Refer blog )

Note : Product variations is Used Only Variable Product.

And Then after put this code in function.php

   add_action( 'woocommerce_after_shop_loop_item_title', 'bbloomer_echo_stock_variations_loop' );

function bbloomer_echo_stock_variations_loop(){
    global $product;
    if ( $product->get_type() == 'variable' ) {
        foreach ( $product->get_available_variations() as $key ) {
            $attr_string = array();
            foreach ( $key['attributes'] as $attr_name => $attr_value ) {
                $attr_string[] = $attr_value;
                $arr_names[] = $attr_name;
            }

            $attr_color = $arr_names[0];
            $attr_size = $arr_names[1];
            $strle="attribute_pa_";

            $attr_name_a  = substr($attr_color, strlen($strle));
            $attr_name_a2  = substr($attr_size, strlen($strle));

           
            if ( $key['max_qty'] > 0 ) { 
              echo '<div><p>' .$attr_name_a.' : '.$attr_string[0].' , '.$attr_name_a2.' : '.$attr_string[1].' , ' . $key['max_qty'] . ' in stock</p></div>'; 
            } else { 
              echo '<div><p>' .$attr_name_a.' : '. $attr_string[0].',' .$attr_name_a2.' : '.$attr_string[1].'   out of stock</p></div>'; 
            }
        }
    }
}

Look Like Below image :

enter image description here

Leave a Comment