getProduct example

$slot->setUniqueIdentifier('id');
        $slot->setType('');
        $slot->setFieldConfig($fieldConfig);

        $this->boxCmsElementResolver->enrich($slot$resolverContext$result);

        /** @var ProductBoxStruct|null $productBoxStruct */
        $productBoxStruct = $slot->getData();

        static::assertInstanceOf(ProductBoxStruct::class$productBoxStruct);

        $product = $productBoxStruct->getProduct();
        static::assertNull($product);
    }

    public function testEnrichWithProductConfigIsMapped(): void
    {
        $productId = Uuid::randomHex();
        $salesChannelContext = $this->createMock(SalesChannelContext::class);
        $product = new SalesChannelProductEntity();
        $product->setId($productId);
        $product->setUniqueIdentifier('product1');

        
$slot = new CmsSlotEntity();
        $slot->setUniqueIdentifier('id');
        $slot->setType('product-box');
        $slot->setFieldConfig(new FieldConfigCollection());

        $this->productBoxResolver->enrich($slot$resolverContext$result);

        /** @var ProductBoxStruct|null $productBoxStruct */
        $productBoxStruct = $slot->getData();
        static::assertInstanceOf(ProductBoxStruct::class$productBoxStruct);
        static::assertNull($productBoxStruct->getProductId());
        static::assertNull($productBoxStruct->getProduct());
    }

    /** * @dataProvider EnrichWithStaticConfigProvider */
    public function testEnrichWithStaticConfig(bool $closeout, bool $hidden, int $availableStock): void
    {
        if ($hidden) {
            $this->systemConfig->method('get')->willReturn(true);
        }

        

        }

        $slots = $page->getElementsOfType('product-box');
        /** @var CmsSlotEntity $slot */
        foreach ($slots as $slot) {
            $box = $slot->getData();

            if (!$box instanceof ProductBoxStruct) {
                continue;
            }
            if ($box->getProduct() === null) {
                continue;
            }

            $ids[] = $box->getProduct()->getId();
            $ids[] = $box->getProduct()->getParentId();
        }

        $ids = array_values(array_unique(array_filter($ids)));

        return [...array_map(EntityCacheKeyGenerator::buildProductTag(...)$ids), ...array_map(EntityCacheKeyGenerator::buildStreamTag(...)$streamIds), ...[EntityCacheKeyGenerator::buildCmsTag($page->getId())]];
    }
}
static::expectExceptionObject(new PageNotFoundException($landingPageId));
        $landingPageLoader->load($request$salesChannelContext);
    }

    public function testItLoads(): void
    {
        $productId = Uuid::randomHex();
        $landingPageId = Uuid::randomHex();
        $request = new Request([][]['landingPageId' => $landingPageId]);
        $salesChannelContext = $this->getSalesChannelContext();

        $product = $this->getProduct($productId);
        $cmsPage = $this->getCmsPage($product);

        $landingPageLoader = $this->getLandingPageLoaderWithProduct($landingPageId$cmsPage$request$salesChannelContext);

        $page = $landingPageLoader->load($request$salesChannelContext);

        /** @phpstan-ignore-next-line */
        $cmsPageLoaded = $page->getLandingPage()->getCmsPage();

        static::assertEquals($cmsPage$cmsPageLoaded);
    }

    

#[Package('checkout')] class CustomerWishlistProductCollection extends EntityCollection
{
    public function getApiAlias(): string
    {
        return 'customer_wishlist_product_collection';
    }

    public function getProducts(): ?ProductCollection
    {
        return new ProductCollection($this->fmap(fn (CustomerWishlistProductEntity $wishlistProductEntity) => $wishlistProductEntity->getProduct()));
    }

    public function getByProductId(string $productId): ?CustomerWishlistProductEntity
    {
        return $this->filterByProperty('productId', $productId)->first();
    }

    protected function getExpectedClass(): string
    {
        return CustomerWishlistProductEntity::class;
    }
}
$orderRepo = $this->getContainer()->get('order.repository');
        $orderRepo->create($orderData$context);

        $browser->request('GET', $_SERVER['APP_URL'] . '/widgets/account/order/detail/' . $orderId);
        /** @var StorefrontResponse $response */
        $response = $browser->getResponse();

        /** @var OrderLineItemCollection $orderLineItemCollection */
        $orderLineItemCollection = $response->getData()['orderDetails'];

        foreach ($orderLineItemCollection as $orderLineItemEntity) {
            static::assertNull($orderLineItemEntity->getProduct());
        }

        $eventDispatcher = $this->getContainer()->get('event_dispatcher');
        $eventDispatcher->addListener(OrderRouteRequestEvent::classstatic function DOrderRouteRequestEvent $event): void {
            $event->getCriteria()->addAssociation('lineItems.product');
        });

        $browser->request('GET', $_SERVER['APP_URL'] . '/widgets/account/order/detail/' . $orderId);
        /** @var StorefrontResponse $response */
        $response = $browser->getResponse();

        
array_values($words)
        );
    }

    /** * @dataProvider casesSearchBaseOnConfigField */
    public function testInsertIntoSearchKeywordForEn(bool $searchable, bool $tokenize, int $ranking): void
    {
        $this->updateProductSearchConfigField($this->enSearchConfigId, $searchable$tokenize$ranking);

        $product = $this->getProduct();
        $configFields = $this->getConfigFieldsByLanguageId($this->enSearchConfigId);
        $analyzer = $this->getContainer()->get(ProductSearchKeywordAnalyzer::class);
        $analyzer = $analyzer->analyze($product$this->context, $configFields);
        $analyzerResult = $analyzer->getKeys();
        sort($analyzerResult);

        $expected = [];
        if ($searchable && $tokenize) {
            $expected = [
                'test',
                'product',
                
1,
                    new ProductCollection([$productEntity]),
                    null,
                    new Criteria(),
                    $this->context->getContext()
                )
            );

        $result = $this->route->load('1', new Request()$this->context, new Criteria());

        static::assertInstanceOf(ProductDetailRouteResponse::class$result);
        static::assertEquals('4', $result->getProduct()->getCmsPageId());
        static::assertEquals('mainVariant', $result->getProduct()->getUniqueIdentifier());
    }

    public function testLoadBestVariant(): void
    {
        $productEntity = new SalesChannelProductEntity();
        $productEntity->setCmsPageId('4');
        $productEntity->setId($this->idsCollection->create('product1'));
        $productEntity->setUniqueIdentifier('BestVariant');

        $idsSearchResult = new IdSearchResult(
            
$product = $this->getRandomProduct($context);

        $request = new Request([][]['productId' => $product->getId()]);

        /** @var ProductPageLoadedEvent $event */
        $event = null;
        $this->catchEvent(ProductPageLoadedEvent::class$event);

        $page = $this->getPageLoader()->load($request$context);

        static::assertInstanceOf(ProductPage::class$page);
        static::assertSame(StorefrontPageTestConstants::PRODUCT_NAME, $page->getProduct()->getName());
        self::assertPageEvent(ProductPageLoadedEvent::class$event$context$request$page);
    }

    public function testItDispatchPageCriteriaEvent(): void
    {
        $context = $this->createSalesChannelContextWithNavigation();
        $product = $this->getRandomProduct($context);

        $request = new Request([][]['productId' => $product->getId()]);

        $page = $this->getPageLoader()->load($request$context);

        
$this->productEntity->setId('test');
        $this->productPage = new ProductPage();
        $this->productPage->setProduct($this->productEntity);
        $this->productPage->setCmsPage(new CmsPageEntity());

        $this->productPageLoaderMock->method('load')->willReturn($this->productPage);

        $response = $this->controller->index($this->createMock(SalesChannelContext::class)new Request());

        static::assertEquals(Response::HTTP_OK, $response->getStatusCode());
        static::assertInstanceOf(ProductPage::class$this->controller->renderStorefrontParameters['page']);
        static::assertEquals('test', $this->controller->renderStorefrontParameters['page']->getProduct()->getId());
        static::assertEquals('@Storefront/storefront/page/content/product-detail.html.twig', $this->controller->renderStorefrontView);
    }

    public function testIndexNoCmsPage(): void
    {
        Feature::skipTestIfActive('v6.5.0.0', $this);

        $this->productEntity = new SalesChannelProductEntity();
        $this->productEntity->setId('test');
        $this->productPage = new ProductPage();
        $this->productPage->setProduct($this->productEntity);

        
$result = new ElementDataCollection();

        $slot = new CmsSlotEntity();
        $slot->setUniqueIdentifier('id');
        $slot->setType('product-description-reviews');

        $this->productDescriptionReviewResolver->enrich($slot$resolverContext$result);

        /** @var ProductDescriptionReviewsStruct|null $productDescriptionReviewStruct */
        $productDescriptionReviewStruct = $slot->getData();
        static::assertInstanceOf(ProductDescriptionReviewsStruct::class$productDescriptionReviewStruct);
        static::assertNull($productDescriptionReviewStruct->getProduct());
    }
}
$criteria = (new Criteria())
            ->addAssociation('manufacturer.media')
            ->addAssociation('options.group')
            ->addAssociation('properties.group')
            ->addAssociation('mainCategories.category')
            ->addAssociation('media');

        $this->eventDispatcher->dispatch(new ProductPageCriteriaEvent($productId$criteria$context));

        $result = $this->productDetailRoute->load($productId$request$context$criteria);
        $product = $result->getProduct();

        if ($product->getMedia()) {
            $product->getMedia()->sort(fn (ProductMediaEntity $a, ProductMediaEntity $b) => $a->getPosition() <=> $b->getPosition());
        }

        if ($product->getMedia() && $product->getCover()) {
            $product->setMedia(new ProductMediaCollection(array_merge(
                [$product->getCover()->getId() => $product->getCover()],
                $product->getMedia()->getElements()
            )));
        }

        
$slot = new CmsSlotEntity();
        $slot->setUniqueIdentifier('id');
        $slot->setType('buy-box');
        $slot->setFieldConfig(new FieldConfigCollection());

        $this->buyBoxResolver->enrich($slot$resolverContext$result);

        /** @var BuyBoxStruct|null $buyBoxStruct */
        $buyBoxStruct = $slot->getData();
        static::assertInstanceOf(BuyBoxStruct::class$buyBoxStruct);
        static::assertNull($buyBoxStruct->getProductId());
        static::assertNull($buyBoxStruct->getProduct());
    }

    public function testEnrichWithStaticConfig(): void
    {
        $product = new SalesChannelProductEntity();
        $product->setId('product123');

        $resolverContext = new ResolverContext($this->createMock(SalesChannelContext::class)new Request());
        $result = new ElementDataCollection();
        $result->add('product_id', new EntitySearchResult(
            'product',
            

                    'switchedGroup' => $request->query->get('switched'),
                    'options' => $options ?? [],
                ]
            ),
            $context
        );

        $newProductId = $variantResponse->getFoundCombination()->getVariantId();

        $result = $this->productRoute->load($newProductId$request$contextnew Criteria());
        $product = $result->getProduct();
        $configurator = $result->getConfigurator();

        $request->request->set('parentId', $product->getParentId());
        $request->request->set('productId', $product->getId());
        $reviews = $this->productReviewLoader->load($request$context);
        $reviews->setParentId($product->getParentId() ?? $product->getId());

        $event = new SwitchBuyBoxVariantEvent($elementId$product$configurator$request$context);
        $this->eventDispatcher->dispatch($event);

        $response = $this->renderStorefront('@Storefront/storefront/component/buy-widget/buy-widget.html.twig', [
            
$this->aggregator->aggregate($this->getContainer()->get(TaxDefinition::class)$criteria$context);
    }

    private function insertData(): void
    {
        $repository = $this->getContainer()->get('product.repository');

        $this->ids = new TestDataCollection();

        $repository->create([
            $this->getProduct('p-1', 't-1', 'm-1', 50, ['c-1', 'c-2'], '2019-01-01 10:11:00'),
            $this->getProduct('p-2', 't-1', 'm-2', 100, ['c-1'], '2019-01-01 10:13:00'),
            $this->getProduct('p-3', 't-2', 'm-2', 150, ['c-1', 'c-3'], '2019-06-15 13:00:00'),
            $this->getProduct('p-4', 't-2', 'm-2', 200, ['c-3'], '2020-09-30 15:00:00'),
            $this->getProduct('p-5', 't-3', 'm-3', 250, [], '2021-12-10 11:59:00'),
            $this->getProduct('p-6', 't-3', 'm-3', 250, [], '2024-12-11 23:59:00'),
        ], Context::createDefaultContext());
    }

    /** * @param list<string> $categoryKeys * * @return array<string, mixed> */
Home | Imprint | This part of the site doesn't use cookies.