Add a custom field to each shipping method settings in WooCommerce admin

247 Views Asked by At

How can I add comments to the delivery methods To WooCommerce admin shipping section in each shipping method?

I use WoodMart theme and this is only for administrative use (only visible in the admin panel, no need to display it on the site).

I tried this, but it didn't work

add_filter( 'woocommerce_shipping_instance_form_fields', 'add_shipping_comments', 10, 2 ); 
 
function add_shipping_comments( $fields, $instance ) { 
    if ( current_user_can( 'manage_options' ) ) { 
        $fields['shipping_comment'] = array( 
            'title'       => __( 'Shipping Comment', 'woocommerce' ), 
            'type'        => 'textarea', 
            'description' => __( 'Enter any additional comments for this shipping method.', 'woocommerce' ), 
            'default'     => "", 
        ); 
    } 
    return $fields;
}
1

There are 1 best solutions below

0
On BEST ANSWER

Use the following, to add a custom field in WooCommerce admin shipping, to each shipping methods setting fields:

add_action('woocommerce_init', 'woocommerce_shipping_instances_form_fields_filters');
function woocommerce_shipping_instances_form_fields_filters(){
    foreach( WC()->shipping->get_shipping_methods() as $shipping_method ) {
        add_filter('woocommerce_shipping_instance_form_fields_' . $shipping_method->id, 'shipping_methods_additional_custom_field');
    }
}

function shipping_methods_additional_custom_field( $settings ) {
    $settings['shipping_comment'] = array(
        'title'         => __('Shipping Comment', 'woocommerce'),
        'type'          => 'text', 
        'placeholder'   => __( 'Enter any additional comments for this shipping method.', 'woocommerce' ),
    );
    return $settings;
} 

Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

enter image description here