简体   繁体   中英

Display product attributes values for specific categories in WooCommerce product loops

I'm building a shop in WP + WooCommerce. I have different types of product categories like discs and bags. For discs products I have some specific attributes like Speed, Glide, Turn and Fade that don't have any other product categories. I want to display these product attribute values only on shop pages under the product picture.

I have found one code for that and I added myself a separation symbol "|", but this separation symbol is now displayed under all the products that are variable.

Is it possible to change the code not to variables but only for specific product categories and sub-categories?

在此处输入图像描述

Code:

add_action( 'woocommerce_before_shop_loop_item_title', 'display_size_attribute', 5 );

function display_size_attribute() {
    global $product;
    
    if ( $product->is_type('variable') ) {
        
        $taxonomy = 'pa_speed';
        echo '<span class="attribute-speed">' . $product->get_attribute($taxonomy) . '</span>' ;
        echo ' | ';
        $taxonomy = 'pa_Glide';
        echo '<span class="attribute-Glide">' . $product->get_attribute($taxonomy) . '</span>';
        echo ' | ';
        $taxonomy = 'pa_Turn';
        echo '<span class="attribute-Turn">' . $product->get_attribute($taxonomy) . '</span>';
        echo ' | ';
        $taxonomy = 'pa_Fade';
        echo '<span class="attribute-Fade">' . $product->get_attribute($taxonomy) . '</span>';
    }
}

Use the following revisited code instead to get separated attribute values only if they exist on your variable products:

add_action( 'woocommerce_before_shop_loop_item_title', 'display_some_attributes', 5 );
function display_some_attributes() {
    global $product;

    if ( $product->is_type('variable') ) {
        $attributes = array('speed', 'Glide', 'Turn', 'Fade'); // here you defined attribute slugs
        $output     = array(); // Initializing

        // Loop through product attributes
        foreach ( $attributes as $attribute ) {
            $taxonomy = 'pa_' . $attribute;
            $values   = $product->get_attribute($taxonomy);

            // If not empty add it to the array
            if ( ! empty($values) ) {
                $output[] = '<span class="attribute-' . $attribute . '">' . $values . '</span>';
            }
        }
        // Convert array to a separated string by pipes
        echo implode(' | ', $output);
    }
}

To target specific product categories only, replace in the code the block:

    global $product;

    if ( $product->is_type('variable') ) {

with (where you will define your product categories terms) :

    global $product;

    $categories = array( 'cats', dogs' ); // Here your category terms ids, slugs or names

    if ( $product->is_type('variable') && has_term( $categories, 'product_cat', $product->get_id() ) ) {

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

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