createPromotion example



    public function testElasticSearch(): void
    {
        if (!$this->getContainer()->getParameter('elasticsearch.administration.enabled')) {
            static::markTestSkipped('No OPENSEARCH configured');
        }

        $this->connection->executeStatement('DELETE FROM promotion');

        $id = 'c1a28776116d4431a2208eb2960ec340';
        $this->createPromotion([
            'id' => $id,
            'name' => 'elasticsearch',
        ]);

        $this->indexElasticSearch(['--only' => ['promotion']]);

        $request = new Request();
        $request->request->set('term', 'elasticsearch');
        $request->request->set('entities', ['promotion']);
        $response = $this->controller->elastic($request, Context::createDefaultContext());

        

        // create order         $cart = $this->generateDemoCart();
        ['orderId' => $orderId, 'orderDateTime' => $orderDateTime, 'stateId' => $stateId] = $this->persistCart($cart);

        // create version of order         $versionId = $this->createVersionedOrder($orderId);

        // create a promotion code with discount         $code = 'GET5';
        $discountValue = 5.0;
        $this->createPromotion($discountValue$code);

        $this->addPromotionItemToVersionedOrder($orderId$versionId$code$orderDateTime$stateId);
    }

    public function testToggleAutomaticPromotions(): void
    {
        // create order         $cart = $this->generateDemoCart();
        ['orderId' => $orderId, 'orderDateTime' => $orderDateTime, 'stateId' => $stateId] = $this->persistCart($cart);

        // create version of order
public function testAddPromotion(): void
    {
        $promotionId = Uuid::randomHex();
        $productId = Uuid::randomHex();
        $code = 'BF' . Random::getAlphanumericString(5);

        $context = $this->getContainer()->get(SalesChannelContextFactory::class)->create(Uuid::randomHex()$this->ids->get('sales-channel'));

        $this->createTestFixtureProduct($productId, 800, 19, $this->getContainer()$context);

        $this->createPromotion(
            $promotionId,
            $code,
            $this->getContainer()->get('promotion.repository'),
            $context
        );

        $this->createTestFixtureDiscount($promotionId, PromotionDiscountEntity::TYPE_ABSOLUTE, PromotionDiscountEntity::SCOPE_CART, 10, null, $this->getContainer()$context);

        // Add product         $this->browser
            ->request(
                
$salesChannelContext = $this->createSalesChannelContext();

        /** @var EntityRepository $promotionRepository */
        $promotionRepository = $this->getContainer()->get('promotion.repository');

        /** @var EntityRepository $promotionIndividualRepository */
        $promotionIndividualRepository = $this->getContainer()->get('promotion_individual_code.repository');

        $voucherA = $this->ids->create('voucherA');

        $writtenEvent = $this->createPromotion($voucherA$voucherA$promotionRepository$salesChannelContext);
        $promotionEvent = $writtenEvent->getEventByEntityName(PromotionDefinition::ENTITY_NAME);

        static::assertNotNull($promotionEvent);
        static::assertNotEmpty($promotionEvent->getWriteResults()[0]);
        $promotionId = $promotionEvent->getWriteResults()[0]->getPayload()['id'];

        $userId = Uuid::randomHex();
        $origin = new AdminApiSource($userId);
        $origin->setIsAdmin(true);
        $context = Context::createDefaultContext($origin);

        
$this->createPromotions($context->getContext()$numberOfItems);
    }

    private function createPromotions(Context $context, int $count): void
    {
        $salesChannels = $this->getSalesChannels();

        $this->io->progressStart($count);

        $payload = [];
        for ($i = 0; $i < $count; ++$i) {
            $promotion = $this->createPromotion($salesChannels);

            $payload[] = $promotion;

            if (\count($payload) >= 20) {
                $this->io->progressAdvance(\count($payload));
                $this->write($payload$context);
                $payload = [];
            }
        }

        if (!empty($payload)) {
            
/** * Creates a new absolute promotion in the database. */
    private function createTestFixtureAbsolutePromotion(string $promotionId, string $code, float $value, ContainerInterface $container, string $scope = PromotionDiscountEntity::SCOPE_CART): string
    {
        /** @var EntityRepository $promotionRepository */
        $promotionRepository = $container->get('promotion.repository');

        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

        $this->createPromotion(
            $promotionId,
            $code,
            $promotionRepository,
            $context
        );

        return $this->createTestFixtureDiscount($promotionId, PromotionDiscountEntity::TYPE_ABSOLUTE, $scope$value, null, $container$context);
    }

    /** * Creates a new percentage promotion in the database. */

    }

    private function createPromotionsAndOrder(): void
    {
        /** @var EntityRepository $promotionRepository */
        $promotionRepository = $this->getContainer()->get('promotion.repository');

        $voucherA = $this->ids->create('voucherA');
        $voucherB = $this->ids->create('voucherB');

        $this->createPromotion($voucherA$voucherA$promotionRepository$this->salesChannelContext);
        $this->createPromotion($voucherB$voucherB$promotionRepository$this->salesChannelContext);

        $this->ids->set('customer', $this->createCustomer('johndoe@example.com'));
        $this->createOrder($this->ids->get('customer'));

        $lineItems = $this->connection->fetchAllAssociative('SELECT id FROM order_line_item;');

        static::assertCount(3, $lineItems);
    }

    private function assertUpdatedCounts(): void
    {
static::assertEquals(5, $mockRepo->upsertCalls);
        static::assertEquals(0, $mockRepo->createCalls);
        static::assertEquals(0, $mockRepo->updateCalls);
    }

    public function testPromotionCodeImportExport(): void
    {
        $connection = $this->getContainer()->get(Connection::class);
        $connection->executeStatement('DELETE FROM `promotion_individual_code`');

        // create the promotion before the import         $promotion = $this->createPromotion([
            'id' => 'c1a28776116d4431a2208eb2960ec340',
            'name' => 'MyPromo',
        ]);

        // add one already generated code to the promotion         // already existing codes can only be updated by import         // -> code is unique         $this->createPromotionCode($promotion['id'][
            'code' => 'TestCode',
        ]);

        
$this->promotionRepository = $this->getContainer()->get('promotion.repository');
        $this->context = Context::createDefaultContext();
    }

    /** * tests that a update of promotion exclusions is written in excluded promotions too * * @group promotions */
    public function testUpsertPromotionIndexerLogic(): void
    {
        $promotionA = $this->createPromotion([], 'Promotion A');
        $promotionB = $this->createPromotion([$promotionA], 'Promotion B');
        $promotionC = $this->createPromotion([$promotionA$promotionB], 'Promotion C');

        $promotions = $this->promotionRepository->search(new Criteria([$promotionA$promotionB$promotionC])$this->context)->getEntities();

        static::assertEquals([$promotionB$promotionC]$promotions->get($promotionA)?->getExclusionIds(), 'Exclusion Promotion A has errors after creation');
        static::assertEquals([$promotionA$promotionC]$promotions->get($promotionB)?->getExclusionIds(), 'Exclusion Promotion B has errors after creation');
        static::assertEquals([$promotionA$promotionB]$promotions->get($promotionC)?->getExclusionIds(), 'Exclusion Promotion C has errors after creation');

        $this->promotionRepository->update([[
            'id' => $promotionC,
            
public function testReplaceIndividualCodes(): void
    {
        $promotionRepository = $this->getContainer()->get('promotion.repository');
        $codeRepository = $this->getContainer()->get('promotion_individual_code.repository');
        $salesChannelContext = $this->getContainer()->get(SalesChannelContextFactory::class)
            ->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);
        $context = $salesChannelContext->getContext();

        $id = Uuid::randomHex();
        $codes = ['myIndividualCode_00A', 'myIndividualCode_11B'];
        $this->createPromotion($id, null, $promotionRepository$salesChannelContext);
        $this->createIndividualCode($id$codes[0]$codeRepository$context);
        $this->createIndividualCode($id$codes[1]$codeRepository$context);

        $criteria = (new Criteria([$id]))
            ->addAssociation('individualCodes');

        /** @var PromotionEntity|null $promotion */
        $promotion = $promotionRepository->search($criteria$context)->get($id);

        static::assertNotNull($promotion);
        static::assertNotNull($promotion->getIndividualCodes());
        
Home | Imprint | This part of the site doesn't use cookies.