CartBeforeSerializationEvent example


class CartBeforeSerializationEventTest extends TestCase
{
    public function testConstructor(): void
    {
        $cart = new Cart('cart');
        $customFieldAllowList = ['foo', 'bar'];

        $event = new CartBeforeSerializationEvent($cart$customFieldAllowList);

        static::assertSame($cart$event->getCart());
        static::assertSame($customFieldAllowList$event->getCustomFieldAllowList());
    }

    public function testSetCustomFieldAllowList(): void
    {
        $customFieldAllowList = ['foo', 'bar'];

        $event = new CartBeforeSerializationEvent(new Cart('cart')$customFieldAllowList);
        $event->setCustomFieldAllowList(['boo']);

        

    public function __construct(
        private readonly Connection $connection,
        private readonly EventDispatcherInterface $eventDispatcher
    ) {
    }

    public function cleanupCart(Cart $cart): void
    {
        $customFieldAllowList = $this->connection->fetchFirstColumn('SELECT `name` FROM `custom_field` WHERE `allow_cart_expose` = 1;');

        $event = new CartBeforeSerializationEvent($cart$customFieldAllowList);
        $this->eventDispatcher->dispatch($event);

        $this->cleanupLineItems($cart->getLineItems()$event->getCustomFieldAllowList());

        foreach ($cart->getDeliveries() as $delivery) {
            $this->cleanupLineItems($delivery->getPositions()->getLineItems()$event->getCustomFieldAllowList());
        }
    }

    /** * @param array<mixed> $customFieldAllowList */
Home | Imprint | This part of the site doesn't use cookies.