在其他信息选项卡中显示一些产品设置自定义字段值 [英] Display in additional information tab, some product settings custom fields values
问题描述
我在运送"标签的WooCommerce产品设置页面中添加了一些自定义字段.
我需要使其在其他信息"选项卡的产品"页面上可见,并且还可以在比较插件中自动使用它吗?
我需要像现有的重量和尺寸"字段一样添加单位.
谢谢.
更新2
基于对您的一个问题所作的
对于此产品设置自定义字段:
由于自定义字段保存在产品元数据中,因此我们使用Wordpress get_post_meta()
函数以这种方式获取值:
add_action('woocommerce_product_additional_information','custom_data_in_product_add_info_tab',20,1);函数custom_data_in_product_add_info_tab($ product){//产品ID-WooCommerce兼容性$ product_id = method_exists($ product,'get_id')?$ product-> get_id():$ product-> id;//获取自定义字段数据$ custom_field1 = get_post_meta($ product_id,'_custom_meta_field1',true);$ custom_field2 = get_post_meta($ product_id,'_custom_meta_field2',true);//设置您的自定义字段标签(或名称)$ label1 = __('您的标签1','woocommerce');$ label2 = __('您的标签2','woocommerce');//输出回声< 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+上进行了测试并有效
现在要在比较功能中使用该数据是另一个问题,并且应该在新问题中提供此比较功能所涉及的代码... >
相关答案:
I have added some custom fields to the WooCommerce product setting pages in the shipping tab.
I need to make it visible on Product page in Additional Information tab and also that it can be automatically used in compare plugins?
I need that it add the units also as in the existing weight and dimensions field.
Thanks.
Update 2
Based on this answer made to one of your questions, here is the way to get this custom product fields data on front-end single product pages "Additional information" tab.
You will get that:
For this product settings custom fields:
As the custom fields are saved in the product meta data, we use Wordpress get_post_meta()
function to get the values this way:
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>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce 3+ and works
Now to use that data in your compare feature is an other question and you should provide, in a new question, the code involved in this compare feature…
Related answers:
Add custom dimension fields to each variation settings for variable products
这篇关于在其他信息选项卡中显示一些产品设置自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!