在Woocommerce档案上显示可变的产品属性和术语 [英] Display Variable Product Attributes and Terms on Woocommerce Archives

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

问题描述

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

I am trying to accomplish a attribute and term list on the shop page using the hook woocommerce_shop_loop_item_title. The goal is to get the attribute(s) and term(s) for the product and then to display it like this example:

颜色:红色,蓝色,绿色

Color: Red, Blue, Green

大小:小,中,大

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

Dimensions: 90*90, 100*100 and 120*120

但行之间没有空格.

它应该获取"与产品一起使用的所有属性以及属性术语.

It should "fetch" all the attributes used with the product and the attributes terms.

我已经尝试过,但是出现致命错误.

I've tried this but got fatal error.

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的新结果之后,这就是我得到的:

without any result. After trying the new result below from LoicTheAztec, this is what I get:

推荐答案

更新2020 -删除了从词条中获取词名时的错误.

Uppdate 2020 - Removed an error when trying to get the term name from a term slug.

在您的第一个代码段中,存在一些错误:

In your first code snippet there are some mistakes:

  • 未定义$product变量
  • 该功能仅应限于可变产品
  • $attributes_and_terms_names变量未初始化...
  • the $product variable was not defined
  • The function needed to be restricted to variable products only
  • the $attributes_and_terms_names variable was not initialized…

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

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文件中.经过测试,可以正常工作.

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

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

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