I plan to place collectively a mini-series overlaying the fundamentals of correctly planning, engineering, launching and sustaining a WooCommerce store. Sooner or later as I write further articles I’ll hyperlink them collectively, for now, this primary article will cowl the fundamentals of making a customized plugin in your WooCommerce website, and give you a plugin skeleton to obtain and use as a place to begin. This text assumes a familiarity with fundamental WordPress plugin growth, and expands upon the WooCommerce doc article on making a plugin. So, let’s get going!
Why a Customized Plugin?
WooCommerce is a wonderful ecommerce resolution, however creating an internet site or store is a really private endeavor, and you might be prone to need to change some performance or conduct of the inventory WooCommerce plugin. Our pure tendency is to look for easy options, and subsequently we might be drawn to enhancing the core information. Resist this base urge! Repeat after me: “I cannot modify the core information.” Thankfully, although not as versatile as Magento, WooCommerce gives a number of motion hooks, filters, template information, and “template features” to permit for customizations. Writing a plugin permits us to cleanly separate and preserve monitor of our customized code, and renders WooCommerce upgrades a comparatively painless course of.
-
What’s an motion?
An motion in WordPress means that you can execute code triggered at a particular level within the web page response course of, for example upon displaying a remark.
-
What’s a filter?
Related conceptually to an motion, filters let you modify knowledge at particular factors within the WordPress web page response course of, for example eradicating profanity from a remark.
-
What’s a template?
A template is a file that incorporates a mixture of HTML and PHP code and renders a web page, part of a web page, or an electronic mail. Templates might be overridden by making a file of the identical title in a particular location inside your theme or youngster theme.
-
What’s a template perform?
A “template perform” is a perform that begins with
if ( ! function_exists( 'function_name' ) {
… In case your plugin defines this perform first it is going to be referred to as instead of the default perform and let you override its conduct with your personal.
Writing a customized plugin largely means that you can alter the performance and conduct of WooCommerce; to customise the appear and feel the popular technique is to create a customized youngster theme. I gained’t cowl that course of on this article sequence as it’s already described effectively within the WordPress codex and there isn’t a lot WooCommerce-specific so as to add. The exception is probably the best way that you simply override WooCommerce template information, which I wrote about in one other article.
The Plugin Skeleton
I want to call my WooCommerce customized plugin woocommerce-company-name, so if your organization is Acme, inc, you may use woocommerce-acme. Though not strictly obligatory with a self-authored plugin, it’s good apply to verify if WooCommerce is lively, and likewise carry out a verify to make sure a category with the identical title as your plugin doesn’t exist already:
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { if ( ! class_exists( 'WC_Acme' ) ) {
Subsequent, it’s once more good apply although not obligatory, to load any translated strings for the plugin. Doing so means that you can use the assorted translate features resembling __( 'Some textual content', 'wc_acme' )
and simply present translation information at some future date.
load_plugin_textdomain( 'wc_acme', false, dirname( plugin_basename( __FILE__ ) ) . '/' );
I want to outline the majority of my plugin features inside a category, which successfully scopes the features you write and retains you from having to fret about perform title clashes with all the opposite WordPress core and plugin features. There are a couple of generally used lifecycle motion hooks which can be included in our skeleton plugin class. Lastly, the plugin class can be instantiated, assuming that WooCommerce is lively, and the category title isn’t already taken.
class WC_Acme { public perform __construct() { // referred to as simply earlier than the woocommerce template features are included add_action( 'init', array( $this, 'include_template_functions' ), 20 ); // referred to as solely after woocommerce has completed loading add_action( 'woocommerce_init', array( $this, 'woocommerce_loaded' ) ); // referred to as in spite of everything plugins have loaded add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); // signifies we're operating the admin if ( is_admin() ) { // ... } // signifies we're being served over ssl if ( is_ssl() ) { // ... } // deal with anything that must be executed instantly upon plugin instantiation, right here within the constructor } /** * Override any of the template features from woocommerce/woocommerce-template.php * with our personal template features file */ public perform include_template_functions() { embody( 'woocommerce-template.php' ); } /** * Deal with something that wants woocommerce to be loaded. * As an example, should you want entry to the $woocommerce world */ public perform woocommerce_loaded() { // ... } /** * Deal with something that wants all plugins to be loaded */ public perform plugins_loaded() { // ... } } // lastly instantiate our plugin class and add it to the set of globals $GLOBALS['wc_acme'] = new WC_Acme();
Wrapping Up
If you happen to have been already accustomed to WordPress/WooCommerce growth there most likely wasn’t a lot new for you right here, however hopefully should you’re new to the sport then the above explanations and strategies will show useful in getting you began. Both approach, one of the simplest ways to study the multitude of WooCommerce actions and filters obtainable for personalisation is to browse the supply code. Additionally keep in mind that along with hooking into actions/filters and including performance, you possibly can simply as simply unhook current actions/filters to take away plugin conduct, or change it wholesale.
The trick is to carry out the remove_action()
/remove_filter()
calls after the goal motion is hooked to (for example inside our woocommerce_loaded()
perform above), and to at all times keep in mind to supply all of the arguments to the take away features that have been handed within the add features, non-compulsory parameters and all.