Zyfella
Zyfella

Reputation: 75

event priority in shopware 5

i have 2 plugins both use same event Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout in plugin A i assing a variable to smarty and need that variable for my other plugin. but plugin B runs before plugin A. so i set priorities and tested it with a var_dump();

Plugin A:

 public static function getSubscribedEvents()
{
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => array('onCheckout', 1)
    ];
}
public function onPostDispatchCheckout(\Enlight_Event_EventArgs $args)
    {
        var_dump("Plugin A");
    }

Plugin B:

public static function getSubscribedEvents()
{
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => array('onCheckout', 2)
    ];
}
    public function onPostDispatchCheckout(\Enlight_Event_EventArgs $args)
    {
        var_dump("Plugin B");
    }

now when I run it, the output is:

plugin B, Plugin A

but plugin A must run first, what am I doing wrong ? Thanks

Upvotes: 0

Views: 621

Answers (2)

dnaumann
dnaumann

Reputation: 474

As Pawel mentioned you can set the priority in the event subscription. There are two syntaxes to do this

event => [subscriber => priority]

'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => ['onCheckout' => 1]

or

event => [subscriber, priority]

'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => ['onCheckout', 1]

As far as I know its like z-index, the higher one wins. And you might need to reinstall the plugin and clear the cache.

Upvotes: 2

Paweł Napierała
Paweł Napierała

Reputation: 1755

The higher the number, the earlier a subscriber is executed. So you have to change the priorities.

Plugin A:

 public static function getSubscribedEvents()
{
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => array('onCheckout', 2)
    ];
}
public function onPostDispatchCheckout(\Enlight_Event_EventArgs $args)
    {
        var_dump("Plugin A");
    }

Plugin B:

public static function getSubscribedEvents()
{
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => array('onCheckout', 1)
    ];
}
    public function onPostDispatchCheckout(\Enlight_Event_EventArgs $args)
    {
        var_dump("Plugin B");
    }

Upvotes: 2

Related Questions