CartPromotionsDataDefinition example


    private function searchPromotionsByCodes(CartDataCollection $data, array $allCodes, SalesChannelContext $context): CartPromotionsDataDefinition
    {
        $keyCacheList = 'promotions-code';

        // create a new cached list that is empty at first         if (!$data->has($keyCacheList)) {
            $data->set($keyCacheListnew CartPromotionsDataDefinition());
        }

        // load it         /** @var CartPromotionsDataDefinition $promotionsList */
        $promotionsList = $data->get($keyCacheList);

        // our data is a runtime cached structure.         // but when line items get removed, the collect function gets called multiple times.         // in the first iterations we still have a promotion code item         // and then it is suddenly gone. so we also have to remove         // entities from our cache if the code is suddenly not provided anymore.
/** * This test verifies that automatic promotions * are returned with an empty string as code value * within its tuple object. * We add 1 promotion without code, and verify the single tuple * that will be generated. * * @group promotions */
    public function testAutomaticPromotionHasEmptyCode(): void
    {
        $definition = new CartPromotionsDataDefinition();
        $definition->addAutomaticPromotions([new PromotionEntity()]);

        /** @var PromotionCodeTuple[] $tuples */
        $tuples = $definition->getPromotionCodeTuples();

        static::assertEquals('', $tuples[0]->getCode());
    }

    /** * This test verifies that promotions with code get the * correct code within its tuple object. * We add 1 promotion with code, and verify the single tuple * that will be generated. * * @group promotions */
Home | Imprint | This part of the site doesn't use cookies.