php - WooCommerce 从属性中获取自定义字段
问题描述
是否可以从产品属性中获取自定义字段?并且可以通过变体或产品ID连接吗?我试过这个:
function color_in_loop(){
global $product;
//Getting product attributes
$attributes = $product->get_attributes();
$values = wc_get_product_terms( $product->id, 'pa_color', array( 'fields' => 'all' ) );
foreach ( $values as $term ) {
$icon = get_term_meta('product_attribute_color', 'pa_color_'.$term->term_id);
echo $icon['url'];
}
}
add_action('woocommerce_after_shop_loop_item', 'color_in_loop');
解决方案
你的get_term_meta错了,没必要用
$attributes = $product->get_attributes();
get_the_terms
由于您知道需要图标的特定属性,因此您可以简单地使用该方法来实现您正在寻找的东西。
function color_in_loop(){
global $product;
//Getting product attributes
$product_id = $product->get_id();
$colors = get_the_terms($product_id, 'pa_color');
foreach ( $colors as $color ) {
$icon = get_term_meta($color->term_id,'product_attribute_color',true);
echo $icon['url'];
}
}
add_action('woocommerce_after_shop_loop_item', 'color_in_loop');
它未经测试,但应该可以工作
推荐阅读
- firebase - Firebase 云功能使文档中的用户对象保持最新
- asp.net-core - 当我编译我的 ASP.NET Core 3 应用程序时,我的已发布文件夹如何自动更新?
- python - 使用 plt.legend 编辑时,Seaborn 散点图图例会中断
- javascript - 在 javascript 验证后,即使存在错误,也会执行该功能
- vba - 如何关闭带壳的应用程序
- sas - 如何根据多个指标变量创建类变量
- mysql - 我无法以任何方式连接到 MySQL 服务器(在 Windows 上)
- python - 如何做一系列点击运行的事件
- docker - 通过 Docker Toolbox + kitematic 在企业代理和私有仓库后面的 Windows 7 中运行 docker
- python - sum() 函数中使用的 [] 参数或输入 - 它在做什么?