如何在模板/功能中显示 WordPress WooCommerce 自定义属性? [英] How to display WordPress WooCommerce custom attribute in templates/functions?

查看:80
本文介绍了如何在模板/功能中显示 WordPress WooCommerce 自定义属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

会把它放在 WP Stack Exchange 中,但他们经常说,因为它有 PHP,所以应该在 SO 中,所以永远不确定哪里是最好的.合适的话可以搬家.

Would put this in WP Stack Exchange, but often they say since it has PHP it should be in SO, so never quite sure where is best. Can move if more appropriate.

要显示名为Fabrics"的自定义 woocommerce 产品属性,例如我读过的,您可以执行以下操作.

To display a custom woocommerce product attribute called, "Fabrics" for example I’ve read you could do the following.

$fabric_values = get_the_terms( $product->id, ‘pa_fabrics’);

foreach ( $fabric_values as $fabric_value ) {
   echo $fabric_value->name;
}

但是,有没有更短的方法,因为我们将在整个 php 模板中使用很多属性.

However, is there a shorter way since we would be using a lot of attributes throughout php templates.

例如有没有办法简单地做,echo get_the_terms( $product->id, ‘pa_fabrics’);"

For instance is there a way to simply do, "echo get_the_terms( $product->id, ‘pa_fabrics’);"

或者是否有一个功能可以添加到他们的网站,这样就可以响应任何产品属性,就像在非 WooCommerce 网站上使用高级自定义字段"时一样,像上面这样的一条非常短的行一样?

Or is there a single function one could add to their site, so that would then be able to echo any product attribute which a single very short line like above like you can when use "Advanced Custom Fields" with non WooCommerce sites?

更新

在 SO 上找到 此主题,提供一种创建单个短代码以相对轻松地获取数据的方法.虽然这当然是一种选择,但想看看是否有任何以如下方式构建的清洁器:

Found this thread on SO that offers a way to create a single short code to relatively easily get the data. While that's certainly an option, would like to see if there is any cleaner built in ways such as:

echo get_the_terms( $product->id, 'pa_fabrics');

echo $product->get_attributes('pa_fabrics');

最后一个选项似乎是最干净和最理想的,但会导致错误:致命错误:未捕获错误:调用成员函数 get_attributes() on null in (my functions.php file where the code was added).

The last option seems the cleanest and most ideal but results in an error: "Fatal error: Uncaught Error: Call to a member function get_attributes() on null in (my functions.php file where the code was added).

推荐答案

您的问题的答案是视情况而定.考虑一下您需要多么灵活.

The answer to your question is it depends. Consider for a moment how flexible you need this to be.

让我们先看看这 2 个建议示例有什么问题.

Let's start by looking at what is wrong with the 2 suggested examples.

1) echo get_the_terms( . . .

在使用函数时,了解返回类型很重要.get_the_terms() 成功时将返回一个数组.您需要对该数组执行某些操作才能显示它.

When using a function it's important to know the return type. get_the_terms() will return an array when successful. You need to do something with that array in order to display it.

https://developer.wordpress.org/reference/functions/get_the_terms/

2) echo $product->get_attributes(...

您正朝着正确的道路前进 :) 您看到的错误告诉您 $product 不是您期望的那样.get_attributes()WC_Product 类的一个方法.您需要拥有该类的实例才能使用它.

You're heading down the right path :) The error you're seeing tells you that $product isn't what you're expecting it to be. get_attributes() is a method of the WC_Product class. You need to have an instance of that class in order to use it.

获取产品的一种方法是使用 wc_get_product().

One way of getting hold of the product would be to use wc_get_product().

$product = wc_get_product();

现在您遇到的第二个问题是方法本身.get_attributes()get_the_terms() 一样,将返回一个数组.然后,您有责任显示该数据.

Now the second problem you have is with the method itself. get_attributes(), like get_the_terms(), will return an array. It's then your responsibility to display that data.

相反,我相信您正在寻找 get_attribute().此方法将属性名称作为其唯一参数,并返回一串属性值.

Instead I believe you're looking for get_attribute(). This method takes an attribute name as its only argument and returns a string of attribute values.

示例:

// Get a product instance. I could pass in an ID here.
// I'm leaving empty to get the current product.
$product = wc_get_product();

// Output fabrics in a list separated by commas.
echo $product->get_attribute( 'pa_fabrics' );

// Now that I have $product, I could output other attributes as well.
echo $product->get_attribute( 'pa_colors' );

这篇关于如何在模板/功能中显示 WordPress WooCommerce 自定义属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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