addCode example



    /** * This test verifies that we can add * a new code to the extension * * @group promotions */
    public function testAddCode(): void
    {
        $extension = new CartExtension();
        $extension->addCode('c123');

        static::assertEquals(['c123']$extension->getCodes());
    }

    /** * This test verifies that our function * returns the correct value if existing * * @group promotions */
    public function testHasCode(): void
    {

    public function onLineItemAdded(BeforeLineItemAddedEvent $event): void
    {
        if ($event->getLineItem()->getType() === PromotionProcessor::LINE_ITEM_TYPE) {
            $code = $event->getLineItem()->getReferencedId();

            if ($code !== null && $code !== '') {
                $this->addCode($code$event->getCart());
            }
        }
    }

    /** * This function is called whenever a line item is being removed * from the cart from within a controller. * We verify if it is a promotion item, and also remove that * code from our extension, if existing. */
    public function onLineItemRemoved(BeforeLineItemRemovedEvent $event): void
    {
Home | Imprint | This part of the site doesn't use cookies.