Clearing the Cart | Building a Shopping Cart with Symfony
Tags: php, symfony, tutorial, form
Published: 2020-12-21 12:56:28

- [Creating the Event Subscriber](#event-subscriber)
- [Registering the Event Subscriber](#register-event-subscriber)

In the same way as the *Remove* button of the cart, we will use the form events to manage the *Clear* button of the cart.

Creating the Event Subscriber <a name="event-subscriber"></a>
-----------------------------

Create a `ClearCartListener` subscriber and subscribe to the `FormEvents::POST_SUBMIT` event:

```php
<?php

namespace App\Form\EventListener;

use App\Entity\Order;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class ClearCartListener implements EventSubscriberInterface
{
    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents(): array
    {
        return [FormEvents::POST_SUBMIT => 'postSubmit'];
    }

    /**
     * Removes all items from the cart when the clear button is clicked.
     *
     * @param FormEvent $event
     */
    public function postSubmit(FormEvent $event): void
    {
        $form = $event->getForm();
        $cart = $form->getData();

        if (!$cart instanceof Order) {
            return;
        }

        // Is the clear button clicked?
        if (!$form->get('clear')->isClicked()) {
            return;
        }

        // Clears the cart
        $cart->removeItems();
    }
}
```

The current cart is available from the main form by using the `FormEvent::getData()` method. It should be an instance of an `Order` entity.

If the *Clear* button has been clicked, we remove all items from the cart. In other words, we clear the cart.

Registering the Event Subscriber <a name="register-event-subscriber"></a>
--------------------------------

To register the `CleartCartListener` subscriber to the `CartType` form, use the `addEventListener` method using the form builder:

```php
<?php

namespace App\Form;

use App\Entity\Order;
use App\Form\EventListener\ClearCartListener;
use App\Form\EventListener\RemoveItemListener;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CartType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('items', CollectionType::class, [
                'entry_type' => CartItemType::class
            ])
            ->add('save', SubmitType::class)
            ->add('clear', SubmitType::class);

        $builder->addEventSubscriber(new RemoveItemListener());
        $builder->addEventSubscriber(new ClearCartListener());
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Order::class,
        ]);
    }
}
```

Of course, you can merge the two listeners into one but the goal was to show you how to use the form events to dynamically modify form data.

At this point, we have managed all the cart features, but there is still some work to be done. Let's go to the next step, it's almost done.