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

风格不统一 提交于 2020-11-24 20:06:04

问题


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.


回答1:


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




回答2:


I was trying to do the same thing, but I wanted to add the custom fields to the existing table, without creating a new one.

Sod if you want to add it into the additionnal information table you can use woocommerce_display_product_attributes filter

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);


来源:https://stackoverflow.com/questions/45280520/display-in-additional-information-tab-some-product-settings-custom-fields-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!