WooCommerce-获取自定义产品属性 [英] WooCommerce - Get custom product attribute
问题描述
我正在尝试在woocommerce中获取特定的自定义属性. 我已经在该站点上阅读了大量的线程,其中提供了大约3-5种方法. 经过全部尝试之后,唯一对我有用的方法是遍历所有属性-所有其他属性均无效. 我有一个名为"pdfs"的自定义属性
以下尝试无效:(链接)
$global product;
$myPdf = array_shift( wc_get_product_terms( $product->id, 'pdfs', array( 'fields' => 'names' ) ) );
$myPdf = $product->get_attribute( 'pdfs' );
$myPdf = get_post_meta($product->id, 'pdfs', true);
这是唯一起作用的:( 我宁愿能够使用第一个选项
任何帮助将不胜感激. 更新:增加了对Woocommerce 3+的兼容性 由于属性始终在数据库中以 I am trying to get a specific custom attribute in woocommerce.
I've read tons of threads on this site which offer about 3-5 methods how to do it.
After trying all, the only method that worked for me is to loop through all attributes - all others did not work.
I have a custom attribute named 'pdfs' The following tries did not work: (link) This is the only one that did work: (link) I would much rather be able to use one of the first options
Any help would be appreciated. Update: Added compatibility for Woocommerce 3+ As attributes are always prepend with Reference: How to get a products custom attributes from WooCommerce 这篇关于WooCommerce-获取自定义产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
谢谢
pa_
开头,因此要使用 wc_get_product_terms()
功能获取它们,您将需要使用 pa_pdfs
,而不是 pdfs
,方法是:global $product;
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id; // Added WC 3+ support
$myPdf = array_shift( wc_get_product_terms( $product_id, 'pa_pdfs', array( 'fields' => 'names' ) ) );
$global product;
$myPdf = array_shift( wc_get_product_terms( $product->id, 'pdfs', array( 'fields' => 'names' ) ) );
$myPdf = $product->get_attribute( 'pdfs' );
$myPdf = get_post_meta($product->id, 'pdfs', true);
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ) {
if (attribute_label( $attribute[ 'name' ] ) == "pdfs" ) {
echo array_shift( wc_get_product_terms( $product->id, $attribute[ 'name' ] ) );
}
}
Thanks
pa_
in DB, for getting them with wc_get_product_terms()
function, you will need to use pa_pdfs
instead of pdfs
, this way:global $product;
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id; // Added WC 3+ support
$myPdf = array_shift( wc_get_product_terms( $product_id, 'pa_pdfs', array( 'fields' => 'names' ) ) );