Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom field in shipping method using woocommerce wordpress

How to add custom field in shipping method using woocommerce wordpress

Please see my screenshot shipping method screenshot

like image 968
Nikunj Kathrotiya Avatar asked Oct 21 '25 04:10

Nikunj Kathrotiya


1 Answers

Please try this code.

if ( ! function_exists( 'dimative_shipping_instance_form_fields_filters' ) ) {
    /**
     * Shipping Instance form add extra fields.
     *
     * @param array $settings Settings.
     * @return array
     */
    function dimative_shipping_instance_form_add_extra_fields( $settings ) {
        $settings['shipping_extra_field_title'] = array(
            'title'       => esc_html__( 'Shipping method title', 'zincheco' ),
            'type'        => 'text',
            'placeholder' => esc_html__( 'Please add shipping method title', 'zincheco' ),
            'description' => '',
        );

        $settings['shipping_extra_field_description'] = array(
            'title'       => esc_html__( 'Shipping method description', 'zincheco' ),
            'type'        => 'textarea',
            'placeholder' => esc_html__( 'Please add your shipping method description', 'zincheco' ),
            'description' => '',
        );

        return $settings;
    }

    /**
     * Shipping instance form fields.
     */
    function dimative_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, 'dimative_shipping_instance_form_add_extra_fields' );
        }
    }
    add_action( 'woocommerce_init', 'dimative_shipping_instance_form_fields_filters' );
}
like image 110
Өлзийбат Нансалцог Avatar answered Oct 23 '25 17:10

Өлзийбат Нансалцог



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!