在 Woocommerce 存档页面中显示特定的产品属性 [英] Display specific product attribute in Woocommerce archive pages

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

问题描述

我一直环顾四周,试图找到答案,但还没有运气.基本上,我想在存档/商店页面上的产品标题下显示一些元数据.我的属性是颜色",所以在尝试了各种代码之后,我想出了这个:

I've been looking around SO trying to find an answer to this, but no luck yet. Basically, I want to display some meta data under the title of my products on the archive / shop page(s). My attribute is 'colors', so after trying various pieces of code, I came up with this:

add_action( 'woocommerce_after_shop_loop_item', 'acf_template_loop_product_meta', 20 );

function acf_template_loop_product_meta() {

    echo '<h4>Color:' . get_field( '$colors = $product->get_attribute( 'pa_colors' )' .'</h4>';
    echo '<h4>Length:' . get_field( 'length' ) . '</h4>';
    echo '<h4>Petal Count:' . get_field( 'petal_count' ) . '</h4>';
    echo '<h4>Bud Size:' . get_field( 'bud_size' ) . '</h4>';
}

最后三行代码与高级自定义字段相关,它们都可以完美运行.这是试图获取我遇到问题的颜色属性的那个.显示它的正确代码是什么?

The last three lines of code relate to Advanced Custom Fields, and they all work perfectly. It's the one trying to get the color attribute I'm having the issue with. What would be the correct code to display that?

推荐答案

首先如果你使用WC_Product实例对象,你需要在使用任何WC_Product之前调用它并检查它代码> 方法.

First if you use the WC_Product instance object, you need to call it and check it before using any WC_Product method on it.

并且 get_field( '$colors = $product->get_attribute( 'pa_colors' )' 总是会抛出错误.或者您使用 ACF 字段或您获得产品属性pa_colors"要显示的值.

And get_field( '$colors = $product->get_attribute( 'pa_colors' )' will always throw an error. Or you use an ACF field or you get the product attribute "pa_colors" values to be displayed.

尝试以下操作:

add_action( 'woocommerce_after_shop_loop_item', 'acf_template_loop_product_meta', 20 );
function acf_template_loop_product_meta() {
    global $product;

    // Check that we got the instance of the WC_Product object, to be sure (can be removed)
    if( ! is_object( $product ) ) { 
        $product = wc_get_product( get_the_id() );
    }

    echo '<h4>Color:' . $product->get_attribute('pa_colors') .'</h4>';
    echo '<h4>Length:' . get_field('length') . '</h4>';
    echo '<h4>Petal Count:' . get_field('petal_count') . '</h4>';
    echo '<h4>Bud Size:' . get_field('bud_size') . '</h4>';
}

代码位于活动子主题(或活动主题)的 function.php 文件中.它应该有效.

Code goes in function.php file of your active child theme (or active theme). It should works.

这篇关于在 Woocommerce 存档页面中显示特定的产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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