【发布时间】:2017-12-30 00:51:00
【问题描述】:
我在配送选项卡的 WooCommerce 产品设置页面中添加了一些自定义字段。
我需要让它在附加信息选项卡的产品页面上可见,并且它可以自动用于比较插件?
我需要在现有的重量和尺寸字段中添加单位。
谢谢。
【问题讨论】:
-
其中一个单位是从三个不同参数创建的维度。
标签: php wordpress woocommerce product dimensions
我在配送选项卡的 WooCommerce 产品设置页面中添加了一些自定义字段。
我需要让它在附加信息选项卡的产品页面上可见,并且它可以自动用于比较插件?
我需要在现有的重量和尺寸字段中添加单位。
谢谢。
【问题讨论】:
标签: php wordpress woocommerce product dimensions
更新 2
根据this answer 对您的一个问题提出的问题,这里是在前端单个产品页面“附加信息”选项卡上获取此自定义产品字段数据的方法。
你会明白的:
对于此产品设置自定义字段:
由于自定义字段保存在产品元数据中,我们使用 Wordpress get_post_meta() 函数以这种方式获取值:
add_action( 'woocommerce_product_additional_information', 'custom_data_in_product_add_info_tab', 20, 1 );
function custom_data_in_product_add_info_tab( $product ) {
//Product ID - WooCommerce compatibility
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Get your custom fields data
$custom_field1 = get_post_meta( $product_id, '_custom_meta_field1', true );
$custom_field2 = get_post_meta( $product_id, '_custom_meta_field2', true );
// Set your custom fields labels (or names)
$label1 = __( 'Your label 1', 'woocommerce');
$label2 = __( 'Your label 2', 'woocommerce');
// The Output
echo '<h3>'. __('Some title', 'woocommerce') .'</h3>
<table class="custom-fields-data">
<tbody>
<tr class="custom-field1">
<th>'. $label1 .'</th>
<td>'. $custom_field1 .'</td>
</tr>
<tr class="custom-field2">
<th>'. $label2 .'</th>
<td>'. $custom_field2 .'</td>
</tr>
</tbody>
</table>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
此代码在 WooCommerce 3+ 上经过测试并且有效
现在要在您的比较功能中使用该数据是另一个问题,您应该在一个新问题中提供此比较功能所涉及的代码......
相关答案:
Add custom dimension fields to each variation settings for variable products
【讨论】:
我试图做同样的事情,但我想将自定义字段添加到现有表中,而不是创建新表。
如果您想将其添加到附加信息表中,您可以使用 woocommerce_display_product_attributes 过滤器
function yourprefix_woocommerce_display_product_attributes($product_attributes, $product){
$product_attributes['customfield'] = [
'label' => __('custom', 'text-domain'),
'value' => get_post_meta($product->get_ID(), 'customfield', true),
];
return $product_attributes;
}
add_filter('woocommerce_display_product_attributes', 'yourprefix_woocommerce_display_product_attributes', 10, 2);
【讨论】: