Reputation: 23
I'm using the following bit of code to get data from ACF to the additional info tab on the single product page. Now I need to add multiple of these labels with values. I tried a few things but I cant seem to figure out how to add more in this bit of code. I also need to add ' cm' behind the value that will get pulled because it is a dimension. But the first problem is the most important. Would appreciate!
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);```
Upvotes: 2
Views: 308
Reputation: 254378
Updated - Included if statements on each custom field, checking that the value is not empty
Did you tried using something like (for multiple custom fields):
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', 'text-domain'),
'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', 'text-domain'),
'value' => $value3 . ' cm'
];
}
return $product_attributes;
}
add_filter('woocommerce_display_product_attributes','yourprefix_woocommerce_display_product_attributes', 10, 2);
It should works.
Upvotes: 1