Change basket data in basket plugin

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
heural
Posts: 58
Joined: 09 Jun 2022, 07:55

Change basket data in basket plugin

Post by heural » 14 Aug 2023, 09:13

Hi Aimeos-Team,

we change some custom data at the "setOrder.before"-event to the basket. That works fine so far, but for example we have paypal as the payment provider and we cancel the payment (backlink to our website ) the order is also canceled in aimeos - new order-(base)-id etc. is created if we checkout again.
But now the custom data from the previous checkout try is missing. It seems the data is not persited in the "session basket", it works only for the "save-the-order-to-database"-way. How can I change/add the custom data in this event to the "session basket" also?

Code: Select all


 public function update( \Aimeos\MW\Observer\Publisher\Iface $basket, $event, $value = null )
    {

        if ($event == 'setOrder.before') {

		$external_userid = <user-has-external-user-id>

                foreach($basket->getProducts() as $pos => $product) {


                            $product->setProjectUser($external_userid);
                            $basket->deleteProduct($pos);
                            $basket->addProduct($product);
                        }

        }

Thank you!
Ralf

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

Re: Change basket data in basket plugin

Post by aimeos » 16 Aug 2023, 13:10

That's true, the "setOrder.before" event is only published in the store() method of the basket frontend controller:
https://github.com/aimeos/ai-controller ... d.php#L289

Thus, the changes are not persisted into the session but only in the database.

There are two options now:
1.) Use another event like "addProduct.before/setProducts.before" to change the product when they are added
2.) Call save() of the basket controller in the HTML client to store the basket in the session too:
https://github.com/aimeos/ai-client-htm ... d.php#L211
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

heural
Posts: 58
Joined: 09 Jun 2022, 07:55

Re: Change basket data in basket plugin

Post by heural » 17 Aug 2023, 14:12

Thank you!

I have another solution found, getting the session-basket and and set the data there:

Code: Select all

  $manager = \Aimeos\MShop::create( $context, 'order/base' );
  $sessionBasket = $manager->getSession( 'default' );
  
  foreach($sessionBasket->getProducts() as $pos => $product) {

                            $product->setProjectUser($external_userid);
                            $sessionBasket->deleteProduct($pos);
                            $sessionBasket->addProduct($product);
                        }

Post Reply