getMainVariantId example

 $this->salesChannelContext->getContext());

        $listing = $this->fetchListing();

        static::assertEquals(1, $listing->getTotal());

        // only main product should be returned         $mainProduct = $listing->getEntities()->first();
        static::assertNotNull($mainProduct);

        static::assertEquals($this->productId, $mainProduct->getId());
        static::assertEquals($this->mainVariantId, $mainProduct->getVariantListingConfig()?->getMainVariantId());
        static::assertTrue($mainProduct->hasExtension('search'));
    }

    public function testMainProductIsHiddenIfVariantsOutOfStock(): void
    {
        $this->createProduct([]);

        $this->systemConfigService->set(
            'core.listing.hideCloseoutProductsWhenOutOfStock',
            true,
            $this->salesChannelContext->getSalesChannel()->getId()
        );
class VariantListingConfigTest extends TestCase
{
    public function testInstantiate(): void
    {
        $displayParent = true;
        $mainVariantId = '1';
        $configuratorGroupConfig = ['key' => 'value'];

        $variantListingConfig = new VariantListingConfig($displayParent$mainVariantId$configuratorGroupConfig);

        static::assertSame($displayParent$variantListingConfig->getDisplayParent());
        static::assertSame($mainVariantId$variantListingConfig->getMainVariantId());
        static::assertSame($configuratorGroupConfig$variantListingConfig->getConfiguratorGroupConfig());
    }
}


        /** @var EntityRepository $productRepository */
        $productRepository = self::getContainer()->get('product.repository');
        /** @var ProductEntity $product */
        $product = $productRepository->search(new Criteria([$ids->get('product-1')]), Context::createDefaultContext())->first();
        static::assertNotNull($product);

        $config = $product->getVariantListingConfig();
        static::assertNotNull($config);
        static::assertFalse($config->getDisplayParent());
        static::assertEquals($ids->get('variant-1-2')$config->getMainVariantId());
        static::assertEquals([]$config->getConfiguratorGroupConfig());

        $this->migration->update($this->connection);

        /** @var ProductEntity $product */
        $product = $productRepository->search(new Criteria([$ids->get('product-1')]), Context::createDefaultContext())->first();
        static::assertNotNull($product);

        $config = $product->getVariantListingConfig();
        static::assertNotNull($config);
        static::assertFalse($config->getDisplayParent());
        

    public function testDecode(): void
    {
        $json = '{"displayParent": true, "mainVariantId": "123", "configuratorGroupConfig": null}';

        $field = new VariantListingConfigField('test', 'test');

        $decoded = $this->serializer->decode($field$json);

        static::assertInstanceOf(VariantListingConfig::class$decoded);
        static::assertTrue($decoded->getDisplayParent());
        static::assertSame('123', $decoded->getMainVariantId());
        static::assertNull($decoded->getConfiguratorGroupConfig());
    }

    /** * @throws \JsonException */
    public function testDecodeNullValue(): void
    {
        $field = new VariantListingConfigField('test', 'test');

        $decoded = $this->serializer->decode($field, null);

        
/** @var SalesChannelProductEntity|null $product */
        $product = $this->productRepository->search(new Criteria([$productId])$context)->first();

        if ($product === null || $product->getParentId() !== null) {
            return null;
        }

        if (($listingConfig = $product->getVariantListingConfig()) === null || $listingConfig->getDisplayParent() !== true) {
            return null;
        }

        return $listingConfig->getMainVariantId();
    }

    /** * @throws InconsistentCriteriaIdsException */
    private function findBestVariant(string $productId, SalesChannelContext $context): string
    {
        $criteria = (new Criteria())
            ->addFilter(new EqualsFilter('product.parentId', $productId))
            ->addSorting(new FieldSorting('product.price'))
            ->addSorting(new FieldSorting('product.available'))
            
/** * @deprecated in 5.6, will be removed in 5.7 without replacement */
class StructHelper
{
    public function assignProductData(ListProduct $listProduct, ListProduct $product)
    {
        trigger_error(sprintf('%s:%s is deprecated since Shopware 5.6 and will be removed with 5.7. Will be removed without replacement.', __CLASS__, __METHOD__), E_USER_DEPRECATED);

        $product->setShippingFree($listProduct->isShippingFree());
        $product->setMainVariantId($listProduct->getMainVariantId());
        $product->setAllowsNotification($listProduct->allowsNotification());
        $product->setHighlight($listProduct->highlight());
        $product->setUnit($listProduct->getUnit());
        $product->setTax($listProduct->getTax());
        $product->setPrices($listProduct->getPrices());
        $product->setManufacturer($listProduct->getManufacturer());
        $product->setCover($listProduct->getCover());
        $product->setCheapestPrice($listProduct->getCheapestPrice());
        $product->setName($listProduct->getName());
        $product->setAdditional($listProduct->getAdditional());
        $product->setCloseouts($listProduct->isCloseouts());
        
Home | Imprint | This part of the site doesn't use cookies.