# Problem mit Beispiel-Plugin "Add discount for specific products"

**URL:** <https://forum.shopware.com/t/problem-mit-beispiel-plugin-add-discount-for-specific-products/70377>\
**Category:** Programmierung\
**Created:** [22. Oktober 2020 um 11:54 UTC](https://forum.shopware.com/t/problem-mit-beispiel-plugin-add-discount-for-specific-products/70377 "2020-10-22T11:54:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lhairman](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@lhairman](https://forum.shopware.com/u/lhairman)\
**Post date:** [22. Oktober 2020 um 11:54 UTC](https://forum.shopware.com/t/problem-mit-beispiel-plugin-add-discount-for-specific-products/70377/1 "2020-10-22T11:54:25Z")

</div>

Hallo zusammen,

foglendes Beispiel-Plugin verwende ich und möchte dies erweitern, um bei bestimmten Produkten im Warenkorb einen Versicherungsartikel anzubieten.

[https://docs.shopware.com/en/shopware-platform-dev-en/how-to/cart-add-discount](https://docs.shopware.com/en/shopware-platform-dev-en/how-to/cart-add-discount)

Das Beispiel gibt einen Discount auf alle Produkte, die einen bestimmten Namen haben. Funktinoiert einwandfrei mit einem Discount. Gebe ich allerdings einen Aufschlag an (also 10, statt -10) bei der Preisberechnung, so stürtzt mir die Entwicklerversion mit der folgenden Fehlermeldung ab:

```
An exception has been thrown during the rendering of a template ("Parameter "productId" for route "frontend.detail.page" must match "[^/]++" ("" given) to generate a corresponding URL.").

```

Auf einer Produktionsumgebung läuft das Plugin dageben auch mit einem Aufschlag.

Ich habe so überhaupt keine Idee, wo ich da ansetzen muss. Hat jemand eine Idee?

Danke und Grüße,

Simon

&nbsp;

Anbei noch Auszug aus der Logdatei:

```
[2020-10-22 11:50:05] request.CRITICAL: Uncaught PHP Exception Twig\Error\RuntimeError: "An exception has been thrown during the rendering of a template ("Parameter "productId" for route "frontend.detail.page" must match "[^/]++" ("" given) to generate a corresponding URL.")." at /var/www/html/sw6/vendor/shopware/platform/src/Storefront/Resources/views/storefront/component/checkout/offcanvas-item.html.twig line 31 {"exception":"[object] (Twig\\Error\\RuntimeError(code: 0): An exception has been thrown during the rendering of a template (\"Parameter \"productId\" for route \"frontend.detail.page\" must match \"[^/]++\" (\"\" given) to generate a corresponding URL.\"). at /var/www/html/sw6/vendor/shopware/platform/src/Storefront/Resources/views/storefront/component/checkout/offcanvas-item.html.twig:31, Symfony\\Component\\Routing\\Exception\\InvalidParameterException(code: 0): Parameter \"productId\" for route \"frontend.detail.page\" must match \"[^/]++\" (\"\" given) to generate a corresponding URL. at /var/www/html/sw6/vendor/symfony/routing/Generator/UrlGenerator.php:193)"} []

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![lhairman](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@lhairman](https://forum.shopware.com/u/lhairman)\
**Post date:** [2. November 2020 um 08:32 UTC](https://forum.shopware.com/t/problem-mit-beispiel-plugin-add-discount-for-specific-products/70377/2 "2020-11-02T08:32:17Z")

</div>

Das war eine gute Übung, das Problem zu finden… Für alle, die hier auch mal das Problem haben:

Liegt daran, dass in dem Beispiel-Plugin durch folgenden Befehl eine neues LineItem im Cart angelegt wird

```
private function createDiscount(string $name): LineItem
    {
        $discountLineItem = new LineItem($name, 'awesome_discount', null, 1);
        
        [...]
    }

```

Entscheident ist \>null\< für die&nbsp;$referencedId im Konstruktor.&nbsp;

Auszug aus der Twig-Datei&nbsp;offcanvas-item.html.twig (\vendor\shopware\storefront\Resources\views\storefront\component\checkout)&nbsp;

```
{% block component_offcanvas_cart_item_container %}
        
            
                {% block component_offcanvas_product_image %}
                    
                        {% block component_offcanvas_product_image_inner %}
                            
                                {% if isDiscount %}
                                    
                                        {% sw_icon 'marketing' style {
                                            'color': 'success'
                                        } %}
                                    
                                {% elseif isNested %}
                                    
                                        {% sw_icon 'bag-product' style {
                                            'color': 'light'
                                        } %}
                                    
                                {% else %}
                                    

isDiscount wird hier behandelt, für eine Warenkorbposition, die <0 ist. Bei einem Aufschlag aber nicht der Fall, daher geht es in die else-Verzweigung mit Fehlermeldung >null< für referenceId
```
