在特定的Woocommerce产品类别档案页面上显示产品属性 [英] Display product attributes on specific Woocommerce product category archives page

查看:119
本文介绍了在特定的Woocommerce产品类别档案页面上显示产品属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在类别页面上显示两个属性,仅在特定类别上显示属性名称和值。

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.

代码:

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>'; 
}


推荐答案

已更新-问题来自以下行,其中product属性属性值始终用于相同的product属性:

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' );

应该是这样的:

$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>';
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。

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

定位产品类别存档页面:

您将使用功能标签 is_product_category() IF 语句…

对于特定产品类别存档页面,您可以将其设置为如此处所述在数组中的函数内部,例如:

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
}

您只需要设置正确的产品类别sl数组中的ugs…

You will just need to set the right product categories slugs in the array…

相关:在自定义家庭和产品类别档案中显示WooCommerce产品属性

这篇关于在特定的Woocommerce产品类别档案页面上显示产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆