Display the product variations stock and name on the shop page in WooCommerce

Display the product variations stock and name on the shop page in WooCommerce on child sites.

Snippet Type

Execute on Child Sites

Snippet

add_action( 'woocommerce_after_shop_loop_item', 'wc_echo_stock_variations_loop' );
    
function wc_echo_stock_variations_loop(){
    global $product;
    if ( $product->get_type() == 'variable' ) {
        foreach ( $product->get_available_variations() as $key ) {
            $variation = wc_get_product( $key['variation_id'] );
            $stock = $variation->get_availability();
            $stock_string = $stock['availability'] ? $stock['availability'] : __( 'In stock', 'woocommerce' );
            $attr_string = array();
            foreach ( $key['attributes'] as $attr_name => $attr_value ) {
                $attr_string[] = $attr_value;
            }
            echo '<br/>' . implode( ', ', $attr_string ) . ': ' . $stock_string;
        }
    }
}
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.