Alfista
Alfista

Reputation: 35

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

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.

Upvotes: 1

Views: 9108

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253867

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:

enter image description here

For this product settings custom fields:

enter image description here

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

Upvotes: 1

ZecKa
ZecKa

Reputation: 2934

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

Upvotes: 1

Related Questions