Creating a Custom Plugin For Your WooCommerce Shop

WooCommerce is a ecommerce solution, but creating a website or store is a very personal endeavor, and you are likely to want to change some functionality or behavior of the stock WooCommerce plugin. Always people natural tendency is to look for the simple solutions, and therefore we can be drawn to editing the core files. Resist this base urge! Repeat after me: “I will not modify the core files.” Fortunately, though not as flexible as Magento, WooCommerce provides a lot of action hooks, filters, template files and “template functions” to allow for customizations. Writing a plugin allows us to cleanly separate and keep track of our custom code, and renders WooCommerce upgrades a relatively painless process.

What is an action?

An action in WordPress allows you to execute code triggered at a specific point in the page response process, for instance upon displaying a comment.

What is a filter?

Similar conceptually to an action, filters allow you to modify data at specific points in the WordPress page response process, for instance removing profanity from a comment.In a simple way it is return the value.

What is a template?

A template is a file that contains a mix of HTML and PHP code, and renders a page, a part of a page, or an email. Templates can be overridden by creating a file of the same name in a special location within your theme or child theme.

What is a template function?

A “template function” is a function that begins with if ( ! function_exists( ‘function_name’ ) {… If your plugin defines this function first it will be called in place of the default function and allow you to override its behavior with your own.

The Plugin Structure

I prefer to name my WooCommerce custom plugin woocommerce-company-name, so if your company is Acme, inc, you might use woocommerce-acme. Although not strictly necessary with a self-authored plugin, it’s good practice to check if WooCommerce is active, and also perform a check to ensure a class with the same name as your plugin doesn’t already exist:


if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
if ( ! class_exists( 'WC_Acme' ) ) {

Next, it’s again good practice though not necessary, to load any translated strings for the plugin. Doing so allows you to use the various translate functions such as __( ‘Some text’, ‘wc_acme’ ) and easily provide translation files at some future date.


load_plugin_textdomain( 'wc_acme', false, dirname( plugin_basename( __FILE__ ) ) . '/' );

I prefer to define the bulk of my plugin functions within a class, which effectively scopes the functions you write and keeps you from having to worry about function name clashes with all the other WordPress core and plugin functions. There are a few commonly used lifecycle action hooks which will be included in our skeleton plugin class. Finally, the plugin class will be instantiated, assuming that WooCommerce is active, and the class name isn’t already taken.


class WC_Acme {

public function __construct() {
// called just before the woocommerce template functions are included
add_action( ‘init’, array( $this, ‘include_template_functions’ ), 20 );

// called only after woocommerce has finished loading
add_action( ‘woocommerce_init’, array( $this, ‘woocommerce_loaded’ ) );

// called after all plugins have loaded
add_action( ‘plugins_loaded’, array( $this, ‘plugins_loaded’ ) );

// indicates we are running the admin
if ( is_admin() ) {
// …
}

// indicates we are being served over ssl
if ( is_ssl() ) {
// …
}

// take care of anything else that needs to be done immediately upon plugin instantiation, here in the constructor
}

/**
* Override any of the template functions from woocommerce/woocommerce-template.php
* with our own template functions file
*/
public function include_template_functions() {
include( ‘woocommerce-template.php’ );
}

/**
* Take care of anything that needs woocommerce to be loaded.
* For instance, if you need access to the $woocommerce global
*/
public function woocommerce_loaded() {
// …
}

/**
* Take care of anything that needs all plugins to be loaded
*/
public function plugins_loaded() {
// …
}
}

// finally instantiate our plugin class and add it to the set of globals

$GLOBALS[‘wc_acme’] = new WC_Acme();

Then the plugin is work. In simple description you want to create the folder name like “woocommerce-acme” inside the folder create the file “woocommerce-acme.php” and enter the above code and also create the template page for showing data as “woocommerce-template.php”.

3 Comments

  1. best hemroid treatment

    I read this article fully about the comparison of latest and preceding technologies, it’s amazing article.

    Reply
  2. vanessa

    Hi,

    I have added the woocommerce beanstream plugin into my website vancityva.com. It works well except where you entire your name, credit card number and cvs number there isn’t a text area. It’s just radio buttons. Can I fix this? I feel it will confuse my clients. Let me know. Thanks!

    Reply
    1. Alok Tiwari (Post author)

      Yes you can fix it go to the plugin folder there is a css and code you can change it accordingly.

      Reply

Leave a Comment

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