new "save basket as template" feature

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: 151
Joined: 09 Oct 2019, 09:42

new "save basket as template" feature

Post by columbo » 01 Sep 2024, 10:57

Hi,

I'm looking for a "save basket as template" feature.
I know that baskets can already be saved at checkout, but I need more functionality:
  • manage multiple basket templates
  • edit saved baskets (remove or add product to template list)
  • on catalog- and product detail page: "add product to basket-template"-button, with a "template list" selector
What do you recommend for implementation?
Extend the favorites feature? Or any other suggestions?

Thank you!

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

Re: new "save basket as template" feature

Post by aimeos » 01 Sep 2024, 21:47

With the current "save basket" feature, you can save multiple baskets as templates and add the products to the current basket (later) again. It's currently not possible to edit saved baskets, only to apply them to the current (empty) basket, save them again and delete the old basket. Editing baskets feature can be add based on the current implementation I guess.

The "add to template" feature definitively needs some more customization but I would try to use a different basket type for the templates to avoid merging with the current basket.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

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

Re: new "save basket as template" feature

Post by columbo » 18 Sep 2024, 08:04

Hi,

I am trying to create a new, empty 'order/basket' with a custom name ($baskettemplateName).
It creates a new basket, but also fills it with products from my current frontend basket.
Because of: ->setItem( \Aimeos\Controller\Frontend::create( $context, 'basket' )->get()

Code: Select all

$manager = \Aimeos\MShop::create( $context, 'order/basket' );

$item = $manager->create()->setId( md5( microtime( true ) . getmypid() . rand() ) )
	->setCustomerId( $userId )->setName( $baskettemplateName )
	->setItem( \Aimeos\Controller\Frontend::create( $context, 'basket' )->get() );

$manager->save( $item );
Tried with empty ->setItem() and also without ->setItem but its not working.
How can I create a new, empty 'order/basket' ?

Thank you!

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

Re: new "save basket as template" feature

Post by aimeos » 18 Sep 2024, 11:47

Create an empty basket/order using:

Code: Select all

\Aimeos\MShop::create( $context, 'order' )->create()
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

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

Re: new "save basket as template" feature

Post by columbo » 20 Sep 2024, 08:13

unfortunately i'm still struggeling:

to create a new basket template:

Code: Select all

$manager = \Aimeos\MShop::create( $context, 'order/basket' );
$item = $manager->create()->setId( md5( microtime( true ) . getmypid() . rand() ) )
       ->setCustomerId( $userId )
       ->setName( $view->param( 'templatename') )
       ->setItem( \Aimeos\MShop::create( $context, 'order' )->create() );
$manager->save( $item );

add a product to a template:

Code: Select all

//first get basket
$manager = \Aimeos\MShop::create( $context, 'order/basket' );
$filter = $manager->filter()
       ->add( 'order.basket.customerid', '==', $context->user() )
       ->add( 'order.basket.id', '==', $basketid );

$basketCntl = $manager->search( $filter )->getItem() ;

//add Product to basket
$product = \Aimeos\MShop::create($context, 'product')->get(	$prodid, ['text', 'media', 'price']);
$orderProduct = \Aimeos\MShop::create($context, 'order/product')->create();
$orderProduct->copyFrom($product);
$basketCntl->addProduct($orderProduct, 1);
get all basket templates and their products

Code: Select all

$manager = \Aimeos\MShop::create( $context, 'order/basket' );
$filter = $manager->filter()->order( '-order.basket.mtime' )
	->add( 'order.basket.customerid', '==', $context->user() )
	->add( 'order.basket.name', '!=', '' );

$view->basketItems = $manager->search( $filter );
I did not receive any errors, but the product is not added to the basket template

thank you

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

Re: new "save basket as template" feature

Post by aimeos » 22 Sep 2024, 21:11

Without saving the basket after you've added the product, no changes will persist in the database.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

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

Re: new "save basket as template" feature

Post by columbo » 23 Sep 2024, 21:06

but how?

tried with manager->save(), but

Code: Select all

$manager->save( $basketCntl );
causes
Aimeos\MShop\Order\Manager\Basket\Standard::saveItem(): Argument #1 ($item) must be of type Aimeos\MShop\Order\Item\Basket\Iface, Aimeos\MShop\Order\Item\Standard given...

and

Code: Select all

$manager->save( $orderProduct );
causes:
Aimeos\MShop\Order\Manager\Basket\Standard::saveItem(): Argument #1 ($item) must be of type Aimeos\MShop\Order\Item\Basket\Iface, Aimeos\MShop\Order\Item\Product\Standard given

I can't use the frontend controller (\Aimeos\Controller\Frontend::create( $context, 'basket' )) as the product should not be added to the (frontend) basket, only added to the template-basket

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

Re: new "save basket as template" feature

Post by aimeos » 24 Sep 2024, 09:27

Code: Select all

$manager = \Aimeos\MShop::create( $context, 'order/basket' );
$filter = $manager->filter()
       ->add( 'order.basket.customerid', '==', $context->user() )
       ->add( 'order.basket.id', '==', $basketid );

$basket = $manager->search( $filter )->first();

if($item = $basket->getItem())
{
	//add Product to basket
	$product = \Aimeos\MShop::create($context, 'product')->get(	$prodid, ['text', 'media', 'price']);
	$orderProduct = \Aimeos\MShop::create($context, 'order/product')->create()->copyFrom($product);
	$item->addProduct($orderProduct, 1);

	// save basket again
	$manager->save($basket);
}
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

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

Re: new "save basket as template" feature

Post by columbo » 27 Sep 2024, 08:38

Unfortunately it doesn't work - the orderProduct is not saved to the basket.
Then loading order/basket again, right after $manager->save($basket) the product is missing

Code: Select all

if($item = $basket->getItem())
  {
    $product = \Aimeos\MShop::create($context, 'product')->get(	$prodid, ['text', 'media', 'price']);
    $orderProduct = \Aimeos\MShop::create($context, 'order/product')->create()->copyFrom($product);
    $item->addProduct($orderProduct, 1);
					
    $manager->save($basket);
    Log::info("item 1:" . print_r(json_encode($item), true));
  }

//test output
$manager2 = \Aimeos\MShop::create( $context, 'order/basket' );
$filter2 = $manager2->filter()
  ->add( 'order.basket.customerid', '==', $context->user() )
  ->add( 'order.basket.id', '==', $basketid );

$basket2 = $manager2->search( $filter2 )->first();
if($item2 = $basket2->getItem())
  {
    Log::info("item 2:" . print_r(json_encode($item2), true));
  }
output:

Code: Select all

item 1:{"order.siteid":"1.","coupons":[],"products":{"1":{"order.product.siteid":"1.","cgroups":0,"order.product.prodcode":"25025","order.product.productid":"101794","order.product.type":"default","order.product.scale":1,"order.product.target":"","order.product.name":"Product ABC","order.product.mediaurl":"..."}},"services":[],"addresses":[],"price":{...},"locale":{...},"customer":null}  

item 2:{"order.siteid":"1.","coupons":[],"products":[],"services":[],"addresses":[],"price":{...},"locale":{...},"customer":null}

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

Re: new "save basket as template" feature

Post by aimeos » 27 Sep 2024, 09:14

This is a working example from an unit test for Aimeos 2024.x:

Code: Select all

$product = \Aimeos\MShop::create( $context, 'product' )->find( 'CNE' );
$orderProduct = \Aimeos\MShop::create( $context, 'order/product' )->create()->copyFrom( $product );
$order = \Aimeos\MShop::create( $context, 'order' )->create()->addProduct( $orderProduct );

$manager = \Aimeos\MShop::create( $context, 'order/basket' );
$basket = $manager->save( $manager->create()->setItem( $order )->setId( 'test-1' ) );
$basket2 = $manager->get( $basket->getId() );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply