Search code examples
phpwordpresswoocommercecustom-taxonomyproduct-variations

Display product attributes not set for variation on Woocommerce product variations


I have custom tab on each product variation with some 'variation description' static content and that I have displayed on product page as well in > product summary when I select a variation (like in this link).

And now I need to display the same on shop page in product loop (see my shop page).

OR even better to display specific attributes. I tried this code:

add_action( 'woocommerce_after_shop_loop_item', 'custom_before_title' );
function custom_before_title() {
    global $product;

    echo '<h4><b>Seizoen:</b>' . $product->get_attribute('pa_seizoen') .'</h4>';
    echo '<h4><b>Maat:</b> ' . $product->get_attribute('pa_maat') .'</h4>';
    echo '<h4><b>Tijk:</b> ' . $product->get_attribute('pa_tijk-weving') .'</h4>';
    echo '<h4><b>Vulkracht:</b> ' . $product->get_attribute('pa_fullkraft[') .'</h4>';
    echo '<h4><b>Vulkracht:</b> ' . $product->get_attribute('pa_vulling') .'</h4>';
}

But whatever I do I get same result, it displays only product attributes that are in the product variations: first two "season and size".

Any help is appreciated.


Solution

  • You are using a plugin (or some customizations) that is displaying on your shop page (or archive pages) each product variations individually.

    So you need to get the parent variable product for other product attributes that are not set for variations (and the product description or other required related data):

    add_action( 'woocommerce_after_shop_loop_item', 'custom_before_title' );
    function custom_before_title() {
        global $product;
    
        if( $seizoen = $product->get_attribute('pa_seizoen') ) {
            echo '<h4><strong>Seizoen:</strong>' . $seizoen . '</h4>';
        }
        if( $maat = $product->get_attribute('pa_maat') ) {
            echo '<h4><strong>Maat:</strong> ' . $maat . '</h4>';
        }
    
        $parent_id = $product->get_parent_id(); // Get the parent Variable Product ID
        // Below, all Parent variable product data
        if( $parent_id > 0 && $parent_product = wc_get_product($parent_id) ) {
            if( $tijk_weving = $parent_product->get_attribute('pa_tijk-weving') ) {
                echo '<h4><strong>Tijk:</strong> ' . $tijk_weving . '</h4>';
            }
            if( $fullkraft = $parent_product->get_attribute('pa_fullkraft') ) {
                echo '<h4><strong>Vulkracht:</strong> ' . $fullkraft . '</h4>';
            }
            if( $vulling = $parent_product->get_attribute('pa_vulling') ) {
                echo '<h4><strong>Vulling:</strong> ' . $vulling . '</h4>';
            }
    
            // Display the parent variable product description
            echo '<p><strong>Beschrijving:</strong> ' . $parent_product->get_description() . '</p>';
    
            // Display the parent variable product short description
            echo '<p><strong>Korte beschrijving:</strong> ' . $parent_product->get_short_description() . '</p>';
        }  
    
        // Display the product variation description
        // echo '<p><strong>Beschrijving:</strong> ' . $product->get_description() . '</p>';
    
        // Display the product variation weight
        // echo '<p><strong>Gewicht:</strong> ' . wc_format_weight( $product->get_weight() ) . '</p>';
    
        // Display the product variation dimensions
        // echo '<p><strong>Dimensies:</strong> ' . $product->get_dimensions() . '</p>';
    }
    

    Code goes in functions.php file of your active child theme (or active theme). It should work now.