Here’s our comprehensive guide to developing a WooCommerce plugin from scratch, covering everything from planning to advanced integrations. We’ll structure it step-by-step so you can use it as a full reference.


1. Planning Your Plugin

Before coding, clarify:

  • Purpose: What the plugin will do (e.g., Amazon order import, payment gateway, subscription manager).
  • Target audience: Who will use it (store owners, developers).
  • Features: List all features, e.g.:

    • Admin settings page
    • Cron jobs
    • REST API integrations
    • Custom product/order meta fields
  • Compatibility: WooCommerce version, WordPress version, PHP version.
  • Architecture: Consider modularity, classes, hooks, and templates.

2. Plugin File Structure

WooCommerce plugins generally follow this structure:

my-woocommerce-plugin/
│
├── my-woocommerce-plugin.php  <-- main plugin file
├── readme.txt
├── uninstall.php
├── assets/
│   ├── js/
│   ├── css/
│   └── images/
├── includes/
│   ├── class-admin-settings.php
│   ├── class-cron-jobs.php
│   ├── class-api-integration.php
│   └── functions.php
├── templates/
│   ├── email/
│   └── frontend/
└── languages/
    └── my-woocommerce-plugin.pot

Notes:

  • includes/ → all core classes and helper functions.
  • templates/ → override WooCommerce templates for custom output.
  • languages/ → for translations.

3. Main Plugin File

The main file includes metadata and bootstraps your plugin.

<?php
/**
 * Plugin Name: My WooCommerce Plugin
 * Plugin URI: https://example.com
 * Description: Custom WooCommerce plugin for XYZ functionality.
 * Version: 1.0.0
 * Author: MAMEDUL ISLAM
 * Author URI: https://example.com
 * Text Domain: my-woocommerce-plugin
 * Domain Path: /languages
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

// Define constants
define('MWP_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('MWP_PLUGIN_URL', plugin_dir_url(__FILE__));
define('MWP_VERSION', '1.0.0');

// Include dependencies
require_once MWP_PLUGIN_DIR . 'includes/functions.php';
require_once MWP_PLUGIN_DIR . 'includes/class-admin-settings.php';
require_once MWP_PLUGIN_DIR . 'includes/class-cron-jobs.php';
require_once MWP_PLUGIN_DIR . 'includes/class-api-integration.php';

// Initialize plugin
add_action('plugins_loaded', 'mwp_init_plugin');
function mwp_init_plugin() {
    if ( class_exists('WooCommerce') ) {
        MWP_Admin_Settings::init();
        MWP_Cron_Jobs::init();
        MWP_API_Integration::init();
    }
}

4. Admin Settings Page

Most plugins require a settings page in WooCommerce.

<?php
class MWP_Admin_Settings {

    public static function init() {
        add_action('admin_menu', [__CLASS__, 'add_menu']);
        add_action('admin_init', [__CLASS__, 'register_settings']);
    }

    public static function add_menu() {
        add_submenu_page(
            'woocommerce',
            'My Plugin Settings',
            'My Plugin',
            'manage_options',
            'mwp-settings',
            [__CLASS__, 'settings_page_html']
        );
    }

    public static function register_settings() {
        register_setting('mwp_settings_group', 'mwp_api_key');
        register_setting('mwp_settings_group', 'mwp_option_enable');
    }

    public static function settings_page_html() {
        ?>
        <div class="wrap">
            <h1>My Plugin Settings</h1>
            <form method="post" action="options.php">
                <?php settings_fields('mwp_settings_group'); ?>
                <?php do_settings_sections('mwp_settings_group'); ?>
                <table class="form-table">
                    <tr>
                        <th scope="row">API Key</th>
                        <td><input type="text" name="mwp_api_key" value="<?php echo esc_attr(get_option('mwp_api_key')); ?>" class="regular-text"></td>
                    </tr>
                    <tr>
                        <th scope="row">Enable Feature</th>
                        <td><input type="checkbox" name="mwp_option_enable" value="yes" <?php checked(get_option('mwp_option_enable'), 'yes'); ?>></td>
                    </tr>
                </table>
                <?php submit_button(); ?>
            </form>
        </div>
        <?php
    }
}

5. WooCommerce Hooks & Filters

WooCommerce uses hooks to extend functionality:

  • Actions: add_action('woocommerce_thankyou', 'function_name');
  • Filters: add_filter('woocommerce_cart_item_price', 'function_name');

Example: Adding a custom field to checkout:

add_action('woocommerce_after_order_notes', 'mwp_custom_checkout_field');
function mwp_custom_checkout_field($checkout) {
    echo '<div id="mwp_custom_field"><h2>Custom Info</h2>';
    woocommerce_form_field('mwp_field', array(
        'type' => 'text',
        'class' => array('mwp-field form-row-wide'),
        'label' => __('Enter something'),
        'required' => true,
    ), $checkout->get_value('mwp_field'));
    echo '</div>';
}

add_action('woocommerce_checkout_update_order_meta', 'mwp_save_custom_field');
function mwp_save_custom_field($order_id) {
    if (!empty($_POST['mwp_field'])) {
        update_post_meta($order_id, 'mwp_field', sanitize_text_field($_POST['mwp_field']));
    }
}

6. REST API Integration

To fetch or send data to an external service:

class MWP_API_Integration {

    public static function init() {
        add_action('init', [__CLASS__, 'fetch_data']);
    }

    public static function fetch_data() {
        $api_url = 'https://api.example.com/orders';
        $response = wp_remote_get($api_url, [
            'headers' => [
                'Authorization' => 'Bearer ' . get_option('mwp_api_key')
            ]
        ]);

        if (is_wp_error($response)) return;

        $data = json_decode(wp_remote_retrieve_body($response), true);

        foreach ($data as $order) {
            // Create WooCommerce orders programmatically
            $new_order = wc_create_order();
            $new_order->add_product(wc_get_product($order['product_id']), $order['quantity']);
            $new_order->set_address($order['billing'], 'billing');
            $new_order->calculate_totals();
        }
    }
}

7. Cron Jobs for Automation

WooCommerce plugins often need scheduled tasks:

class MWP_Cron_Jobs {

    public static function init() {
        add_action('mwp_hourly_event', [__CLASS__, 'run_task']);
        if (!wp_next_scheduled('mwp_hourly_event')) {
            wp_schedule_event(time(), 'hourly', 'mwp_hourly_event');
        }
    }

    public static function run_task() {
        // e.g., Sync orders or send emails
    }

    public static function deactivate() {
        $timestamp = wp_next_scheduled('mwp_hourly_event');
        wp_unschedule_event($timestamp, 'mwp_hourly_event');
    }
}

// On plugin deactivation
register_deactivation_hook(__FILE__, ['MWP_Cron_Jobs', 'deactivate']);

8. WooCommerce Order & Product Meta

You can store extra data per order or product:

// Adding meta to product
update_post_meta($product_id, '_custom_meta_key', 'value');

// Retrieving meta
$value = get_post_meta($product_id, '_custom_meta_key', true);

9. Custom Email Notifications

WooCommerce allows custom email classes:

// Extend WC_Email
class WC_Email_Custom_Notification extends WC_Email {
    public function __construct() {
        $this->id = 'custom_notification';
        $this->title = 'Custom Notification';
        $this->description = 'Send a custom email to customer';
        $this->template_html = 'emails/custom-email.php';
        parent::__construct();
    }

    public function trigger($order_id) {
        $this->object = wc_get_order($order_id);
        $this->recipient = $this->object->get_billing_email();
        if (!$this->is_enabled() || !$this->get_recipient()) return;
        $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
    }
}

10. Best Practices

  • Sanitize and validate all inputs.
  • Use wp_nonce for form security.
  • Follow WooCommerce coding standards.
  • Separate logic into classes and functions.
  • Use WordPress Settings API for admin options.
  • Translation ready: Use __() and _e().
  • Avoid direct queries; use WooCommerce/WP functions.
  • Use hooks instead of editing core files.

11. Testing & Deployment

  • Test on a staging environment.
  • Use WP_DEBUG and logging.
  • Test WooCommerce compatibility with popular themes/plugins.
  • Package plugin with:

    • readme.txt
    • languages/
    • Assets and includes
  • Deploy to WordPress repository or privately via zip upload.

12. Advanced Features (Optional)

  • Amazon or eBay Integration using official APIs.
  • Payment Gateway Integration via WooCommerce Gateway class.
  • Custom Reports with WooCommerce Reports API.
  • PWA/Offline features for mobile users.
  • React/JS frontend for dynamic admin panels.

This guide gives a full blueprint to build almost any WooCommerce plugin, from simple options to complex API-driven automation.


Need a Custom WooCommerce Solution?

Building a high-performance WooCommerce plugin requires precision, security, and a deep understanding of the WordPress ecosystem. At NeedleCode, we transform complex business requirements into seamless, scalable plugins.

Why Choose NeedleCode for Your Plugin?

Ready to build something extraordinary? Talk to Our Expert Developers

Precision engineering for the future of eCommerce.