Reputation: 23
i use the following code to add a discount to the shopping cart.
{% set discountPrice = services.cart.price.create({
'default': { 'gross': 19.99, 'net': 19.99},
'EUR': { 'gross': 19.99, 'net': 19.99},
}) %}
{% if not services.cart.has('my-custom-discount') %}
{% do services.cart.remove('my-custom-discount') %}
{% endif %}
{% do services.cart.discount('my-custom-discount', 'absolute', discountPrice, 'my.custom.discount.label'|trans) %}
This also works quite well in the frontend. However, when I edit the order in the backend, I get the error CHECKOUT__CART_LINEITEM_NOT_STACKABLE Line item with identifier my-custom-discount is not stackable and the quantity cannot be changed. What do I have to consider to make it work? I got the code example from the Shopware website
Upvotes: 0
Views: 143
Reputation: 13161
The script you posted has a logical mistake. You're removing the discount if the cart does not have the discount yet. You actually want to remove the discount if the cart does already have it.
{# removed `not` #}
{% if services.cart.has('my-custom-discount') %}
{% do services.cart.remove('my-custom-discount') %}
{% endif %}
You probably don't want to re-add the discount when you edit an already placed order, e.g. if you change the script in the future but older discounts should remain. Then you can check the sales channel permissions for the skipPromotion
permission. It should be true
when editing orders in the administration and then you could skip the script entirely.
{% if hook.salesChannelContext.permissions['skipProductRecalculation'] is empty %}
{% set discountPrice = services.cart.price.create({
'default': { 'gross': 19.99, 'net': 19.99},
'EUR': { 'gross': 19.99, 'net': 19.99},
}) %}
{% if not services.cart.has('my-custom-discount') %}
{% do services.cart.remove('my-custom-discount') %}
{% endif %}
{% do services.cart.discount('my-custom-discount', 'absolute', discountPrice, 'my.custom.discount.label'|trans) %}
{% endif %}
Upvotes: 0