How to automatically save baskets in 2022.10

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!
columbo
Advanced
Posts: 126
Joined: 09 Oct 2019, 09:42

How to automatically save baskets in 2022.10

Post by columbo » 04 Jan 2023, 07:18

Hi,

we are using Aimeos as a B2B shop, where all customers have to register / login first. (So there are no anonymous baskets)

We are looking for a solution to automatically saves customer baskets during their visits:
  1. customer login
  2. customer adds items to basket
  3. customer logs out and (some days later) logs in again
  4. previously added items (from step 2) should still be present in the basket
With Aimeos 2022.10 customers can save their baskets by entering a basket name.
Is it possible to extend this function so that baskets are automatically stored? eg. using the customer-Id as basket name?
How would you suggest an implementation?

Thank you!

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

Re: How to automatically save baskets in 2022.10

Post by aimeos » 05 Jan 2023, 08:17

There are two solutions for that:

1.) The easiest one is to extend the PHP session lifetime to 30, 90 or more days but this only works if the customer doesn't log out

2.) You can create a Basket Plugin that stores the basket to the mshop_order_basket table whenever the basket is updated using it's manager and repopulate the basket at login:
- https://aimeos.org/docs/latest/provider ... t-plugins/
- https://github.com/aimeos/aimeos/blob/m ... roller.php
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

columbo
Advanced
Posts: 126
Joined: 09 Oct 2019, 09:42

Re: How to automatically save baskets in 2022.10

Post by columbo » 10 Jan 2023, 09:03

Thank you. I made a few tests and it seems as table mshop_order_basket gets already update with every basket change - either a new entry added or the existing one update (already without a Basket Plugin)

But I'm still wondering how to repopulate the stored baskets from mshop_order_basket back to the frontend basket? I searched for an already extising implementation or code example (eg. in ai-client-html/src/Client/Html/Account/Basket or
History) but without any success.
Could you provide some guidance or reference code please?

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

Re: How to automatically save baskets in 2022.10

Post by aimeos » 11 Jan 2023, 09:07

We haven't done that before too but the posted link should be the right place to load the basket as soon as the customer logs in: https://github.com/aimeos/aimeos/blob/m ... roller.php
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

columbo
Advanced
Posts: 126
Joined: 09 Oct 2019, 09:42

Re: How to automatically save baskets in 2022.10

Post by columbo » 17 Jan 2023, 22:47

I'm using Laravel Fortify, I don't have AuthenticatedSessionController.php in /app/Http/Controllers/Auth (only in /vendor/laravel/fortify/src/Http/Controllers)

After some research I found Laravel Fortify LoginResponse and I'm now trying to repopulate the basket there
https://laravel.com/docs/9.x/fortify#cu ... -redirects

by using this code:

Code: Select all

<?php

namespace App\Http\Responses;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

class LoginResponse 
implements LoginResponseContract

{
    public function toResponse($request) 
    {
		$context = $this->context();
		$manager = \Aimeos\MShop::create( $context, 'order/basket' );
		$filter = $manager->filter()
			->add( 'order.basket.customerid', '==', $context->user())
			->add( 'order.basket.name', '==', 'default' );;

		$basketItems = $manager->search( $filter );

		$domain = ['attribute', 'catalog', 'media', 'price', 'product', 'text', 'locale/site'];
		$basketCntl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
		$productCntl = \Aimeos\Controller\Frontend::create( $context, 'product' )->uses( $domain );

		foreach( $basketItems as $basketItem ) {
			if( $basket = $basketItem->getItem() ) {
				foreach( $basket->getProducts() as $pos => $orderProduct ){
					$basketCntl->addProduct( $productCntl->get( $orderProduct->getProductId()),
						$orderProduct->getQuantity()
					);
				}
			}
		}

       return redirect()->intended($dashboard);
    }
}
  1. How can I get $view, $context etc. and return the updated view?
  2. Are there any other methods or better solutions to load products from order/basket the frontend basket?
Thank you

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

Re: How to automatically save baskets in 2022.10

Post by aimeos » 19 Jan 2023, 13:33

columbo wrote: 17 Jan 2023, 22:47 How can I get $view, $context etc. and return the updated view?
Read here how to get the Aimeos context which also contains the view:
https://aimeos.org/docs/latest/laravel/ ... os-objects
columbo wrote: 17 Jan 2023, 22:47 Are there any other methods or better solutions to load products from order/basket the frontend basket?
Up to now it's not possible to inject your own basket into the basket controller, which then would create a new entry in the mshop_order_basket table. We could add a inject() method in the next version if you create a pull request in the Github repo.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply