Wikus
Wikus

Reputation: 65

Woocommerce - How to add settings fields to existing or newly created shipping methods programmatically

Hope someone can help me out with this. I need to add a setting field to all the shipping methods being created in WooCommerce.

By default the options are:

How do I add an additional field there so that the options are:

Any help would be greatly appreciated.

Upvotes: 0

Views: 1365

Answers (1)

Oleg Apanovich
Oleg Apanovich

Reputation: 710

With these snippet of code you can set new custom shipping method with additional setting field "Code"

function imp_shipping_method_init() {
    class Imp_WC_Pickup_Shipping_Method extends WC_Shipping_Method {
        /**
         * Constructor for your shipping class
         *
         * @access public
         * @return void
         */
        public function __construct() {
            $this->id                 = 'imp_pickup_shipping_method'; // Id for your shipping method. Should be uunique.
            $this->method_title       = "Custom shipping method";  // Title shown in admin

            $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
            $this->title              = "Custom shipping method"; // This can be added as an setting but for this example its forced.

            $this->enabled = $this->get_option( 'enabled' );
            $this->title = $this->get_option( 'title' );

            add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );

            $this->init();
        }

        /**
         * Init your settings
         *
         * @access public
         * @return void
         */
        function init() {
            // Load the settings API
            $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
            $this->init_settings(); // This is part of the settings API. Loads settings you previously init.

            // Save settings in admin if you have any defined
            add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
        }

        public function init_form_fields() {
            $this->form_fields = array(
                'enabled' => array(
                    'title'     => __( 'Enable/Disable', 'woocommerce' ),
                    'type'       => 'checkbox',
                    'default'     => 'yes'
                ),

                'title' => array(
                    'title'         => __( 'Method Title', 'woocommerce' ),
                    'type'          => 'text',
                    'description'   => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
                    'default'       => __( 'Custom Shipping Method', 'woocommerce' ),
                    'desc_tip'      => true,
                ),

                'code' => array(
                    'title'         => __( 'Code', 'woocommerce' ),
                    'type'          => 'text',
                    'description'   => __( 'Code', 'woocommerce' ),
                    'default'       => __( 'Code', 'woocommerce' ),
                    'desc_tip'      => true,
                ), 
            );
        }

        /**
         * calculate_shipping function.
         *
         * @access public
         * @param mixed $package
         * @return void
         */
        public function calculate_shipping( $package=array() ) {
            $rate = array(
                'id' => $this->id,
                'label' => $this->title,
                // 'cost' => '100',
                // 'calc_tax' => 'per_item'
            );

            // Register the rate
            $this->add_rate( $rate );
        }
    }
}
add_action( 'woocommerce_shipping_init', 'imp_shipping_method_init' );

function add_your_shipping_method( $methods ) {
    $methods[] = 'Imp_WC_Pickup_Shipping_Method';
    return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );

Then you can find new custom method and additional setting code on a woocommerce settings > shipping tab

enter image description here

After it you can get these additional filed on cart-shipping.php template with these snippet of code

$shipping_methods = WC()->shipping->get_shipping_methods();

foreach ( $shipping_methods as $key => $shipping_method ) {

    $code = $shipping_method->settings->code;
}

Similarly you can try deregister default shipping woocomerce methods and register new customs with the same functionality and with new additional setting "Code"

Hope it will help you.

Upvotes: 1

Related Questions