P粉7104549102023-09-02 14:33:53
Find if the discount has been added to the cart by referencing the identifier. If so, simply remove the old instance from your cart and add the recalculated discount.
if ($original->has($discountLineItem->getId())) { $original->get($discountLineItem->getId())->setRemovable(true); $original->remove($discountLineItem->getId()); return; } // $discountLineItem->getType() should equal LineItem::DISCOUNT_LINE_ITEM $toCalculate->add($discountLineItem);
Additionally, you must also ensure that your processor executes after Shopware's own PromotionProcessor
, otherwise it will try to re-add the discount you previously added manually.
<service id="MyPlugin\Cart\CustomPromotionProcessor"> <!-- ... inject after default discount cart processor (3700) --> <tag name="shopware.cart.processor" priority="3600"/> </service>
I created a sample plugin that contains all the changes in the guide required for the recalculation. Tested on current release candidate 6.5
, but should also work on the latest release 6.4
.
This example is based on a discount as a percentage of the cart value. If the discount should be the absolute value of the change, the process is slightly different. I created a branch in the above repository and provided an example.