简体   繁体   English

在特定的 Woocommerce 产品类别档案页面上显示产品属性

[英]Display product attributes on specific Woocommerce product category archives page

I want to show two attributes on the category pages, with the attribute name and value only on specific categories.我想在类别页面上显示两个属性,仅在特定类别上显示属性名称和值。

This code that I found displays the labels of the attributes, but is duplicating the value and I am really struggling with a show if categories variable.我找到的这段代码显示了属性的标签,但正在复制值,而且我真的很费劲来显示类别变量。 Any help is greatly appreciated.任何帮助是极大的赞赏。

截屏

The code:编码:

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

    $product_attributes = array( 'pa_set', 'pa_team');
    $attr_output = array();

    // Loop through the array of product attributes
    foreach( $product_attributes as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
            $value = $product->get_attribute('pa_set');

               if( ! empty($value) ){
                // Storing attributes for output
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': 
    '.$value.'</span>';
            }
        }
    }

    // Output attribute name / value pairs separate by a "<br>"
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output 
    ).'</div>'; 
}

Updated - The problem comes from the following line, where the product attribute attribute value is always for the same product attribute:更新- 问题来自以下行,其中产品属性属性值始终用于相同的产品属性:

$value = $product->get_attribute( 'pa_set' );

and it should be this instead:应该是这样的:

$value = $product->get_attribute( $taxonomy );

The complete revisited code will be:完整的重新访问代码将是:

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

    $product_attributes = array('pa_set', 'pa_team'); // Defined product attribute taxonomies.
    $attr_output = array(); // Initializing

    // Loop through the array of product attributes
    foreach( $product_attributes as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            if( $value = $product->get_attribute($taxonomy) ){
            // The product attribute label name
            $label_name = wc_attribute_label($taxonomy);
                // Storing attributes for output
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
            }
        }
    }
    // Output attribute name / value pairs separate by a "<br>"
    echo '<div class="product-attributes">'.implode('<br>', $attr_output).'</div>';
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明


Targeting Product category archive pages:定位产品类别存档页面:

You will use the conditional tag is_product_category() inside the function on an IF statement…您将在IF语句的函数内使用条件标记is_product_category() ...

For specific product category archive pages, you can set them as explained here inside the function in an array, like:对于特定的产品类别存档页面,您可以按照此处说明在数组中的函数内设置它们,例如:

if( is_product_category( array('chairs', 'beds') ) {
    // Here go the code to be displayed
}

You will just need to set the right product categories slugs in the array…您只需要在数组中设置正确的产品类别 slug...


Related: Show WooCommerce product attributes in custom home and product category archives相关: 在自定义主页和产品类别档案中显示 WooCommerce 产品属性

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

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