Search code examples
phpwordpresswoocommercehook-woocommerceproduct-variations

Show list of product variations with price behind it in product grid WooCommerce


I want to echo a list of the product variations in the product grid in WooCommerce.

For example on the shop page there is a product grid with product title + product description. After this I want to echo the product variations with the price.

The product is now like:

Product image
Product title
Product description

For example the product has two variations:

Kids € 12,00
Adults € 16,00

Then the product needs to be like:

Product image
Product title
Product description
Kids € 12,00
Adults € 16,00

Is there any hook for this?


Solution

  • Add the follows code snippet in your active theme's functions.php -

    function woocommerce_after_shop_loop_item() {
        global $product;
        $variation_ids = $product->get_children();
        if( $variation_ids ) {
            foreach ( $variation_ids as $id ) {
                $v_product = wc_get_product($id);
                $variations = $v_product->get_variation_attributes();
                $variations_name = array();
                if( $variations ) {
                    foreach ( $variations as $key => $value ) {
                        $variations_name[] = ucfirst( $value );
                    }
                }
                echo '<p class="variation_price">'. implode( ', ', $variations_name ). " ". wc_price( $v_product->get_price() ).'</p>';
            }
        }
    }
    add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_after_shop_loop_item' );