如何在 WooCommerce 挂钩函数上输出多个自定义字段?
How to output multiple custom fields on a WooCommerce hooked function?
我正在使用以下代码将数据从 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', '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);
应该可以。
我正在使用以下代码将数据从 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', '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);
应该可以。