显示在附加信息选项卡中,某些产品设置自定义字段值
我已将一些自定义字段添加到运费标签中的WooCommerce产品设置页面。显示在附加信息选项卡中,某些产品设置自定义字段值
我需要使其在“附加信息”选项卡中的产品页面上可见,并且它可以自动用于比较插件?
我需要它添加单位也像现有的重量和尺寸字段。
谢谢。
更新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
感谢您的回答。我把代码放在function.php中,但不会显示表格。我已经将该产品的一些数据添加到有显示内容的字段中,但我看不到它。我对这段代码有个疑问。添加的字段将被添加到现有的表格或作为新的表格?也许有问题。你可以看看[这里](http://www.grandtech。eu/2/product/small-case-dc-171305x /)它的外观如何。附加信息选项卡由我重命名为产品数据。 – Alfista
而这部分是什么:'$ custom_field1 ='first-value'; $ custom_field2 ='second-value'; '它是否满足具有固定信息的变量并覆盖正确的数据? – Alfista
在添加的自定义字段的附加信息选项卡中,它仍然不会在产品页面的前端显示表格或参数。你可以在我上面发布的链接上看到它。而对于维度字段,也不需要改变字段数组的定义,它可以接受三个参数而不只是一个(3x占位符)? – Alfista
其中一个单位是从三个不同参数创建的尺寸。 – Alfista
@LoicTheAztec - 我用你的代码,[链接](https://stackoverflow.com/questions/45212221/add-custom-fields-to-woocomerce-product-setting-pages-in-the-shipping-tab ?answertab = votes#tab-top),我会在产品页面的额外信息标签中看到它,例如尺寸和重量,请参阅图片[link](http://www.grand-tech.eu/2/wp-content/uploads/2017 /07/xxx.jpeg),并且它会自动用于比较函数。您可以在构建网站[链接]上检查它(http://www.grand-tech.eu/2/)。谢谢。 – Alfista