Skip to content

Data removed when using multiple repositories in same session #3662

@p3mbo

Description

@p3mbo

I have the following code that should create 2 simple products and a product relation. When adding a link via the link repository it seems to get rid of the stock from a product. This is not just limited to link repositories; adding a website via the website repository also has the same effect.

Code I'm using is similar to the below but correctly pushed through the constructor in my case:

        /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
        $productRepository = $this->_objectManager->create('\Magento\Catalog\Api\ProductRepositoryInterface');

        /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
        $productLink = $this->_objectManager->create('\Magento\Catalog\Api\Data\ProductLinkInterface');

        /** @var \Magento\Catalog\Api\ProductLinkRepositoryInterface $productLinkRepository */
        $productLinkRepository = $this->_objectManager->create('\Magento\Catalog\Api\ProductLinkRepositoryInterface');


        for($i = 1; $i <= 2; $i++ ) {
            /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
            $product = $this->_objectManager->create('\Magento\Catalog\Api\Data\ProductInterface');
            $product->setSku('test_sku_' . $i);
            $product->setName('Test Product ' . $i);
            $product->setAttributeSetId(4);
            $product->setPrice(5);
            $product->setTypeId('simple');

            /** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
            $stockItem = $this->_objectManager->create('\Magento\CatalogInventory\Api\Data\StockItemInterface');
            $stockItem->setIsInStock(true);
            $stockItem->setQty(50);


            /** @var \Magento\Catalog\Api\Data\ProductExtensionFactory $productExtensionFactory */
            $productExtensionFactory = $this->_objectManager->create('\Magento\Catalog\Api\Data\ProductExtensionFactory');
            $productExtension = $product->getExtensionAttributes();
            if ($productExtension === null) {
                $productExtension = $productExtensionFactory->create();
            }

            $productExtension->setStockItem($stockItem);
            $product->setExtensionAttributes($productExtension);

            $productRepository->save($product);
        }

        // If the code is exited here; the products are created with stock.


        // Related products one and two
        $productLink->setSku('test_sku_1');
        $productLink->setLinkedProductSku('test_sku_2');
        $productLink->setLinkType('related');
        $productLink->setLinkedProductType('simple');
        $productLink->setPosition(1);
        $productLinkRepository->save($productLink);

Expected Result:
2 Products created with 50 in stock and related to one-another via Related Products

Actual Result:
2 Products created with no stock and related to one-another via Related Products

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions