getAssignedProducts example

$criteria->addAssociation('crossSellings.assignedProducts');

        /** @var ProductEntity $productA */
        $productA = $this->productRepository->search($criteria, Context::createDefaultContext())->get('cf682b73be1afad47d0f32559ac34627');
        /** @var ProductEntity $productB */
        $productB = $this->productRepository->search($criteria, Context::createDefaultContext())->get('c9a70321b66449abb54ba9306ad02835');

        static::assertInstanceOf(ProductCrossSellingCollection::class$productA->getCrossSellings());
        $aCrossSelling = $productA->getCrossSellings()->first();
        static::assertInstanceOf(ProductCrossSellingEntity::class$aCrossSelling);
        static::assertEquals('Lorem', $aCrossSelling->getName());
        static::assertInstanceOf(ProductCrossSellingAssignedProductsCollection::class$aCrossSelling->getAssignedProducts());
        static::assertCount(3, $aCrossSelling->getAssignedProducts());

        static::assertInstanceOf(ProductCrossSellingCollection::class$productB->getCrossSellings());
        $bCrossSelling = $productB->getCrossSellings()->first();
        static::assertInstanceOf(ProductCrossSellingEntity::class$bCrossSelling);
        static::assertEquals('Ipsum', $bCrossSelling->getName());
        static::assertInstanceOf(ProductCrossSellingAssignedProductsCollection::class$bCrossSelling->getAssignedProducts());
        static::assertCount(3, $bCrossSelling->getAssignedProducts());

        $progress = $this->export($context, ProductCrossSellingDefinition::ENTITY_NAME);

        
return $element;
    }

    private function loadByIds(ProductCrossSellingEntity $crossSelling, SalesChannelContext $context, Criteria $criteria): CrossSellingElement
    {
        $element = new CrossSellingElement();
        $element->setCrossSelling($crossSelling);
        $element->setProducts(new ProductCollection());
        $element->setTotal(0);

        if (!$crossSelling->getAssignedProducts()) {
            return $element;
        }

        $crossSelling->getAssignedProducts()->sortByPosition();

        $ids = array_values($crossSelling->getAssignedProducts()->getProductIds());

        $filter = new ProductAvailableFilter(
            $context->getSalesChannel()->getId(),
            ProductVisibilityDefinition::VISIBILITY_LINK
        );

        
/** @var EntityRepository $assignedProductsRepository */
        $assignedProductsRepository = $this->getContainer()->get('product_cross_selling_assigned_products.repository');
        $productCrossSellingDefinition = $this->getContainer()->get(ProductCrossSellingDefinition::class);

        $serializer = new ProductCrossSellingSerializer($assignedProductsRepository);
        $serializer->setRegistry($this->getContainer()->get(SerializerRegistry::class));

        $serialized = iterator_to_array($serializer->serialize(new Config([][][])$productCrossSellingDefinition$crossSelling));

        static::assertNotEmpty($serialized);

        $assignedProducts = $crossSelling->getAssignedProducts();
        $assignedProducts->sort(fn (ProductCrossSellingAssignedProductsEntity $a, ProductCrossSellingAssignedProductsEntity $b) => $a->getPosition() <=> $b->getPosition());
        $productsIds = $assignedProducts->map(fn (ProductCrossSellingAssignedProductsEntity $assignedProductsEntity) => $assignedProductsEntity->getProductId());

        static::assertSame($crossSelling->getId()$serialized['id']);
        static::assertSame($crossSelling->getProductId()$serialized['productId']);
        static::assertSame(implode('|', $productsIds)$serialized['assignedProducts']);

        $deserialized = $serializer->deserialize(new Config([][][])$productCrossSellingDefinition$serialized);

        static::assertSame($crossSelling->getId()$deserialized['id']);
        static::assertSame($crossSelling->getProductId()$deserialized['productId']);
        
$this->productRepository->create([$productData]$this->salesChannelContext->getContext());

        $product = $this->productRepository->search(new Criteria([$productId])$this->salesChannelContext->getContext())->get($productId);
        static::assertInstanceOf(ProductEntity::class$product);
        $result = $this->route->load($product->getId()new Request()$this->salesChannelContext, new Criteria())->getResult();

        static::assertEquals(1, $result->count());

        $element = $result->first();
        static::assertNotNull($element);
        static::assertEquals(5, $element->getProducts()->count());
        static::assertEquals(5, $element->getCrossSelling()->getAssignedProducts()?->count());

        $this->browser->request(
            'POST',
            $this->getUrl($productId),
            [
                'includes' => [
                    'product' => ['id', 'name'],
                    'product_cross_selling' => ['id', 'name'],
                ],
            ]
        );

        
Home | Imprint | This part of the site doesn't use cookies.