# Controller function überschreiben / Warenkorb leeren bevor neuer Artikel hinzugefügt wird

**URL:** <https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438>\
**Category:** Programmierung\
**Created:** [8. Mai 2019 um 08:54 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438 "2019-05-08T08:54:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fezzzzy](https://avatars.discourse-cdn.com/v4/letter/f/90db22/32.png) [@Fezzzzy](https://forum.shopware.com/u/Fezzzzy)\
**Post date:** [8. Mai 2019 um 08:54 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/1 "2019-05-08T08:54:55Z")

</div>

Hallo,

ich bräuchte nochmal Eure Hilfe.

1. Gibt es eine Möglichkeit, dass wenn man den „In den Warenkorb“ - Button drückt,&nbsp;erst alle Artikel die bereits im Warenkorb liegen, gelöscht werden? (Am liebsten anhand eines definierten Attributes einer speziellen Form)

2. Wie kann ich im Listing Artikel aus dem Warenkorb ausgeben lassen? Mir reicht Ordernumber und Menge.

Vielen Dank im Voraus!&nbsp; ![Halo](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/halo.png "Halo")  
&nbsp;

---

<div class="post-metadata">

**Author:** ![R4M](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/r4m/32/9983_2.png) [@R4M](https://forum.shopware.com/u/R4M)\
**Post date:** [8. Mai 2019 um 10:39 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/2 "2019-05-08T10:39:25Z")

</div>

Hm,

zu 1. Das würde bedeuten, der Kunde kann nie 2 Artikel in den Warenkorb legen. Vermutlich nur über Plugin zu lösen.

zu 2. Auch dies ist vermutlich nur über Plugin zu lösen.

---

<div class="post-metadata">

**Author:** ![Fezzzzy](https://avatars.discourse-cdn.com/v4/letter/f/90db22/32.png) [@Fezzzzy](https://forum.shopware.com/u/Fezzzzy)\
**Post date:** [8. Mai 2019 um 12:28 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/3 "2019-05-08T12:28:44Z")

</div>

Danke für die Rückmeldung!

Ergänzend dazu muss ich sagen, dass der Shop lediglich die Funktion hat, ein Array von &nbsp;ordernumbers und values in den Warenkorb zu legen, welche vorher nach verschiedenen Parametern zusammengestellt werden.

Wenn ich die Artikel nun in den Warenkorb gelegt habe und wieder zurück klicke und wieder die Artikel&nbsp;in den Warenkorb lege, dann sind alle Artikel 2x drin. Es müsste nur so sein, dass immer nur die Artikel im Warenkorb sein dürfen, die man zuletzt zusammengestellt hat und über das array per sAddAccessoires übergibt.&nbsp;

Hoffe das ist einigermaßen verständlich ausgedrückt.

---

<div class="post-metadata">

**Author:** ![Fezzzzy](https://avatars.discourse-cdn.com/v4/letter/f/90db22/32.png) [@Fezzzzy](https://forum.shopware.com/u/Fezzzzy)\
**Post date:** [8. Mai 2019 um 13:59 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/4 "2019-05-08T13:59:13Z")

</div>

Habe es nun hinbekommen, indem ich im Checkout Controller die&nbsp;addAccessoriesAction Funktion angepasst habe.

```
public function addAccessoriesAction()
    {
        $basket = $this->getBasket();
        foreach ($basket['content'] as $item) {
          $this->basket->sDeleteArticle($item['id']);
        }
        $this->addAccessories(
            $this->Request()->getParam('sAddAccessories'),
            $this->Request()->getParam('sAddAccessoriesQuantity')
        );
        $this->forward($this->Request()->getParam('sTargetAction', 'cart'));
    }

```

Jetzt bräuchte ich nur noch einen Tipp, wie ich die Funktion updatesicher überschreiben kann.

---

<div class="post-metadata">

**Author:** ![sonic](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/sonic/32/25728_2.png) [@sonic](https://forum.shopware.com/u/sonic)\
**Post date:** [8. Mai 2019 um 14:01 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/5 "2019-05-08T14:01:12Z")

</div>

Garnicht&nbsp; ![Wearing-Sunglasses](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/301eaa4db0af1251d93dae7904e5b71a4b15e729.png "Wearing-Sunglasses")  
Einfach auf den PreDispatch von CheckOut registrieren, und dort die action auf AddAccssories abfragen&nbsp;und dann das Löschen durchführen.  
Warum die Schleife, geht das nicht?  
[https://github.com/shopware/shopware/blob/5.5/engine/Shopware/Core/sBasket.php#L1762](https://github.com/shopware/shopware/blob/5.5/engine/Shopware/Core/sBasket.php#L1762)

---

<div class="post-metadata">

**Author:** ![Fezzzzy](https://avatars.discourse-cdn.com/v4/letter/f/90db22/32.png) [@Fezzzzy](https://forum.shopware.com/u/Fezzzzy)\
**Post date:** [8. Mai 2019 um 14:33 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/6 "2019-05-08T14:33:18Z")

</div>

Puh, okay&nbsp; ![Angry-Face](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/angry-face.png "Angry-Face")

Ich habe jetzt einfach zu Testzwecken das SwagGlobalVariables - Plugin genommen und die .php angepasst und versucht so umzusetzen. Leider funktioniert das so allerdings nicht. Habe ich da irgendwas grundlegendes falsch?

```
 'onPreDispatchCheckout',
        ];
    }

    /**
     * @param \Enlight_Controller_ActionEventArgs $args
     */
     public function onPreDispatchCheckout(\Enlight_Event_EventArgs $args)
     {
           /** @var \Shopware_Controllers_Frontend_Checkout $controller */
           $controller = $args->get('subject');

           if ($controller->Request()->getActionName() == 'addAccessories') {
                       $basket = $this->getBasket();
                       foreach ($basket['content'] as $item) {
                         $this->basket->sDeleteArticle($item['id']);
                       }
                       $this->addAccessories(
                           $this->Request()->getParam('sAddAccessories'),
                           $this->Request()->getParam('sAddAccessoriesQuantity')
                       );
                       $this->forward($this->Request()->getParam('sTargetAction', 'cart'));
                       return;
           }
    }
}

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![sonic](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/sonic/32/25728_2.png) [@sonic](https://forum.shopware.com/u/sonic)\
**Post date:** [8. Mai 2019 um 14:35 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/7 "2019-05-08T14:35:21Z")

</div>

Fang mal von vorne an&nbsp; ![Wink](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/b3785da0cbf2c8566cb535dcf55b2730b5224899.png "Wink")  
[https://developers.shopware.com/plugin-guide/](https://developers.shopware.com/plugin-guide/)

Den Aufruf von Add kannst Du ja schon mal sparen - die Action wird ja so oder so aufgerufen.  
Edit: Sollte so gehen - aber bitte nicht die Action nachbauen und gar nicht den forward! Das wird ja später regulär ausgeführt.  
&nbsp;

---

<div class="post-metadata">

**Author:** ![Fezzzzy](https://avatars.discourse-cdn.com/v4/letter/f/90db22/32.png) [@Fezzzzy](https://forum.shopware.com/u/Fezzzzy)\
**Post date:** [8. Mai 2019 um 15:10 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/8 "2019-05-08T15:10:32Z")

</div>

Danke schonmal! Wie müsste die Plugin Struktur denn aussehen? Habe jetzt mal versucht, das ganze anhand des Controller Beispiel Plugin ([https://developers.shopware.com/developers-guide/controller/#example](https://developers.shopware.com/developers-guide/controller/#example)) zu versuchen, aber da erhalte ich&nbsp;direkt einen 500- Fehler beim aktivieren des Beispiel-Plugins.

So funktioniert das jedoch auch nicht (es werden keine Artikel mehr hinzugefügt & auch keine gelöscht)&nbsp; ![Foot-in-Mouth](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/bedbc37f276faa6d086ddea9cd70cb0550bbd56e.png "Foot-in-Mouth")

```
public function onPreDispatchCheckout(\Enlight_Event_EventArgs $args)
     {
           /** @var \Shopware_Controllers_Frontend_Checkout $controller */
           $controller = $args->get('subject');

           if ($controller->Request()->getActionName() == 'addAccessories') {
                       $basket = $this->getBasket();
                       foreach ($basket['content'] as $item) {
                         $this->basket->sDeleteArticle($item['id']);
                       }
           }
    }

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![shyim](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/shyim/32/7681_2.png) [@shyim](https://forum.shopware.com/u/shyim)\
**Post date:** [8. Mai 2019 um 16:37 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/9 "2019-05-08T16:37:49Z")

</div>

$this-\>basket-\>sDeleteBasket(). Ist viel schneller und löscht alles wie du es willst

---

<div class="post-metadata">

**Author:** ![Fezzzzy](https://avatars.discourse-cdn.com/v4/letter/f/90db22/32.png) [@Fezzzzy](https://forum.shopware.com/u/Fezzzzy)\
**Post date:** [8. Mai 2019 um 16:47 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/10 "2019-05-08T16:47:18Z")

</div>

Perfekt, danke - funktioniert im Core!

Weißt du vielleicht wie ich das Plugin anpassen müsste damit es funktioniert?

Warum gibt es denn keine {$smarty.block.parent} ähnliche Variante um functions zu erweitern&nbsp; ![Angry-Face](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/angry-face.png "Angry-Face") ![Foot-in-Mouth](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/foot-in-mouth.png "Foot-in-Mouth")

&nbsp;

public function onPreDispatchCheckout(\Enlight\_Event\_EventArgs $args)  
&nbsp; &nbsp; &nbsp;{  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/\*\* @var \Shopware\_Controllers\_Frontend\_Checkout $controller \*/  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$controller = $args-\>getSubject();

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($controller-\>Request()-\>getActionName() == ‚addAccessories‘) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-\>basket-\>sDeleteBasket();  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; }

---

<div class="post-metadata">

**Author:** ![Fezzzzy](https://avatars.discourse-cdn.com/v4/letter/f/90db22/32.png) [@Fezzzzy](https://forum.shopware.com/u/Fezzzzy)\
**Post date:** [12. August 2019 um 08:00 UTC](https://forum.shopware.com/t/controller-function-uberschreiben-warenkorb-leeren-bevor-neuer-artikel-hinzugefugt-wird/59438/11 "2019-08-12T08:00:20Z")

</div>

Ich möchte das Thema nochmal kurz aufbereiten weil ich mich nun endlich mit Plugins auseinander setzen möchte ![Smile](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/smile.png "Smile")

Im Core funktioniert das so:

```
public function addAccessoriesAction()
    {
        $this->basket->sDeleteBasket();
        $this->addAccessories(
            $this->Request()->getParam('sAddAccessories'),
            $this->Request()->getParam('sAddAccessoriesQuantity')
        );

        $this->forward($this->Request()->getParam('sTargetAction', 'cart'));
    }

```

Im Plugin funktioniert das leider nicht:&nbsp;

```
public function onPreDispatchCheckout(\Enlight_Event_EventArgs $args)
     {
       /** @var \Enlight_Controller_Action $controller */
       $controller = $args->getSubject();

       /** @var \Enlight_Controller_Request_Request $request */
       $request = $controller->Request();

       if ($request->getActionName() !== 'addAccessories') {
          return;
       }

       $this->basket->sDeleteBasket();

    }

```

Es werden so&nbsp;keine neuen Artikel hinzugefügt und keine gelöscht.

Denke der Fehler kann nur marginal sein ![Undecided](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/undecided.png "Undecided")&nbsp;Vielen Dank im Voraus!
