How to add a custom delivery method in woocommerce

If you are familiar with woocommerce, you will find it an excellent free e-commerce solution with very useful functions. The extensibility of WordPress module is similar to that of WordPress module. However, if woocommerce needs something else, for example, you will usually find that woocommerce does not provide everything you need, such as you want to add a custom delivery method. Still, don’t worry as much as you do in this simple tutorial. You will learn how to create a simple shipping method for woocomerce. This method calculates the distribution cost.
You will also learn how to add restrictions to the plug-in to use shipping methods in some countries. Custom distribution rules for distribution methods this section creates custom distribution rules for the distribution methods used to generate codes. The distribution method defines the cost calculation method and the distributable location. This means that the cost depends on the weight and region we want to ship. In this example, we will ship to the United States, Canada, Germany, the United Kingdom, Italy, Spain and Kenya. Kenya $10, $1.30 for Italy, $2.50 for Spain, $50 for the UK and $70 for Canada.
For weight, 0 to 10 kg is free, 11 to 30 kg is $5, 31 to 50 kg is $10, 51 to 100 kg is $20. You may be ready to start coding, but not yet! First, you need to understand the woocommerce shipping API. To create a custom delivery method, you must extend the woocommerce abstract class wc_shipping_method. There are several defined properties:$ ID: the ID required for delivery (slug, keyword)$ Number: integer ID$ Method_title: the shipping name displayed in the administrator$ Method_description: briefly describe the shipment displayed in admin (optional)$ Enabled: \
Is_tax(): Returns whether tax should be calculated except freight. Add_rate ($args = array(): push the freight defined in the $args parameter to the $rates attribute. Return has_settings(): $has_settings property value. Is_available(): returns if the goods can be delivered; returns true or false if the $countries property is set to the country \/ region and the $availability property is set to include specific or excluded values. Get_title(): returns the title of this delivery. Get_fee ($fee, $total): according to the analyzed $fee and $total
Public access
*@Param mixed $package
@Invalid return
*\/
Calculate public function delivery ($package){
\/\/Add costs, ratios, and logic here.
}
}
}
}
add_action( ‘woocommerce_shipping_init’, ‘njengahplus_shipping_method’ );
Function add_njengahplus_shipping_method ($methods){
$methods[] = ‘NjengahPlus_Shipping_Method’;
Returns the $method.
}
add_filter( ‘woocommerce_shipping_methods’, ‘add_njengahplus_shipping_method’ );
}How it works first ensures that the constant wpinc is defined. If not defined, someone is trying to access the file instead of directly or word press. Then, you must ensure that woocomerce is active. This is woocomerce Make sure that PHP is under the active_plugins option and is stored in the active plug-in array in the database. Woocommerce_shipping_init is the main task of woocommerce shipping. It contains all delivery classes before instantiation. This operation will include the delivery method at the correct location that woocommerce will use after woocommerce is initialized__ The construct method sets several general properties, some of which can be easily overwritten after loading the settings in the database of the init method. Other methods will be defined in later tutorials, so they are empty. You must then activate the plug-in using the steps shown below.
Log in to the WordPress site and access the dashboard as an administrator. In the dashboard menu, click plug in and activate njengah shipping as follows: After activating the male plug-in, go to woocommerce > settings. Click the woocommerce tab, and then click njengahplus shipping. You should be able to: Male b) country availability setting the distribution method we created can only be used in the list of previously determined countries. Male _add the following content to the construct method: Male method_description = _ (\
\/\/Availability and country
$this – > availability = \
$I – > country = array(
\
\
\
\
\
\/ \/ Spain \
\
);
$this – > initialization ();
\/\/… How the code works. The availability attribute of this delivery is set to include, which is only available in the country of the country attribute.
Woocomerce checks whether the shipping country is included in the attribute country. Create settings, copy this code and fill in the init_form_fields method. Male form_fields = arrange(
\
\
\
\
\
),
\
\
\
\
\
),
);
}Please check the results as follows:
Log in to the WordPress site and access the dashboard as an administrator. From the instrument panel menu
Click woocommerce > settings. Click the shipping tab, and then click njengahplus shipping as follows: Enable the check box and save the changes. You can set the delivery method to deliver up to 100kg. Then, if the rule is about to change, you can modify it in the settings. After setting, the setting code is as follows: Male form_fields = arrange(
\
\
\
\
\
),
\
\
\
\
\
),
\
\
\
\
\
),
);
}The above code now sets the maximum weight in the setting. The results are as follows:
c) Calculate freight calculation update the calculation distribution method at each step to understand each step to calculate freight. First of all, the cost should be based on the weight. To do this, simply add the following code to the compute_shipping method: $values ​​)
{
$_product = $value [data];
[$u weight] + – $s];
}
$weight = wc_get_weight( $weight, ‘kg’ );
If ($) weight ≠ 0,
\
‘GB’ => 2,
\
‘ES’ => 2,
“de”=>1,
\
);
$zoneprices = array(
0 => 10,
1 => 30,
2 => 50,
3 => 70
);
$zoneFromCountry = $countryZones[ $country ];
$priceFromZone = $zonePrices[ $zoneFromCountry ];
$cost += $priceFromZone;
}
We calculated the cost by total weight and increased the cost by delivery country. The last step is to add the registration fee for this code. How the code works the first fragment of this section defines several startup variables, including $weight, $cost, $country. To request the total, repeat the shopping cart and add the weight of each product in the shopping cart to the variable $weight to obtain the total weight. After obtaining the total weight, you are using function wc_get_weight to convert the weight to kilograms. This is because this is the unit that sets the limit in the delivery method. Then finally set the limit to 100 kg. In the second snipt, the $countryzones array retains the region of each country, and the second array $zoneprices retains the price of each region. The results of zone 1 in Italy are as follows: Male: This is the result of span zone 2. The following are the results for Canada. If you encounter difficulties in implementing the code, the whole code is as follows: $values ​​)
{
$_product = $value [data];
$weight = $weight + $_product – > get_weight() * $values [\
}
$weight = wc_get_weight( $weight, ‘kg’ );
If ($) weight ≠ 0,
\
‘GB’ =
> 2,
\
‘ES’ => 2,
“de”=>1,
\
);
$zoneprices = array(
0 => 10,
1 => 30,
2 => 50,
3 => 70
);
$zoneFromCountry = $countryZones[ $country ];
$priceFromZone = $zonePrices[ $zoneFromCountry ];
$cost += $priceFromZone;
$rate = array(
‘id’ => $this->id,
\
\
);
$this->add_rate( $rate );
}
Conclusion this post creates a custom plug-in to add a custom delivery method to woocommerce. In addition, we also share the woocomerce delivery API, which can create the delivery method through simple steps. In addition, when calculating the availability or cost of the distribution method, you can also set restrictions and availability within the distribution method. However, the woocommerce task allows you to set up outside the delivery method. I hope this tutorial provides a solution for adding custom delivery methods to woocommerce. In the similar article woocommerce, the method of adding GTIN number to the product and deleting it from the shopping cart message are added. How to add a trust or security flag on the woocommerce checkout page and log out without confirming woocommerce: \

Author:

Leave a Reply

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