Reading list Switch to dark mode

    How to create a new shipping method in WooCommerce..?

    Updated 1 August 2023

    Today, we will learn how to create a new shipping method in WooCommerce.

    For this, we will create a separate plugin and go through step by step.

    First of all, Create a function file where all basic details are added like plugin name description author, etc.

    Create function.php

    <?php
    /**
     * Plugin Name:Webkul WooCommerce Shipping
     * Plugin URI: https://webkul.com
     * Description:This is the test shipping from webkul
     * Author: Webkul
     * Author URI: https://webkul.com
     * Version: 1.0.0
     * Text Domain: wkwc-shipping
     * Domain Path: languages/
     *
     * @package Webkul WooCommerce Shipping
     */
    
    /*----- Preventing Direct Access -----*/
    defined( 'ABSPATH' ) || exit;
    
    /**
     * Include your shipping file.
     */
    function wkwc_include_shipping_method() {
      require_once 'wkwc-class-shipping-method.php';
    }
    add_action( 'woocommerce_shipping_init', 'wkwc_include_shipping_method' );
    
    /**
     * Add Your shipping method class in the shipping list
     */
    function wkwc_add_shipping_method( $methods ) {
      $methods[] = 'WKWC_Webkul_Shipping_Method';
      return $methods;
    }
    add_filter( 'woocommerce_shipping_methods', 'wkwc_add_shipping_method' );

    Action woocommerce_shipping_init  – It will be used to include your shipping class file path

    Searching for an experienced
    Woocommerce Company ?
    Find out More

    Filter woocommerce_shipping_methods –  It will be used to add a shipping class in the WooCommerce shipping class list.

    Now it will be listed in the plugin list.

    PluginList

    WooCommerce provides the WC_Shipping_Method class to use and create your own custom shipping method so we just extend here the same class to override the calculate_shipping method and other methods to create the new shipping.

    Now create the class file wkwc-class-shipping-method.php

    <?php
    
    defined( 'ABSPATH' ) || exit;
    
    class WKWC_Webkul_Shipping_Method extends WC_Shipping_Method {
    
      /**
       * Shipping class
       */
      public function __construct() {
    
        // These title description are display on the configuration page
        $this->id = 'wkwc-shipping-method';
        $this->method_title = esc_html__('Webkul Shipping', 'wkwc-shipping' );
        $this->method_description = esc_html__('Webkul WooCommerce Shipping', 'wkwc-shipping' );
    
        // Run the initial method
        $this->init();
    
       }
    
       /**
        ** Load the settings API
        */
       public function init() {
         // Load the settings API
         $this->init_settings();
    
         // Add the form fields
         $this->init_form_fields();
       }
    
       public function init_form_fields() {
    
         $form_fields = array(
    
           'enabled' => array(
              'title'   => esc_html__('Enable/Disable', 'wkwc-shipping' ),
              'type'    => 'checkbox',
              'label'   => esc_html__('Enable this shipping method', 'wkwc-shipping'  ),
              'default' => 'no'
           ),
    
           'title' => array(
              'title'       => esc_html__('Method Title', 'wkwc-shipping' ),
              'type'        => 'text',
              'description' => esc_html__('Enter the method title', 'wkwc-shipping'  ),
              'default'     => esc_html__('', 'wkwc-shipping' ),
              'desc_tip'    => true,
           ),
    
           'description' => array(
              'title'       => esc_html__('Description', 'wkwc-shipping' ),
              'type'        => 'textarea',
              'description' => esc_html__('Enter the Description', 'wkwc-shipping'  ),
              'default'     => esc_html__('', 'wkwc-shipping' ),
              'desc_tip'    => true
           ),
    
           'cost' => array(
              'title'       => esc_html__('Cost', 'wkwc-shipping' ),
              'type'        => 'number',
              'description' => esc_html__('Add the method cost', 'wkwc-shipping'  ),
              'default'     => esc_html__('', 'wkwc-shipping' ),
              'desc_tip'    => true
           )
         );
    
          $this->form_fields = $form_fields;
       }
    
       /**
        ** Calculate Shipping rate
        */
       public function calculate_shipping( $package = array() ) {
          $this->add_rate( array(
            'id'     => $this->id,
            'label'  => $this->settings['title'],
            'cost'   => $this->settings['cost']
          ));
       }
    
    }

    After activating the plugin we can see that Webkul Shipping is listed under the WooCommerce Shipping list.

    ShippingConfig

    Now we can check at the front how shipping is calculated using calculate_shipping method.

    CartPage

    We can see the output on the checkout page.

    CheckOut

    Now we can see the Admin order view page where shipping is added successfully.

    OrderPage

    This is all about how you can add a new shipping method in WooCommerce. Hope you enjoy this article.

    Support

    Still, have any issues feel free to raise a ticket or send us a mail at [email protected] and let us know your views to make the code better

    You may also check out our exclusive WooCommerce Plugins and can also explore our WooCommerce Development Services.

    Thanks for Your Time..!! Have a Good Day..!!

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    4 comments

  • shailesh
  • E. OdL
    • Thakur Amit Chauhan
      • vinu
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content