getReferencedId example

private SalesChannelContext $context;

    protected function setUp(): void
    {
        $this->service = $this->getContainer()->get(LineItemFactoryRegistry::class);
        $this->context = $this->getContainer()->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);
    }

    public function testCreateProduct(): void
    {
        $lineItem = $this->service->create(['type' => 'product', 'referencedId' => 'test']$this->context);
        static::assertSame('test', $lineItem->getReferencedId());
        static::assertSame(LineItem::PRODUCT_LINE_ITEM_TYPE, $lineItem->getType());
        static::assertSame(1, $lineItem->getQuantity());
    }

    public function testCreateProductWithPriceDefinition(): void
    {
        $this->expectException(CartException::class);

        $this->service->create([
            'type' => 'product',
            'referencedId' => 'test',
            
/** * in case of a delivery discount we add a 0.0 lineItem just to show customers and * shop owners, that delivery costs have been discounted by a promotion discount * if promotion is a auto promotion (no code) it may not be removed from cart * * @throws CartException */
    public function buildDeliveryPlaceholderLineItem(LineItem $discount, QuantityPriceDefinition $priceDefinition, CalculatedPrice $price): LineItem
    {
        $mayRemove = true;
        if ($discount->getReferencedId() === null) {
            $mayRemove = false;
        }
        // create a fake lineItem that stores our promotion code         $promotionItem = new LineItem($discount->getId(), PromotionProcessor::LINE_ITEM_TYPE, $discount->getReferencedId(), 1);
        $promotionItem->setLabel($discount->getLabel());
        $promotionItem->setDescription($discount->getLabel());
        $promotionItem->setGood(false);
        $promotionItem->setRemovable($mayRemove);
        $promotionItem->setPayload($discount->getPayload());
        $promotionItem->setPriceDefinition($priceDefinition);
        $promotionItem->setPrice($price);

        
return $cart;
    }

    /** * Removes the provided code to the current cart. */
    public function removePromotionCode(string $code, Cart $cart, CartService $cartService, SalesChannelContext $context): Cart
    {
        $promotions = $cart->getLineItems()->filterType(PromotionProcessor::LINE_ITEM_TYPE);

        foreach ($promotions->getElements() as $promotion) {
            if ($promotion->getReferencedId() === $code) {
                return $cartService->remove($cart$promotion->getId()$context);
            }
        }

        return $cart;
    }

    /** * Gets all promotion codes that have been added * to the current session. * * @return array<mixed> */
$service->calculate();

        $item = $service->products()->get($this->ids->get($input));

        if ($expected === null) {
            static::assertNull($item);

            return;
        }

        static::assertInstanceOf(ItemFacade::class$item);
        static::assertEquals($this->ids->get($expected)$item->getReferencedId());
        static::assertEquals(LineItem::PRODUCT_LINE_ITEM_TYPE, $item->getType());
    }

    public function testContainer(): void
    {
        $context = $this->getContainer()->get(SalesChannelContextFactory::class)
            ->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL, []);

        $hook = new CartHook($this->createCart()$context);

        $service = $this->getContainer()->get(CartFacadeHookFactory::class)
            


    /** * This function is called whenever a new line item has been * added to the cart from within the controllers. * We verify if we have a placeholder line item for a promotion * and add that code to our extension list. */
    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. */

        /** @var string $label */
        $label = $item->getLabel();

        /** @var PriceDefinitionInterface $priceDefinition */
        $priceDefinition = $item->getPriceDefinition();

        $discount = new DiscountLineItem(
            $label,
            $priceDefinition,
            $item->getPayload(),
            $item->getReferencedId()
        );

        $packager = match ($discount->getScope()) {
            PromotionDiscountEntity::SCOPE_CART => $this->cartScopeDiscountPackager,
            PromotionDiscountEntity::SCOPE_SET => $this->setScopeDiscountPackager,
            PromotionDiscountEntity::SCOPE_SETGROUP => $this->setGroupScopeDiscountPackager,
            default => throw new InvalidScopeDefinitionException($discount->getScope()),
        };

        $packages = $packager->getMatchingItems($discount$calculatedCart$context);

        
foreach ($this->ids->prefixed('product-state-') as $id) {
            $this->cart->getLineItems()->add(new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE, $id));
        }

        $this->getContainer()->get(ProductCartProcessor::class)->collect($this->cart->getData()$this->cart, $this->context, new CartBehavior());
    }

    #[Bench\BeforeMethods(['setupWithLogin'])]     #[Bench\Assert('mode(variant.time.avg) < 150ms +/- 20ms')]     public function bench_order_10_physical_products(): void
    {
        $this->cart->setLineItems($this->cart->getLineItems()->filter(fn (LineItem $lineItem): bool => \in_array($lineItem->getReferencedId()$this->ids->prefixed('product-state-physical-'), true)));
        $this->getContainer()->get(CartOrderRoute::class)->order($this->cart, $this->context, new RequestDataBag());
    }

    #[Bench\BeforeMethods(['setupWithLogin'])]     #[Bench\Assert('mode(variant.time.avg) < 170ms +/- 20ms')]     public function bench_order_10_digital_products(): void
    {
        $this->cart->setLineItems($this->cart->getLineItems()->filter(fn (LineItem $lineItem): bool => \in_array($lineItem->getReferencedId()$this->ids->prefixed('product-state-digital-'), true)));
        $this->getContainer()->get(CartOrderRoute::class)->order($this->cart, $this->context, new RequestDataBag());
    }
}
        $this->promotionCalculator->calculate($discountItems$original$toCalculate$this->salesChannelContext, new CartBehavior());
        static::assertCount(2, $toCalculate->getLineItems());

        // Make sure that only the expected promotion is in the cart         $promotionLineItems = $toCalculate->getLineItems()->filterType(PromotionProcessor::LINE_ITEM_TYPE);
        static::assertCount(1, $promotionLineItems);

        $promotionItem = $promotionLineItems->first();
        static::assertNotNull($promotionItem);
        static::assertNotNull($promotionItem->getPrice());
        static::assertSame(-10.0, $promotionItem->getPrice()->getTotalPrice());
        static::assertSame($promotionItem->getReferencedId()$validDiscountItem->getReferencedId());
    }

    public function testAutomaticExclusionsDontAddError(): void
    {
        $firstPromotionId = $this->getPromotionId(true, 1, false);
        $discountItemToBeExcluded = $this->getDiscountItem($firstPromotionId);
        $discountItemToBeExcluded->setPayloadValue('preventCombination', true);

        $secondPromotionId = $this->getPromotionId(true, 2, false);
        $validDiscountItem = $this->getDiscountItem($secondPromotionId);
        $validDiscountItem->setPayloadValue('preventCombination', true);

        
$output = [];
        /** @var IdStruct|null $idStruct */
        $idStruct = $lineItem->getExtensionOfType(OrderConverter::ORIGINAL_ID, IdStruct::class);
        if ($idStruct !== null) {
            $id = $idStruct->getId();
        } else {
            $id = Uuid::randomHex();
        }

        $productId = null;
        if ($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
            $productId = $lineItem->getReferencedId();
        }

        $promotionId = null;
        if ($lineItem->getType() === PromotionProcessor::LINE_ITEM_TYPE) {
            $promotionId = $lineItem->getPayloadValue('promotionId');
        }

        $definition = $lineItem->getPriceDefinition();

        $data = [
            'id' => $id,
            
static::assertCount(3, $cart->getLineItems());

        $cart = $cartService->removeItems($cart[
            $productId1,
            $productId2,
        ]$context);

        static::assertCount(1, $cart->getLineItems());

        $remainingLineItem = $cart->getLineItems()->get($productId3);
        static::assertInstanceOf(LineItem::class$remainingLineItem);
        static::assertEquals($productId3$remainingLineItem->getReferencedId());
    }

    public function testZeroPricedItemsCanBeAddedToCart(): void
    {
        $cartService = $this->getContainer()->get(CartService::class);

        $context = $this->getSalesChannelContext();

        $productId = Uuid::randomHex();
        $product = [
            'id' => $productId,
            


            $discountItem = $this->itemBuilder->buildDiscountLineItem(
                $code,
                $promotion,
                $discount,
                $context->getCurrency()->getId(),
                $factor
            );

            $originalCodeItem = $cart->getLineItems()->filter(function DLineItem $item) use ($code) {
                if ($item->getReferencedId() === $code) {
                    return $item;
                }

                return null;
            })->first();

            if ($originalCodeItem && (is_countable($originalCodeItem->getExtensions()) ? \count($originalCodeItem->getExtensions()) : 0) > 0) {
                $discountItem->setExtensions($originalCodeItem->getExtensions());
            }

            $lineItems[] = $discountItem;
        }
static::assertEquals($destinationStateTechnicalName$toStateMachineState->getTechnicalName());

        static::assertEquals($this->getContainer()->get(OrderDefinition::class)->getEntityName()$historyEntry->getEntityName());

        if (!Feature::isActive('v6.6.0.0')) {
            static::assertEquals($orderId$historyEntry->getEntityId()['id']);
            static::assertEquals(Defaults::LIVE_VERSION, $historyEntry->getEntityId()['version_id']);

            return;
        }

        static::assertEquals($orderId$historyEntry->getReferencedId());
        static::assertEquals(Defaults::LIVE_VERSION, $historyEntry->getReferencedVersionId());
    }

    public function testTransitionToNotAllowedState(): void
    {
        $context = Context::createDefaultContext();
        $customerId = $this->createCustomer($context);
        $orderId = $this->createOrder($customerId$context);

        $this->getBrowser()->request('POST', '/api/_action/state-machine/order/' . $orderId . '/state/foo');

        
if ($this->getItems()->has($id->getId())) {
            return true;
        }

        foreach ($this->getItems() as $item) {
            if ($item->getType() !== $id->getType()) {
                continue;
            }

            // same type and same reference id             if ($item->getReferencedId() === $id->getReferencedId()) {
                return true;
            }
        }

        return false;
    }

    private function getItems(): LineItemCollection
    {
        return $this->items;
    }
}
                $products = $this->productGateway->get($ids$context);

                // add products to data collection                 foreach ($products as $product) {
                    $data->set($this->getDataKey($product->getId())$product);
                }

                $hash = $this->generator->getSalesChannelContextHash($context[RuleAreas::PRODUCT_AREA]);

                // refresh data timestamp to prevent unnecessary gateway calls                 foreach ($items as $lineItem) {
                    if (!\in_array($lineItem->getReferencedId()$products->getIds(), true)) {
                        $lineItem->setDataTimestamp(null);

                        continue;
                    }
                    $lineItem->setDataTimestamp(new \DateTimeImmutable());
                    $lineItem->setDataContextHash($hash);
                }
            }

            foreach ($lineItems as $match) {
                // enrich all products in original cart

class ContainerFacadeTest extends TestCase
{
    public function testPublicApiAvailable(): void
    {
        $facade = $this->rampUpFacade();

        static::assertEquals('container', $facade->getType());
        static::assertEquals('container', $facade->getId());
        static::assertEquals('container', $facade->getReferencedId());

        static::assertEquals(1, $facade->getQuantity());
        static::assertTrue($facade->has('foo'));

        $facade->remove('foo');
        static::assertFalse($facade->has('foo'));
        static::assertCount(0, $facade->products());
    }

    public function testAbsoluteDiscount(): void
    {
        
Home | Imprint | This part of the site doesn't use cookies.