php - 在 Woocommerce 文件上显示可变产品属性和术语

标签 php wordpress woocommerce custom-taxonomy taxonomy-terms

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

颜色:红、蓝、绿

尺寸:小、中、大

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

但行之间没有空格。

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

我已经尝试过,但出现了 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 的新结果之后,这就是我得到的结果: enter image description here

最佳答案

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

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

  • $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 文件中。经过测试并有效。

关于php - 在 Woocommerce 文件上显示可变产品属性和术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943931/

相关文章:

php - 获取所选字段中可用的 WooCommerce 优惠券的列表

php - 计算来自另一个页面的 <LI> 元素

php - BS Modal 中的 WooCommerce 客户订单详细信息

php - WooCommerce 成员(member)资格检查用户(使用当前成员(member)计划)是否能够访问内容

javascript - 如何在AJAX中同时访问JSON对象、二维和单个数组?

php - 使用单个查询更新多行

php - 在自己的插件中为 Wordpress Media Uploader 自定义上传目录

php - Woocommerce 根据类别对 ASC 和 DESC 进行排序

php - jQuery 和 php 注册脚本不起作用

php - 表格中的重复条目