Set a basket custom rebate & multiple rebates

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

Set a basket custom rebate & multiple rebates

Post by sixbynine » 15 Oct 2018, 10:10

Hello,

1/
For a special offer, when some particular products are bought together following their given category, I would like to set a custom fixed calculated rebate on my current basket with a basket plugin.

I was hoping that something like :

Code: Select all

<check if products are in the given categories == true >
$myspecialcalculatedrebate = 5.00;
$basket->getPrice()->setRebate($myspecialcalculatedrebate );
could work ...
but If i'm not wrong, I realize that setRebate() method is related to a product price only.
And the basket rebate is the sum of the product rebates. Is that right?

So, i'm wondering what is the best way (do you think) to add a "global" fixed rebate to the current basket?

2/
In some case I would like to add a percentRebate on basket total, so the basket rebate will contain the sum of 2 reductions. What is the best way to manage multiple rebates together?

Thank you very much,

sbn

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

Re: Set a basket custom rebate & multiple rebates

Post by aimeos » 16 Oct 2018, 06:32

sixbynine wrote: but If i'm not wrong, I realize that setRebate() method is related to a product price only.
And the basket rebate is the sum of the product rebates. Is that right?
So, i'm wondering what is the best way (do you think) to add a "global" fixed rebate to the current basket?
You are right, the basket rebate value is automatically calculated by the product rebate values.
The best way is to add a rebate product which you can name, add an image and calculate a price for. It's also easy to remove that product from the basket if the rebate conditions doesn't apply any more, e.g. due to the customer deleted a product from the basket.
sixbynine wrote: In some case I would like to add a percentRebate on basket total, so the basket rebate will contain the sum of 2 reductions. What is the best way to manage multiple rebates together?
Easiest is to use two rebate products because of the things said before. Otherwise, you will run into edge cases that may be hard to fix.
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: Set a basket custom rebate & multiple rebates

Post by sixbynine » 16 Oct 2018, 07:34

Thank you very much for your answer.

Just to be sure I correctly understand by a "rebate product", you mean a default product where I set a negative price?

Which code do I use to add a given product to the basket in my plugin?
Is there an example somewhere?

Thank you very much,

sbn

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

Re: Set a basket custom rebate & multiple rebates

Post by sixbynine » 16 Oct 2018, 08:40

Here is my code :

Code: Select all

$value = <mynewnegativevalue>;
$manager = \Aimeos\MShop\Factory::createManager( $context, 'product' );
$product = $manager->getItem($myid, array( 'product','price'));
$prices = $product->getRefitems( 'price', 'default', 'default' );
foreach($prices as $p){
 $p->setValue($value);
}
$basket->addProduct($product);
But I finally get this error : Type error: Argument 1 passed to Aimeos\MShop\Order\Item\Base\Base::addProduct() must implement interface Aimeos\MShop\Order\Item\Base\Product\Iface, instance of Aimeos\MShop\Product\Item\Standard given

EDIT : this problem was the same as explained here : help-f15/basket-controller-addproduct-d ... -t855.html

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

Re: Set a basket custom rebate & multiple rebates

Post by sixbynine » 16 Oct 2018, 13:10

I finally have 2 last questions :

1/

Code: Select all

foreach ($basket->getProducts() as $basketItem) {
if($myproductid == $basketItem->getProductId()){
 var_dump($basketItem->getPosition());
}
}
returns NULL instead of the expected product position in the basket when $myproductid is in the basket.
I don't understand why.

2/
I would like to finally override the price value of my product :

Code: Select all

$myvalue = <mynegativevalue>;
$tobasket->addProduct($myproductid, 1);

$basket = $controller->get();
foreach ($basket->getProducts() as $basketItem) {
    if($basketItem->getProductId() == $myproductid){
    $price = $basketItem->getPrice();
    $price->setValue($value);
    $basketItem->setPrice($price);
var_dump( $basketItem)
}
}
The var_dump gives me the expected result but when I check the basket in the frontend, myproductid is well there but the price is not the "$value" one. Not sure about the reason why...

Thank you very much in advance,

sbn

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

Re: Set a basket custom rebate & multiple rebates

Post by aimeos » 17 Oct 2018, 11:49

A "rebate product" is a simple article which is not associated to a category and is used to add a rebate value to the basket. It can have a negative price associated but more often the price is dynamically calculated and added to the order base product by your own code:

Code: Select all

$product = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'product' )->findItem( '<your code>', ['text', 'media'] );
$orderProduct = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'order/base/product' )->createItem();
$orderProduct->copyFrom( $product )->getPrice()->setValue( -5 )->setRabate( 5 );

// basket is passed to the basket plugins as first parameter
$basket->addProduct( $orderProduct );
The position of the product in the basket is undefined until it's stored in the mshop_order_base_product table.

If you use the basket controller (which you should not in a basket plugin!), the basket needs to be saved to the session to persist the changes using $cntl->save()
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: Set a basket custom rebate & multiple rebates

Post by sixbynine » 20 Oct 2018, 07:57

Thank you !
It works perfectly.

createanet
Posts: 72
Joined: 22 Mar 2021, 16:56

Re: Set a basket custom rebate & multiple rebates

Post by createanet » 01 Dec 2021, 12:10

Sorry to hijack this thread, its been helpful but im having some issues relating to the same questions. I notice that when creating the rebate order product we now have to set the stock type, else the productStock throws an error.

Im wanting to add single article to adjust the price of the customers basket from my plugin based on condition(s) im defining. The problems I am now encountering is the user can modify the quantity of the rebate product in the basket, and I am unable to add unless I set a quantity on the order item.

Is this something I need to guard against? Am I still missing some things that have changed in 2020.10.*?

My code is as follows:

Code: Select all

public function update(\Aimeos\MW\Observer\Publisher\Iface $order, string $action, $value = null)
{
    ...
    
    $code = $this->getConfigValue('productcode');

    $product = \Aimeos\MShop::create($this->getContext(), 'product')->findItem($code, ['text', 'media', 'price']);
    $orderProduct = \Aimeos\MShop::create($this->getContext(), 'order/base/product')->create()->copyFrom($product);
    $orderProduct->getPrice()->setValue(-5)->setRebate(5);
    $orderProduct->setQuantity(1)->setStockType('default');

    $order->addProduct( $orderProduct );

    return $value;
}
Once I get it added, I notice I get errors then reporting that "Product not available" which is an exception thrown from the \Aimeos\MShop\Order\Item\Base\Base class when calling getProduct.

Thanks

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

Re: Set a basket custom rebate & multiple rebates

Post by aimeos » 02 Dec 2021, 12:16

To prevent customers to change the rebate product, set the immutable flag for the order product like done in the coupon providers:
https://github.com/aimeos/aimeos-core/b ... e.php#L238
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

createanet
Posts: 72
Joined: 22 Mar 2021, 16:56

Re: Set a basket custom rebate & multiple rebates

Post by createanet » 02 Dec 2021, 15:28

Everyday is a school day, thanks again for this it works perfectly and does exactly what I want.

Post Reply