Service custom price with CalcPrice()

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!
sixbynine
Posts: 93
Joined: 10 Jan 2018, 11:22

Service custom price with CalcPrice()

Post by sixbynine » 10 Sep 2018, 12:43

Hello,

I would like to calculate a custom price for my delivery service... I created a provider and add a custom calcprice() function based on a service attribute choosen by user, I added :

$myprices = //array of prices;
$value = $service->getAttribute('mycode','delivery');

$thecost = myprices[$value];
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'price' );
$price = $manager->createItem();
$price->setCosts($thecost);
return $price;

but most of the time, this calcprice() function is not taken in account and the service cost is finally 0.00

I checked my code and I realized that $basket->getService is empty at that point.
So my $value also is.

I'm not sure the reason why... The service is probably not added yet to the basket at this point.

Maybe I do need to move my custom CalcPrice() function to a decorator that will be called later?
Or maybe I do need to move it in a basket plugin set to addService after?

Sorry, I'm a bit confused !

Could you help me?

Thank you very much in advance :)

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

Re: Service custom price with CalcPrice()

Post by aimeos » 10 Sep 2018, 22:07

As you've noticed correctly, the calcPrice() method is called before the (delivery) service options are displayed so the costs can be based on the current basket content. At this point, the basket doesn't contain any service. When the customer has chosen the service attribute and the "Next" button is clicked, the calcPrice() method is called again. At this point, the service and the attributes aren't part of the basket because it's only added afterwards.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

sixbynine
Posts: 93
Joined: 10 Jan 2018, 11:22

Re: Service custom price with CalcPrice()

Post by sixbynine » 11 Sep 2018, 07:14

Ok ... Sorry ! I thought that the calcprice() method was called to set the cost of the selected service.
So what is the correct way to set the cost of the service with a dynamic price?

Where do I need to set the cost:

- otherwhere in my provider?

- in a decorator that is maybe called after the service is added?

- in the basket plugin using the listener' setService.after '?

Thank you very much !

sbn

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

Re: Service custom price with CalcPrice()

Post by aimeos » 11 Sep 2018, 20:00

As long as we don't extend the calcPrice() method and hand over the attributes as well, the only way is by implementing a basket plugin.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply