问题描述
我正在使用以下代码将数据从ACF获取到单个产品页面上的附加信息标签。现在,我需要为这些标签添加多个值。我尝试了一些尝试,但似乎无法弄清楚如何在这段代码中添加更多内容。我还需要在要拉的值后面添加“ cm”,因为它是尺寸。但是第一个问题是最重要的。会感激的!
function yourprefix_woocommerce_display_product_attributes($product_attributes,$product){
$product_attributes['customfield'] = [
'label' => __('Zithoogte','text-domain'),'value' => get_post_meta($product->get_ID(),'_custom_Meta_field1',true),];
return $product_attributes;
}
add_filter('woocommerce_display_product_attributes','yourprefix_woocommerce_display_product_attributes',10,2);```
解决方法
已更新-在每个自定义字段上包含if语句,以检查该值是否为空
您是否尝试过使用(对于多个自定义字段)类似的内容:
function yourprefix_woocommerce_display_product_attributes( $product_attributes,$product ){
// First custom field
$value1 = get_post_meta($product->get_ID(),'_custom_meta_field1',true);
if ( ! empty( $value1 ) ) {
$product_attributes['customfield1'] = [
'label' => __('Zithoogte','text-domain'),'value' => $value1 . ' cm'
];
}
// 2nd custom field
$value2 = get_post_meta($product->get_ID(),'_custom_meta_field2',true);
if ( ! empty( $value2 ) ) {
$product_attributes['customfield2'] = [
'label' => __('Label text 2','value' => $value2 . ' cm'
];
}
// 3rd custom field
$value3 = get_post_meta($product->get_ID(),'_custom_meta_field3',true);
if ( ! empty( $value3 ) ) {
$product_attributes['customfield3'] = [
'label' => __('Label text 3','value' => $value3 . ' cm'
];
}
return $product_attributes;
}
add_filter('woocommerce_display_product_attributes','yourprefix_woocommerce_display_product_attributes',10,2);
应该可以。