首页 文章

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

提问于
浏览
2

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

我找到的这段代码显示了属性的标签,但是重复了这个值,如果是类别变量,我真的很挣钱 . 任何帮助是极大的赞赏 .

screenshot

代码:

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

1 回答

  • 0

    问题来自以下行,其中product属性值始终为同一产品属性:

    $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' );
        $attr_output = array();
    
        // 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 = get_taxonomy( $taxonomy )->labels->singular_name;
                    // 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文件中 . 经过测试和工作 .

    要在特定产品类别存档页面上使用它,您将使用the conditional tag,如:

    add_action('woocommerce_after_shop_loop_item','display_loop_product_attribute' );
    function display_loop_product_attribute() {
        global $product;
    
        if( is_product_attribute( array('t-shirts','body-l')) ){
    
            $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) ){
                    if( $value = $product->get_attribute($taxonomy) ){
                    // The product attribute label name
                    $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
                        // 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>';
        }
    }
    

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

相关问题