【问题标题】:Display product attributes on specific Woocommerce product category archives page在特定的 Woocommerce 产品类别档案页面上显示产品属性
【发布时间】:2019-04-29 20:16:54
【问题描述】:

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

我发现的这段代码显示了属性的标签,但是复制了值,我真的很努力显示类别变量。任何帮助是极大的赞赏。

代码:

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

【问题讨论】:

    标签: php wordpress woocommerce product custom-taxonomy


    【解决方案1】:

    更新 - 问题来自以下行,其中产品属性属性值始终针对相同的产品属性:

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

    应该是这样的:

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

    完整的重新访问的代码将是:

    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 文件中。经过测试并且可以工作。


    定位产品类别存档页面:

    您将在 IF 语句的函数内使用 the conditional tag is_product_category()...

    对于具体的产品分类存档页面,可以在函数内以数组的形式设置as explained here,如:

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

    您只需要在数组中设置正确的产品类别 slug...


    相关:Show WooCommerce product attributes in custom home and product category archives

    【讨论】:

    • 谢谢 LoicTheAztec!该代码运行良好,但无论我将 if 语句放在哪里,它都会对我产生影响。我到底需要把它放在哪里?
    猜你喜欢
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 2020-01-25
    • 2021-06-13
    相关资源
    最近更新 更多