The development of an online store is a time-consuming and complex process.
There are tons of features to take into account. And before you dive into this process, you need to make sure that you understand what exactly you are supposed to do.
When developing an online store using a combination of WooCommerce and WordPress, besides all the standard shipping methods, one might also need his own unique method. Most frequently a technical task for the website development implies several methods. Each of these methods might have a different cost.
In this article, we would like to describe the procedure of adding a new shipping method to your online store.
Bear in mind that for this you will have to create your own plugin for WordPress.
H1 How to create a plugin for WordPress
Start with creating a new file that will look like this: [the name of the plugin].php. Then add this snippet of code into it:
/* Plugin Name: [the name of the plugin].php;
Plugin URI:
Description:
Version: 1.0.0
Author: WooThemes Author URI: */
Do not forget to change the meaning of the “name of the plugin” and “description of the plugin” to your own.
H2 How to create a new function and a class
To start the development process, you need to the following snippet of code in front of the «?>» in the previously created file:
function name_1() {
// Add a new class here
}
add_action( 'woocommerce_shipping_init', 'name_1' );
Remember to change «name_1» into the name of your own project (you are only allowed to use Latin symbols).
If you want to create a new class and place it inside the function you created earlier, then add this snippet of code. Add it before the following part «add_action( ‘woocommerce_shipp…»
if ( ! class_exists( 'Name_1' ) ) {
class name_of_the_class extends Name_1 {
public function __construct() {
$this->id
$this->title = __( 'Name' );
$this->method_description = __( 'Description' ); //
$this->enabled = "yes"; // Plugin is activated by
default, you can change it through the shipping method
$this->init();
}
function init() {
$this->init_form_fields();
$this->init_settings();
add_action( 'woocommerce_update_options_shipping_' . $this->id,
array( $this, 'process_admin_options' ) );
}
public function calculate_shipping( $package ) {
// code 1
}
}
}
Do not forget to change the default “Name_1”, “id_1”, “Name” and “Description to your own ones.
Next comes the time to inform WooCommerce about a new class with the aid of a new function:
function add_id_К( $methods ) {
$methods[] = 'Name_1'
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_id_1' );
The meaning of “Name_1” and “id_1” should be changed to your custom ones so that they match the ones mentioned previously.
Now, you need to specify the cost of the shipping, the code writer has to add this particular snippet of code instead of «// code 1»
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => 'cost',
'calc_tax' => 'per_item'
);
$this->add_rate( $rate );
Mention the cost the shipping. And in case the shipping is free, simply add 0.
Here is what this code with the shipping method will look like:
id = 'carters-com'; $this->method_title = __( 'Carterscom ); // Title shown in admin $this->method_description = __( 'Carterscom' ); $this->enabled = "yes"; $this->title = "Carterscom"; $this->init(); } function init() { $this->init_form_fields(); $this->init_settings(); add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); } public function calculate_shipping( $package ) { $rate = array( 'id' => $this->id, 'label' => $this->title, 'cost' => '0', 'calc_tax' => 'per_item' ); $this->add_rate( $rate ); } } } } add_action( 'woocommerce_shipping_init', 'carters_com' ); function add_carters_com( $methods ) { $methods[] = 'WC_Carters_Com'; return $methods; } add_filter( 'woocommerce_shipping_methods', 'add_carters_com' ); } ?>
Finally, now you can save the file and upload it to the server. Put them directly in the WordPress plugins folder. Remember to activate the plugin and check the WooCommerce settings to see whether the new shipping method was added.
We hope that this article was helpful to you! Should you have more questions, you can always contact us via the relevant form on the website or in the comments area. We will be happy to hear your feedback!
Author’s Bio:
Kevin is a professional educator and a private tutor with over 8 years of experience. He is also a content writer for various blogs about higher education, entertainment, social media & blogging. During his off time, Kevin enjoys traveling and cooking. Feel free to connect with him on Twitter, Linkedin & Google+.
WordPress Development | WordPress Theme Development | PSD To WordPress