Page 1 of 1

"setLocale.after" event not working for my plugin

Posted: 21 Sep 2022, 13:07
by РоманАндрейчук
Aimeos 2021.x, php7.4

Hello. I'm create new plugin for basket. It changes the amount according to the client's logic.

Code: Select all

class TenthFree
    extends \Aimeos\MShop\Plugin\Provider\Factory\Base
    implements \Aimeos\MShop\Plugin\Provider\Iface, \Aimeos\MShop\Plugin\Provider\Factory\Iface
{
	 public function register( \Aimeos\MW\Observer\Publisher\Iface $p ) : \Aimeos\MW\Observer\Listener\Iface
    {
        $plugin = $this->getObject();

        $p->attach( $plugin, 'addProduct.after' );
        $p->attach( $plugin, 'deleteProduct.after' );
        $p->attach( $plugin, 'setProducts.after' );
        $p->attach( $plugin, 'addCoupon.after' );
        $p->attach( $plugin, 'deleteCoupon.after' );
        $p->attach( $plugin, 'setOrder.before' );
        $p->attach( $plugin, 'setLocale.before' );
        $p->attach( $plugin, 'setLocale.after' );
        return $this;
    }
    
    ......
    
 }
The plugin fires in all necessary events. But if I change the language or currency, the cart amount is not recalculated. Can you help me figure it out?

Re: "setLocale.after" event not working for my plugin

Posted: 22 Sep 2022, 15:23
by aimeos
If you change the language or currency, the basket is migrated in the basket frontend controller:
https://github.com/aimeos/ai-controller ... #L124-L162

In that case, setLocale() of the basket is never used because the basket object is created with a new locale object passed to its constructor directly:
https://github.com/aimeos/aimeos-core/b ... hp#L42-L49

Not sure if we can simply call Basket::setLocale() at the end of the migration:
https://github.com/aimeos/ai-controller ... #L154-L156

Can you test if that has negative consequences elsewhere?

Re: "setLocale.after" event not working for my plugin

Posted: 24 Sep 2022, 16:24
by РоманАндрейчук
Thank you! This method works. Can you add this code to your repository?

Found some more problems:
1)Sometimes a variable is not stored in the session. Because of this, there is an unnecessary initialization of the basket object.
https://github.com/aimeos/ai-controller ... e.php#L161

2) https://github.com/aimeos/ai-controller ... d.php#L514
This method does not check for the uniqueness of the added service. And many same delivery or payment services can be added to the basket object.

Re: "setLocale.after" event not working for my plugin

Posted: 27 Sep 2022, 07:33
by aimeos
РоманАндрейчук wrote: 24 Sep 2022, 16:24 Thank you! This method works. Can you add this code to your repository?
Thanks, the setLocale method is no called in dev-master and 2022.07.x-dev versions.
РоманАндрейчук wrote: 24 Sep 2022, 16:24 1)Sometimes a variable is not stored in the session. Because of this, there is an unnecessary initialization of the basket object.
https://github.com/aimeos/ai-controller ... e.php#L161
Can you find out why the variable isn't stored?
РоманАндрейчук wrote: 24 Sep 2022, 16:24 2) https://github.com/aimeos/ai-controller ... d.php#L514
This method does not check for the uniqueness of the added service. And many same delivery or payment services can be added to the basket object.
That's true. We've added checks in addService() and copyServices() now to avoid duplicate services. The new code is also available in dev-master and 2022.07.x-dev.

Re: "setLocale.after" event not working for my plugin

Posted: 27 Sep 2022, 10:08
by РоманАндрейчук
Is version 2021.x no longer supported?

Re: "setLocale.after" event not working for my plugin

Posted: 28 Sep 2022, 11:12
by aimeos
We are hesitating a bit porting back bigger changes to LTS versions.
But we've made the changes also available in aimeos/ai-controller-frontend:2021.10.x-dev now.