简体   繁体   中英

Display specific product attributes under product title in Woocommerce archive pages

In woocommerce, I would like to show some product attributes on shop page under the product titles. This product attributes are "year", "model" and "oil".

This is what I have for now:

add_action('woocommerce_shop_loop_item_title', 'wh_insertAfterShopProductTitle', 15);

function wh_insertAfterShopProductTitle()
{
    global $product;

    $abv = $product->get_attribute('pa_year');
    if (empty($abv))
        return;
    echo __($abv, 'woocommerce');
}

Any help is appreciated.

To display "year", "model" and "oil" product attributes under the product titles in Woocommerce archive pages as shop, use the following:

add_action('woocommerce_shop_loop_item_title', 'display_attributes_after_product_loop_title', 15);
function display_attributes_after_product_loop_title(){
    global $product;

    $output = array(); // Initializing

    // The year
    if( $year = $product->get_attribute('pa_year') ){
        // Save the value in the array
        $output[] = $year; 
    }

    // The model
    if( $model = $product->get_attribute('pa_model') ){
        // Save the value in the array
        $output[] = $model;
    }

    // The type of oil
    if( $oil = $product->get_attribute('pa_oil') ){
        // Save the value in the array
        $output[] = $oil;
    }

    // Output
    if( sizeof($output) > 0 ){
        // Display product attributes coma separated values (you can change the separator by something else below).
        echo implode( ', ', $output);
    }
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

how do i do it Archives?

after i use this code its not Archive(It is no longer a link)

please your help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM