Add price after pressing "add to basket" button

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
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Add price after pressing "add to basket" button

Post by aimeos » 23 Aug 2019, 06:59

The parameters are read-only. You have to retrieve all, change the one you need and setup the view helper again:

Code: Select all

$params = $view->param();
$params['b_prod'] = [/* modified array */];
$view->addHelper( 'param', new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

lucadambros
Posts: 44
Joined: 24 Jun 2019, 14:46

Re: Add price after pressing "add to basket" button

Post by lucadambros » 23 Aug 2019, 10:19

Sorry I have another question:

how can I clone the product and change the price of it?

I have the product

Code: Select all

    $params = $view->param();
    $productManager = \Aimeos\MShop::create( $context, 'product' );
    $item = $productManager->getItem( $params['b_prod'][0]['prodid'], ['media', 'price', 'text'] );
Now what I have to do?

Thank you very much

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

Re: Add price after pressing "add to basket" button

Post by aimeos » 24 Aug 2019, 09:04

Code: Select all

    $priceManager = \Aimeos\MShop::create( $context, 'price' );
    $productManager = \Aimeos\MShop::create( $context, 'product' );
    $item = $productManager->getItem( $view->param( 'b_prod/0/prodid' ), ['media', 'price', 'text'] );
    
    $listItem = $productManager->createListItem()->setType( 'default' );
    $priceItem = $priceManager->createItem()->setType( 'default' )->setCurrencyId( 'EUR' );
    $item->addListItem( 'price', $listItem, $refItem );
Here's the interface: https://github.com/aimeos/aimeos-core/b ... /Iface.php
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

lucadambros
Posts: 44
Joined: 24 Jun 2019, 14:46

Re: Add price after pressing "add to basket" button

Post by lucadambros » 26 Aug 2019, 06:57

I got this error:

Code: Select all

Aimeos \ MShop \ Exception
Unable to call method "createListItem"
EDIT: the function is createListsItem not createListItem

lucadambros
Posts: 44
Joined: 24 Jun 2019, 14:46

Re: Add price after pressing "add to basket" button

Post by lucadambros » 26 Aug 2019, 07:07

I got another error:

Code: Select all

Call to undefined method Aimeos\MShop\Price\Item\Standard::setCurrency()
EDIT: the function is SetCurrencyId

lucadambros
Posts: 44
Joined: 24 Jun 2019, 14:46

Re: Add price after pressing "add to basket" button

Post by lucadambros » 27 Aug 2019, 07:20

Hi,
I created the decorator. I tried to clone the product and sending the new ID to the AddProduct function. The product is in the db correctly but I am getting an error.
I wrote this code:

Code: Select all

$priceManager = \Aimeos\MShop::create( $context, 'price' );
$productManager = \Aimeos\MShop::create( $context, 'product' );
$mediaManager = \Aimeos\MShop::create( $context, 'media' );

$item = $productManager->getItem( $view->param( 'b_prod/0/prodid' ), ['media', 'price', 'text']);
$listItem = $productManager->createListsItem()->setType( 'default' );
$priceItem = $priceManager->createItem()->setType( 'default' )->setCurrencyId( 'EUR' )->setValue(5);
$mediaItem = $item->getRefItems('media', 'default');
$mediaItem = array_values($mediaItem)[0];

$clone_item = $productManager->createItem();
$clone_item->addListItem( 'price', $listItem, $priceItem);
$clone_item->addListItem( 'media', $listItem, $mediaItem);
       
$productManager->saveItem($clone_item);    
$tmp = $clone_item->toArray();
$params['b_prod'] = [["prodid"=> $tmp["product.id"], "quantity"=>"1"]];
$view->addHelper( 'param', new \Aimeos\MW\View\Helper\Param\Standard( $view, $params ));
I am getting this error:

Code: Select all

Adding product with ID "95" is not allowed
The ID "95" is the new product. Why I am getting this error?

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

Re: Add price after pressing "add to basket" button

Post by aimeos » 27 Aug 2019, 07:56

Your product has no category assigned and that's required when using the basket frontend controller.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

lucadambros
Posts: 44
Joined: 24 Jun 2019, 14:46

Re: Add price after pressing "add to basket" button

Post by lucadambros » 27 Aug 2019, 08:07

How can I assign it?

(it must be the same as the original)

lucadambros
Posts: 44
Joined: 24 Jun 2019, 14:46

Re: Add price after pressing "add to basket" button

Post by lucadambros » 27 Aug 2019, 10:09

i also wrote this:

Code: Select all

$clone_item->addListItem( 'catalog', $listItem, $catalogManager->getItem(1));
but I got basket empty

Seems like the file ext/ai-client-html/client/html/src/Client/Html/Basket/Standard/Standard.php is not getting the product in the line

Code: Select all

// the list has the right ID
$products = $productCntl->product( $list )->slice( 0, count( $list ) )->search();

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

Re: Add price after pressing "add to basket" button

Post by aimeos » 27 Aug 2019, 14:35

You have to create the product in the backend first and can't create a product on the fly. There are dependencies that require the product in the database.

Alternatively, if all your products can be bought, you can remove the Category frontend basket decorator:
https://aimeos.org/docs/Configuration/C ... tors/local
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply