【问题标题】:Display Variable Product Attributes and Terms on Woocommerce Archives在 Woocommerce 档案中显示可变产品属性和术语
【发布时间】:2019-09-20 11:20:56
【问题描述】:

我正在尝试使用钩子woocommerce_shop_loop_item_title 在商店页面上完成属性和术语列表。目标是获取产品的属性和术语,然后像这个例子一样显示它:

颜色:红、蓝、绿

尺寸:小、中、大

尺寸:90*90、100*100 和 120*120

但行之间没有空格。

它应该“获取”与产品和属性术语一起使用的所有属性。

我已经尝试过了,但出现了致命错误。

add_action( 'woocommerce_shop_loop_item_title', 'variable_att_and_terms_on_loop');
function variable_att_and_terms_on_loop() {

    foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ) {

    $taxonomy_label = wc_attribute_label( $taxonomy, $product );

    foreach($terms_slug as $term) {
        $term_name  = get_term_by('slug', $term, $taxonomy)->name;
        $attributes_and_terms_names[$taxonomy_label][$term] = $term_name;
    }
}
foreach ( $attributes_and_terms_names as $attribute_name => $terms_name ) {
    $terms_string = implode( ', ', $terms_name );
    echo '<p>' . $attribute_name . ': ' . $terms_string . '</p>';
}
}

我也试过这个:

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

    $product_attributes = array( 'pa_weight', 'pa_quantity', 'pa_length', 'pa_color' );
    $attr_output = array();

    foreach( $product_attributes as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
            $value = $product->get_attribute('pa_weight');

            if( ! empty($value) ){
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
            }
        }
    }
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}

没有任何结果。 在尝试了以下 LoicTheAztec 的新结果后,我得到了以下结果:

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy taxonomy-terms


    【解决方案1】:

    2020 年更新 - 删除了尝试从术语 slug 获取术语名称时出现的错误。

    在您的第一个代码 sn-p 中有一些错误:

    • $product 变量未定义
    • 该功能需要仅限于可变产品
    • $attributes_and_terms_names 变量未初始化...

    这是重新访问的代码(行之间没有空格)

    add_action( 'woocommerce_shop_loop_item_title', 'variable_att_and_terms_on_loop');
    function variable_att_and_terms_on_loop() {
        global $product;
    
        if( ! $product->is_type('variable') ) return; // Only for variable products
    
        $variation_attributes = $product->get_variation_attributes();
    
        if( sizeof($variation_attributes ) == 0 ) return; // Exit if empty
    
        $attributes = array(); // Initializing
    
        foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ) {
            $taxonomy_label = wc_attribute_label( $taxonomy, $product );
    
            $terms_name = array();
    
            foreach($terms_slug as $term_slug ) {
                // We try to get the term name when it's a term slug
                $term         = get_term_by('slug', $term_slug, $taxonomy);
                $terms_name[] = ! is_a($term, 'WP_Term') ? $term_slug : $term->name; 
            }
            $attributes[] = $taxonomy_label . ':&nbsp;' . implode( ', ', $terms_name );
        }
    
        echo '<div class="product-attributes">';
        echo '<span>' . implode('</span><br><span>', $attributes) . '</span>';
        echo '</div>';
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 谢谢,这行得通。只有一个小细节;它在属性之后插入一个新行/行,并将术语放在一个新行上。
    • 我已经更新了我最初的问题,并附上了一张显示结果的图片。
    • @KevinAronsson 已更新 - 我的实际答案只是回答了您最初的问题……尺寸和重量是其他需要不同的东西,应该在新问题中提出。一次问多个问题是不允许的,应该避免的。
    • 我明白这一点,但在我最初的问题中,我清楚地解释了我需要如何提供数据。但这没关系。如图所示,您能帮忙不要在新行上创建条款吗?
    • @KevinAronsson 已更新以在同一行上获取属性名称和术语。
    猜你喜欢
    • 2019-07-09
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2017-03-27
    • 2019-12-24
    • 2019-04-29
    • 1970-01-01
    • 2019-06-22
    相关资源
    最近更新 更多