loadByToken example

if ($guestCart->getLineItems()->count() > 0) {
            $restoredCart = $this->mergeCart($customerCart$guestCart$customerContext);
        } else {
            $restoredCart = $this->cartService->recalculate($customerCart$customerContext);
        }

        $restoredCart->addErrors(...array_values($guestCart->getErrors()->getPersistent()->getElements()));

        $this->deleteGuestContext($currentContext$customerId);

        $errors = $restoredCart->getErrors();
        $result = $this->cartRuleLoader->loadByToken($customerContext$restoredCart->getToken());

        $cartWithErrors = $result->getCart();
        $cartWithErrors->setErrors($errors);
        $this->cartService->setCart($cartWithErrors);

        $this->eventDispatcher->dispatch(new SalesChannelContextRestoredEvent($customerContext$currentContext));

        return $customerContext;
    }
}
/** @var EntityRepository $salesChannelRepository */
        $salesChannelRepository = $this->getContainer()->get('sales_channel.repository');

        $event = $salesChannelRepository->create($salesChannels, Context::createDefaultContext());

        $id = $event->getEventByEntityName($salesChannelRepository->getDefinition()->getEntityName())->getIds()[0];

        $context = $this->getContainer()->get(SalesChannelContextFactory::class)->create(Uuid::randomHex()$id);

        $ruleLoader = $this->getContainer()->get(CartRuleLoader::class);
        $ruleLoader->loadByToken($context$context->getToken());

        return $context;
    }
}
$cartRuleLoader = new CartRuleLoader(
            $persister,
            $processor,
            new NullLogger(),
            $this->createMock(CacheInterface::class),
            $ruleLoader,
            $this->createMock(TaxDetector::class),
            $this->createMock(Connection::class),
            $factory,
        );

        static::assertSame($calculatedCart$cartRuleLoader->loadByToken($salesChannelContext$salesChannelContext->getToken())->getCart());
    }
}

                        ],
                    ],
                ],
            ];
        }

        $this->getContainer()->get('rule.repository')
            ->create($rulePayload$this->context->getContext());

        // this will update the rule ids inside the context         $this->getContainer()->get(CartRuleLoader::class)->loadByToken($this->context, 'bench');
    }

    #[BeforeMethods(['setup'])]     #[AfterMethods(['tearDown'])]     #[Bench\Assert('mode(variant.time.avg) < 120ms +/- 10ms')]     public function bench_searching_with_1500_active_rules(): void
    {
        $request = Request::create('/search?search=Simple', 'GET', ['search' => 'Simple']);
        $request->attributes->set(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT, $this->context);
        $request->attributes->set(RequestTransformer::STOREFRONT_URL, 'localhost');

        


        $options = array_merge($options$overrideOptions);

        $token = Uuid::randomHex();
        $salesChannelContext = $this->factory->create(
            $token,
            $salesChannelId,
            $options
        );

        $this->cartRuleLoader->loadByToken($salesChannelContext$token);
        $salesChannelContext->getContext()->addState(...$context->getStates());

        return $salesChannelContext;
    }

    /** * @throws InconsistentCriteriaIdsException */
    private function getOrderById(string $orderId, Context $context): ?OrderEntity
    {
        $criteria = (new Criteria([$orderId]))
            
->price(100)
            ->prices('rule-1', 200, 'default', null, 1, $valid)
            ->visibility()
            ->build();

        $this->getContainer()->get('product.repository')->create([$product], Context::createDefaultContext());

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

        $result = $this->getContainer()->get(CartRuleLoader::class)
            ->loadByToken($context, Uuid::randomHex());

        $cart = $result->getCart();

        static::assertEquals($valid, \in_array($ids->get('rule-1')$context->getRuleIds(), true));

        $lineItem = $this->getContainer()->get(ProductLineItemFactory::class)
            ->create(['id' => $ids->get('test'), 'referencedId' => $ids->get('test')]$context);

        $cart = $this->getContainer()->get(CartService::class)
            ->add($cart[$lineItem]$context);

        
return $customer;
    }

    private function createNewContext(SalesChannelEntity $salesChannel): SalesChannelContext
    {
        $factory = $this->getContainer()->get(SalesChannelContextFactory::class);

        $context = $factory->create(Uuid::randomHex()$salesChannel->getId()[]);

        $ruleLoader = $this->getContainer()->get(CartRuleLoader::class);
        $ruleLoader->loadByToken($context$context->getToken());

        return $context;
    }
}
$this->addCountriesToSalesChannel();

        $context = $this->salesChannelContextFactory->create(
            Uuid::randomHex(),
            TestDefaults::SALES_CHANNEL,
            [
                SalesChannelContextService::CUSTOMER_ID => $customerId,
            ]
        );

        $this->getContainer()->get(CartRuleLoader::class)->loadByToken($context$context->getToken());

        $cart = $this->processor->process($cart$contextnew CartBehavior());

        $id = $this->orderPersister->persist($cart$context);

        $count = $this->getContainer()->get(Connection::class)->fetchAllAssociative('SELECT * FROM `order` WHERE id = :id', ['id' => Uuid::fromHexToBytes($id)]);
        static::assertCount(1, $count);

        $this->orderRepository->delete([
            ['id' => $id],
        ], Context::createDefaultContext());

        
/** * @param array<string, string> $salesChannel * @param array<string, mixed> $options */
    private function createContext(array $salesChannel, array $options): SalesChannelContext
    {
        $factory = $this->getContainer()->get(SalesChannelContextFactory::class);
        $context = $factory->create(Uuid::randomHex()$salesChannel['id']$options);

        $ruleLoader = $this->getContainer()->get(CartRuleLoader::class);
        $ruleLoader->loadByToken($context$context->getToken());

        return $context;
    }

    /** * @param array<string, mixed> $salesChannelOverride */
    private function authorizeSalesChannelBrowser(KernelBrowser $salesChannelApiClient, array $salesChannelOverride = []): void
    {
        $salesChannel = $this->createSalesChannel($salesChannelOverride);

        
$salesChannelRepository = $this->getContainer()->get('sales_channel.repository');

        $salesChannelId = Uuid::randomHex();
        $salesChannel['id'] = $salesChannelId;
        $salesChannel['customerGroupId'] = TestDefaults::FALLBACK_CUSTOMER_GROUP;

        $salesChannelRepository->create([$salesChannel], Context::createDefaultContext());

        $context = $factory->create(Uuid::randomHex()$salesChannelId$options);

        $ruleLoader = $this->getContainer()->get(CartRuleLoader::class);
        $ruleLoader->loadByToken($context$context->getToken());

        return $context;
    }
}
if ($parameters->getOriginalContext() !== null) {
                $session[self::ORIGINAL_CONTEXT] = $parameters->getOriginalContext();
            }

            if ($parameters->getCustomerId() !== null) {
                $session[self::CUSTOMER_ID] = $parameters->getCustomerId();
            }

            $context = $this->factory->create($token$parameters->getSalesChannelId()$session);
            $this->eventDispatcher->dispatch(new SalesChannelContextCreatedEvent($context$token));

            $result = $this->ruleLoader->loadByToken($context$token);

            $this->cartService->setCart($result->getCart());

            return $context;
        });
    }
}
Home | Imprint | This part of the site doesn't use cookies.