Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Custom Description field in Shipping methods (Backend)

I want to add a custom field in Shipping Zone page under shipping method, it will be a text input, the user will able to add a custom message and I'll show that message in the front end.

I noticed it saves the data in wp_woocommerce_shipping_zone_methods table which doesn't have any extra column to save the data; so I think I have to use my custom logic, but I don't know the name of the hook(s).

So my question is, is there is any hook which will help/allow me

  1. To add a custom field.
  2. To add a custom column.

TL;DR: enter image description here

enter image description here

like image 312
Raunak Gupta Avatar asked May 18 '17 08:05

Raunak Gupta


1 Answers

add_action('woocommerce_init', 'shipping_instance_form_fields_filters');

function shipping_instance_form_fields_filters()
{
    $shipping_methods = WC()->shipping->get_shipping_methods();
    foreach($shipping_methods as $shipping_method) {
        add_filter('woocommerce_shipping_instance_form_fields_' . $shipping_method->id, 'shipping_instance_form_add_extra_fields');
    }
}

function shipping_instance_form_add_extra_fields($settings)
{
    $settings['shipping_extra_field'] = [
        'title' => 'Shipping extra field',
        'type' => 'text', 
        'placeholder' => 'shipping',
        'description' => ''
    ];

    return $settings;
} 

Thanks @Wprog_dy for idea, but your code only adds field to the 'flat_rate' shipping method and your function is really weirdly complicated.

My example will add a custom field to all shipping methods

like image 172
Matic Jan Avatar answered Sep 19 '22 13:09

Matic Jan