简体   繁体   English

显示 WooCommerce 产品循环中特定类别的产品属性值

[英]Display product attributes values for specific categories in WooCommerce product loops

I'm building a shop in WP + WooCommerce.我正在 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.对于光盘产品,我有一些特定的属性,例如 Speed、Glide、Turn 和 Fade,这些属性没有任何其他产品类别。 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) : with (您将在其中定义产品类别术语)

    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).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Woocommerce 档案页面上显示特定的产品属性 - Display specific product attributes on Woocommerce archives pages 在特定的 Woocommerce 产品类别档案页面上显示产品属性 - Display product attributes on specific Woocommerce product category archives page 在 Woocommerce 存档页面中的产品标题下显示特定的产品属性 - Display specific product attributes under product title in Woocommerce archive pages 在 Woocommerce 的单个产品页面上显示特定的自定义产品属性 - Display specific custom product attributes on single product pages in Woocommerce 显示特定产品类别的术语或产品属性(woocommerce) - Display the terms or product attributes for a specific product category(woocommerce) 获取 Woocommerce 中特定产品属性值的所有产品变体 - Get all the product variations for specific product attributes values in Woocommerce WooCommerce在产品标题中显示产品类别 - WooCommerce display product categories in product title 在woocommerce中取消特定产品类别的产品标签 - Unset product tabs for specific product categories in woocommerce WooCommerce 如何显示特定产品类别的产品 - WooCommerce how to display products from specific product categories 显示特定产品类别的 WooCommerce 购物车项目简短描述 - Display WooCommerce cart item short description for a specific product categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM