"setLocale.after" event not working for my plugin

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
User avatar
РоманАндрейчук
Posts: 15
Joined: 13 Jan 2022, 15:08

"setLocale.after" event not working for my plugin

Post by РоманАндрейчук » 21 Sep 2022, 13:07

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?

User avatar
aimeos
Administrator
Posts: 6739
Joined: 01 Jan 1970, 00:00

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

Post by aimeos » 22 Sep 2022, 15:23

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?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
РоманАндрейчук
Posts: 15
Joined: 13 Jan 2022, 15:08

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

Post by РоманАндрейчук » 24 Sep 2022, 16:24

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.

User avatar
aimeos
Administrator
Posts: 6739
Joined: 01 Jan 1970, 00:00

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

Post by aimeos » 27 Sep 2022, 07:33

РоманАндрейчук 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.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
РоманАндрейчук
Posts: 15
Joined: 13 Jan 2022, 15:08

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

Post by РоманАндрейчук » 27 Sep 2022, 10:08

Is version 2021.x no longer supported?

User avatar
aimeos
Administrator
Posts: 6739
Joined: 01 Jan 1970, 00:00

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

Post by aimeos » 28 Sep 2022, 11:12

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.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply