Adding custom events

To create a new event, you have to do two things:

  1. Create a new class that implements \WPDesk\ShopMagic\Event\Event interface.
  2. Inform the ShopMagic that a new event is available.

Creating a new event class

As interface \WPDesk\ShopMagic\Event\Event requires some more in-depth knowledge about Event\<->Automation\<->Filter interactions, we've created some intermediary abstracts to help you with the most common uses.

An abstract class already implements most of the required methods.

  • For events that are linked to order data, we've prepared \WPDesk\ShopMagic\Event\OrderCommonEvent.
  • For events that are linked to user, we've prepared \WPDesk\ShopMagic\Event\UserCommonEvent.
  • For events that are linked to some new kind of data, you can extend \WPDesk\ShopMagic\Event\BasicEvent.

Creating a new event that is linked to order

To facilitate the creation of an order-linked event, it's best to extend \WPDesk\ShopMagic\Event\OrderCommonEvent class and override two primary methods: get_name and initialize.

The get_name method defines the name for the event that would be visible in Automations in admin panel. The initialize should hook to the WordPress action system and execute run_action method after event fires. The order that event hook should receive from WooCommerce should be assigned to order property.

Example

A event that is fired when WooCommerce item is added to order in admin panel can has initialize method defined as:

public function initialize() {
    add_action( 'woocommerce_ajax_order_items_added', function( $items, $order ) {
        $this->order = $order;
        $this->run_actions();
    }, 10, 2);
}

Informing the ShopMagic plugin about a new event

To integrate a newly created event class, you should use a filter shopmagic/core/events and add a new class to the event hashmap.

Example

Let's create a new event with a name Invoice Resend that is fired after invoice was sent.

namespace YourNamespace;

class InvoiceAfterResendEvent extends \WPDesk\ShopMagic\Event\OrderCommonEvent {

    public function get_name() {
        return __( 'Invoice Resend', 'my-text-domain' );
    }

    public function initialize() {
        add_action( 'woocommerce_after_resend_order_email', function( $order ) {
            $this->order = $order;
            $this->run_actions();
        });
    }
}

Then we need to inform the ShopMagic event factory using the shopmagic/core/events filter.

/**
 * @param \WPDesk\ShopMagic\Event\Event[] $hashmap Hashmap with built in events.
 *
 * @return \WPDesk\ShopMagic\Event\Event[] Hashmap with appended event.
 */

function integrate_custom_shopmagic_event( array $hashmap ) {
    require_once(__DIR__ . '/path/to/event/class/if/no/autoloading/used.php');

    $hashmap['invoice_resend_evest'] = new \YourNamespace\InvoiceAfterResendEvent();
    return $hashmap;
}

add_filter('shopmagic/core/events', 'integrate_custom_shopmagic_event');
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us