Woocommerce Adding Multiple shipping methods

I used the code below (as provided by woocommerce API) to add custom Shipping Method and it is working but now I want to add another shipping method I tried copy pasting same code with different class name but it doesn’t work actually the second method is replacing the first one

I want to know how can I create another Shipping method?
Thank you

    function your_shipping_method_init() {
    if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
        class WC_Your_Shipping_Method extends WC_Shipping_Method {
            /**
             * Constructor for your shipping class
             *
             * @access public
             * @return void
             */
            public function __construct() {
                $this->id                 = 'vip_rate'; // Id for your shipping method. Should be uunique.
                $this->method_title       = __( 'VIP Shipping Rate' );  // Title shown in admin
                $this->method_description = __( '$35 flate rate' ); // Description shown in admin

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

                $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' ) );
            }

            /**
             * calculate_shipping function.
             *
             * @access public
             * @param mixed $package
             * @return void
             */
            public function calculate_shipping( $package ) {


                $cost=35; 

                $rate = array(
                    'id' => $this->id,
                    'label' => $this->title,
                    'cost' => round($cost,2),
                    'calc_tax' => 'per_item'
                );

                // Register the rate
                $this->add_rate( $rate );
            }
        }
    }
}

add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );

function add_your_shipping_method( $methods ) {
    $methods[] = 'WC_Your_Shipping_Method';
    return $methods;
}

add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );

Related posts

Leave a Reply

2 comments

  1. I’m having the same issue , my solution was made a new class who extends from WC_Shipping_Method and keep all the same code there and i made 3 new classes extending the new one , any one with their own ID and mothod type

    Its not the best solution but its more legant than duplicate N times the same code of the class

  2. OK I have succeeded in adding another Shipping method by renaming Class name.Previously I may be doing something wrong

    However I would like to know if there was some better way of doing it because I have copy pasted whole chunk of code twice , My background is not in OOP however I think it is not the proper way of doing this thing