How to Create a WooCommerce Plugin from Scratch

WooCommerce has gained huge popularity in the world of eCommerce due to its simple to use interface. WooCommerce is equably competing with Magento and Shopify for a full fledged eCommerce website. Now even you can get complete B2B features with WooCommerce. In order to customize WooCommerce, it’s recommended to create a WordPress plugin that extends WooCommerce with the required features. In this article we will follow the steps of creating a simple WordPress plugin for WooCommerce from scratch.

Custom Database or Custom Post Types?

It completely depends on the features the plugin will add. If your Pages, Posts like format supports your plugin then you can create Custom Post Types to showcase your custom data. In case you have more complex data types and relationships then it is better to go for creating custom databases. The major difference is that CPT or Custom Post Types will automatically be included in all the global functions of WordPress through which you can get, set or edit the post data. Also, the post data can be included in the REST APIs as well. On the other hand using custom databases, every function has to be created from scratch, including custom APIs.

Hooks: Actions and Filters

The backbone of any WordPress plugin is Hooks. Using the actions and filters you can extend anything to infinity.

Actions: To be used in order to fire a function when something happens in WordPress

Filters: To be used to filter data

We shall discuss some important hooks while developing the plugin.

Lets get started in creating a WooCommerce plugin that lets you provide a percentage discount to all customers on special events.

b2b woocommerce

Step 1: Creating the plugin file

Create a folder named Discounts and inside the folder, create a file Discounts.php. Jut add the following details to make it act as a plugin.

* Plugin Name: Discounts  
* Plugin URI:  
* Description: A simple discounts plugin   
* Author: InSync   
* Version: 1.0.0   
* Author URI:   
* License: GPL3+   
* Text Domain:   
* Domain Path: /languages/   

The most important things are Plugin Name, Description and

Step 2: Check WooCommerce and Create your Plugin Class

In the same file after the above comments add

if ( in_array( 'woocommerce/woocommerce.php', get_option('active_plugins'))) {

    if ( ! class_exists('WC_Discounts_Woo')){

class WC_Discounts_Woo{

            public function __construct(){

Here the first line checks if WooComerce has been installed. Then it checks if a class exists then creates that class. We will be putting all our codes within our own class so that they do not conflict with other plugins.

Step 3: Add Discount field in users profile section

In order to assign different types of discounts to customers , you need a field to specify discount rates for each customer. The edit_user_profile & edit_user_profile_update helps to display and save custom profile values respectively. Every hook has an associated callback to fire after the action or filter takes place.

Within the constructor of your class write

public function __construct(){

add_action('edit_user_profile', array( $this, 'show_user_discount'), 10, 1);
add_action( 'edit_user_profile_update', array( $this, 'save_user_discount'), 10, 1 );


//Within the class body

//The 'get_user_meta' helps to get the save metadata for the logged in customer.

public function show_user_discount($user) {
<table class="form-table">
<tr id="show_user_discount">

       <th style="color:#0073AA"<Discount</th>
       <input type="number" id="show_user_discount" name="show_user_discount" min="0" max="100" value="<?php echo get_user_meta($user->ID, 'show_user_discount', true)  ?>">

 public function save_user_type_b2bwoo($user) {

        if ( ! current_user_can( 'edit_user') ) {

            return false;


        if(isset($_POST ['show_user_discount'])) {

            $show_user_discount = $_POST['show_user_discount'];

            update_user_meta($user, 'show_user_discount', $show_user_discount);



Step 4. Apply the respective discount on the products

The woocommerce_product_get_price filter helps to change the product display prices.
//Add within the class constructor

 add_filter( 'woocommerce_product_get_price', array( $this, 'show_discount_price' ), 10, 2);

//Add within the class body
 public function show_discount_price($price, $product) {

 // $price = floatval($price);
 $current_user_id = get_current_user_id();
 $discount = floatval(get_user_meta ($current_user_id, 'show_user_discount', true));

if(!empty($discount)) {
$final_price = $price - (($price*$discount)/100);
return $final_price;

Thus you can add a global discount to all products for specific customers.
We have touched only the tip of the iceberg for creating a WooCommerce plugin. In our next article, we will cover more advanced techniques for WooCommerce plugin development.


On Key

Related Posts

We use cookies to give you a better website experience. by using INSYNC website, you agree to our Cookie Policy.