Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register Shopware hook using the 5.2 plugin system

Tags:

shopware

I'm developing a Shopware plugin using the 5.2 plugin system and I'm having trouble registering a hook into a method of the sBasket module.

What I'm trying to achieve is to get notified when an article is removed from the shopping basket, to be able to remove other articles automatically (the articles are correlated and it doesn't make sense to have one in the basket if the other one is removed).

So far my plugin bootstrap looks as follows:

class MyTestPlugin extends \Shopware\Components\Plugin {

    public static function getSubscribedEvents() {

        return [
                'Shopware_Modules_Basket_AddArticle_Start' => 'onBasketAddArticle',
                'sBasket::sDeleteArticle::after' => 'onBasketDeleteArticle'
        ];
    }

    // Handler methods declared here...
}

The "AddArticle_Start" event registration works as expected, that is, the "onBasketAddArticle" handler is called when I add articles to the basket. The sBasket hook, however, isn't invoked.

I suspect that I'm mixing up legacy and new plugin system here and that hooks maybe have to be registered another way than events, but I can't find anything in the Shopware documentation.

Any help appreciated.

like image 864
Quagaar Avatar asked Dec 23 '22 17:12

Quagaar


2 Answers

At last, I found the solution. For anyone running into the same problem:

Shopware has a lot of caches and one of them is the proxy cache. Shopware creates proxy classes containing information about the registered hooks and those classes don't get rebuilt when updating a plugin.

So, if you add/remove/change hook registrations, you have to clear the proxy cache. In the backend go to Settings / Caches/Performace / Caches/Performance, select the checkbox "Proxies and meta data" and click the clear button.

like image 77
Quagaar Avatar answered Jan 30 '23 02:01

Quagaar


You can clean any cache in install/enable actions next way:

public function install(Shopware\Components\Plugin\Context\InstallContext\InstallContext $context){
    parent::install($context);
    // Only proxy cahe
    $context->scheduleClearCache( array( Shopware\Components\Plugin\Context\InstallContext\InstallContext::CACHE_TAG_PROXY ) );
}

public function activate(Shopware\Components\Plugin\Context\InstallContext\ActivateContext $context)
{
    // Clear all caches
    $context->scheduleClearCache( array( Shopware\Components\Plugin\Context\InstallContext\InstallContext::CACHE_LIST_ALL ) );
}
like image 43
Alexey Palamar Avatar answered Jan 30 '23 01:01

Alexey Palamar