在附加信息选项卡中显示,一些产品设置自定义字段值

Display in additional information tab, some product settings custom fields values

我在 WooCommerce 产品设置页面的发货选项卡中添加了一些自定义字段。

我需要在“附加信息”选项卡的“产品”页面上使其可见,并且它可以自动用于比较插件?

我需要它在现有的重量和尺寸字段中添加单位。

谢谢。

Update 2

基于您提出的一个问题,这里是在前端单一产品页面的“附加信息”选项卡上获取此自定义产品字段数据的方法。

你会得到:

为此产品设置自定义字段:

由于自定义字段保存在产品元数据中,我们使用 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+ 上测试并有效

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…


相关回答:

我试图做同样的事情,但我想将自定义字段添加到现有 table,而不创建新字段。

Sod 如果你想把它添加到附加信息中 table 你可以使用 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);