Creating a Custom Plugin For Your WooCommerce Shop

,


The Plugin Skeleton

First need to check woocommerce is active, also check your class already exist in woocommerce. You must use unique class, so it's not conflict with old one

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

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_myclass' ) and easily provide translation files at some future date.

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

Here is the class functions


class WC_myclass {
 
  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_myclass'] = new WC_myclass();



Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.