Blueness Store
shopping_cart
Sign In

Extending The Plugin

v1.0.1

Last updated 1 month ago

Developer Guide: Extensibility & Add-ons (Pro)

WooCommerce Order Tracker Pro is designed with an "Add-on Architecture" that allows developers to extend the plugin without modifying the core files.


🏗️ Registering New Notification Channels

The plugin supports Email, SMS, and WhatsApp by default. You can add entirely new channels (e.g., Slack, Push) using the bot_active_channels filter.

php
add_filter('bot_active_channels', function($channels) {
    $channels[] = 'slack';
    return $channels;
});

When you add a new channel:

  1. A new tab will automatically appear in the Notifications settings page.

  2. A new notification toggle will appear in the Tracking Stages manager.

  3. You must handle the rendering of the settings tab using do_action("bot_render_notification_tab_{$active_tab}").


📱 Adding New SMS/WhatsApp Providers

If you want to use a provider other than Africa's Talking or Twilio, you can register it using the provider filters.

Example: Adding a custom SMS Provider

  1. Register the Provider:

php
add_filter('bot_sms_providers', function($providers) {
    $providers['my_custom_gateway'] = 'My Custom Gateway';
    return $providers;
});
  1. Render Configuration Fields: Use the bot_render_sms_provider_fields action to inject your API key fields into the settings page.

php
add_action('bot_render_sms_provider_fields', function($settings) {
    ?>
    <tr data-provider="my_custom_gateway">
        <th>API Key</th>
        <td><input type="text" name="bot_notifications_sms[my_api_key]" value="<?php echo esc_attr($settings['my_api_key'] ?? ''); ?>"></td>
    </tr>
    <?php
});
  1. Implement the Notifier: Create a class that implements Blueness\WoocommerceOrderTracker\Notifications\Contracts\Notifier and register it via bot_notifier_factory_make.

🛠️ Factories and Custom Logic

The plugin uses factory patterns to create Notifiers and Notifications. You can intercept these to inject your own classes.

Custom Notifier Logic

php
add_filter('bot_notifier_factory_make', function($notifier, $channel) {
    if ($channel === 'sms' && get_option('bot_notifications_sms')['provider'] === 'my_custom_gateway') {
        return new MyCustomSmsNotifier();
    }
    return $notifier;
}, 10, 2);

Custom Notification Template Logic

php
add_filter('bot_notification_factory_make', function($notification, $channel, $order, $stage) {
    if ($channel === 'email' && $stage['id'] === 'shipped') {
        return new MyCustomShippedNotification($order, $stage);
    }
    return $notification;
}, 10, 4);

🚀 Implementation Examples

1. Creating a Custom Notification Class

A Notification class is responsible for preparing the content (Recipient, Subject, and Body).

php
use Blueness\WoocommerceOrderTracker\Notifications\Contracts\Notification;

class MyCustomShippedNotification implements Notification {
    protected $order;
    protected $stage;

    public function __construct($order, array $stage) {
        $this->order = $order;
        $this->stage = $stage;
    }

    public function getRecipient(): string {
        return $this->order->get_billing_email();
    }

    public function getSubject(): string {
        return "Great news! Your order #" . $this->order->get_id() . " is on its way!";
    }

    public function getBody(): string {
        return "Hello " . $this->order->get_billing_first_name() . ",\n\n" .
               "Your order is now " . $this->stage['name'] . ".";
    }
}

2. Creating a Custom Notifier Class

A Notifier class is responsible for the delivery of the notification (e.g., calling an external API).

php
use Blueness\WoocommerceOrderTracker\Notifications\Contracts\Notifier;

class MyCustomSmsNotifier implements Notifier {
    protected $to;
    protected $subject;
    protected $body;

    public function setTo(string $recipient): self {
        $this->to = $recipient;
        return $this;
    }

    public function setSubject(string $subject): self {
        $this->subject = $subject;
        return $this;
    }

    public function setBody(string $body): self {
        $this->body = $body;
        return $this;
    }

    public function send(): bool {
        // Example: Call your custom SMS Gateway API
        /*
        $response = wp_remote_post('https://api.mygateway.com/send', [
            'body' => [
                'to'      => $this->to,
                'message' => $this->body,
                'key'     => get_option('wot_notifications_sms')['my_api_key']
            ]
        ]);
        return !is_wp_error($response);
        */
        return true;
    }
}

List of Key Filters

Filter

Description

bot_active_channels

Define which notification channels are available.

bot_sms_providers

Register new SMS gateways.

bot_whatsapp_providers

Register new WhatsApp Business API providers.

bot_notification_tabs

Customize the tabs on the notification settings page.

bot_default_templates

Modify the default message templates for new stages.

bot_notification_factory_make

Override the notification object creation.

bot_notifier_factory_make

Override the notifier object creation.