Issue when editing order in Shopware admin panel when adding custom discount
P粉811349112
2023-09-01 17:30:17
<p>I wrote a custom shopping cart discount according to the documentation https://developer.shopware.com/docs/guides/plugins/plugins/checkout/cart/add-cart-discounts
Everything works fine, when the customer adds or removes a product from the cart, the discount is recalculated, but when I want to edit this order in the admin panel, I get the error: </p>
<blockquote>
<p> Uncaught PHP exception Shopware\Core\Checkout\Cart\Exception\LineItemNotStackableException: "The line item with identifier 'CHEAPEST_ITEM_CART_DISCOUNT' is not stackable and the quantity cannot be changed." in /var/www/shop/vendor/ shopware/core/Checkout/Cart/LineItem/LineItem.php line 233 {"Exception": "[Object](Shopware\Core\Checkout\Cart\Exception
LineItemNotStackableException (Code: 0): The line item with identifier "CHEAPEST_ITEM_CART_DISCOUNT" is not stackable and the quantity cannot be changed. at /var/www/shop/vendor/shopware/core/Checkout/Cart/LineItem/LineItem.php:233)"} []</p>
</blockquote>
<p>I "solved" this problem by adding this check to my code: </p>
<pre class="brush:php;toolbar:false;">if($behavior->hasPermission(self::SKIP_PROMOTION)){
$items = $original->getLineItems()->filterType(self::LINE_ITEM_TYPE);
foreach ($items as $item) {
$toCalculate->add($item);
}
return;
}</pre>
<p>Now, if I edit the order in the admin panel, the error does not show up, but the discount is not calculated again, and toggling "Disable automatic promotions" does not work. </p>
<p>Is there any solution to recalculate custom discounts sequentially on edit operations?
When I add a new order in the admin panel everything works fine, the problem is only with editing.
The documentation doesn't say anything about this. </p>
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.
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.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 release6.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.