In this post we shall learn on how to create a new shipping method in addition to existing ones and exploring some options from Shipping Method API of WooCommerce. Since version 2.6 of WooCommerce, there are only 3 default types of shipping methods. They are as follows:
- Flat Rate
- Free Shipping and
- Local Pickup
Let us follow the below mentioned steps to add our new shipping method called Tyche Shipping.
1. Register shipping method
WooCommerce has a filter called
woocommerce_shipping_methods. The function hooked to this filter will have the current available shipping methods in the form of an array. We shall add our shipping method tyche_method as shown in the below snippet.
Note that WC_Shipping_Tyche is the name of the class for which we are adding the shipping method. Here is a sample snippet of the class WC_Shipping_Tyche which is an extension of WC_Shipping_Method class.
Let’s take a look at a few properties of the WC_Shipping_Method that we just extended to create a new class for our Tyche Shipping Method.
- id – A unique text based ID for the new shipping method you are adding. Here we have used our ID as
- instance_id – Unique instance ID of the method (zones can contain multiple instances of a single shipping method). This ID is stored in the database.
- method_title – The title applied to the shipping method. This is not the same title that will be displayed on the front-end on checkout page but used only for backend settings. For our shipping method we have used Tyche Shipping Method as the title
- supports – This property is used to determine which all features the new shipping method will support. This property is an array with possible values as:
- shipping-zones – Shipping zone functionality.
- instance-settings – Instance settings screens.
- settings – Non-instance settings screens. Enabled by default for backward compatibility with methods before instances existed.
- instance-settings-modal – Allows the instance settings to be loaded within a modal in the zones UI.
- enabled – Stores whether or not the instance or method is enabled. In the above snippet we have assigned the instance which is saved in database. This value will be displayed in Shipping Methods section of the shipping zone.
- title – Stores the title of your method which can be used on the frontend for Checkout and Cart Pages.
- instance_form_fields – This property is an array of settings fields used at instance level. Here we have shown just one setting which is the title to be displayed on the frontend of the store that we had discussed above.
Now that the shipping method has been registered, let us verify and check if it is getting displayed and settings getting saved and applied.
2. Verify the newly created Shipping Method
The settings for shipping are available under WooCommerce -> Settings -> Shipping. Let us create a new Shipping Zone by clicking on Add Shipping Zone.
On clicking on Add Shipping Method under Shipping Methods tab, a pop-up modal shall be displayed like one shown below. Now you will notice our newly created shipping method Tyche Shipping Method getting listed in the dropdown list.
The settings associated with this shipping method, in our case the Title to be displayed on frontend can be configured and edited as per our needs. Just a reminder that we had added title element in instance_form_fields property of the Shipping Method API.
The above setting can also be displayed in a modal pop-up by adding
instance-settings-modal value to
supports property array. The default value, Test Method can be changed by changing the value of the default key of the
title element of
Increase WooCommerce Store Sales
"Customer service is great, they go above and beyond to assist and corrected any issues I had with setting everything up in a timely manner. I use the abandoned cart plugin and am very satisfied with the results and carts recovered." - Bran Weintraub
3. Price calculations for newly created Shipping Methods
If the shipping method is enabled it will be displayed on the Checkout page, but how about the pricing for the Shipping Method ? We have a few methods or functions which are provided by the Shipping Method API which we shall use to add pricing options to the shipping method. Let us quickly go through those functions.
calculate_shipping( package )
This function is called to calculate shipping for a package. The function accepts an array containing the package of items to be shipped. One should use
::add_rate() to register calculated rates.
add_rate( args )
This is the actual function that adds rate to the checkout page. It accepts an array as an argument. A brief gist of the args parameter is as follows:
'id' => 'tyche_method',
'label' => 'Tyche Shipping Method',
'cost' => '100',
So let us use the above two methods that we just described in our WC_Shipping_Tyche class so that we can add rates on the checkout page. Add the below given snippet after
__construct function of WC_Shipping_Tyche class.
4. Verify the Shipping Method on frontend
Now that we also added a rate to our Shipping Method, add a product to your shopping cart and verify the shipping method. You should see something like this:
Apart from the properties and methods mentioned above there are a few other properties and methods as well which can be used to create and tweak a Shipping Method as per your needs. Those are available on the following link.
So after following the above steps you shall be able to create a new Shipping Method as per your needs and requirements. I hope you find this tutorial useful in creating and modifying Shipping Methods.
Please mention your queries/suggestions/comments in comments area below and we shall get back to you as early as possible.