0

In magento 2 I had created 3 product via my code. Product type is configurable, simple, simple. so now don't know set two simple product into configuration product. thank for help!

1 Answer 1

0

You can associate the simple products by loading the Configurable product.

Inject the below Class to Construct Method \Magento\Catalog\Model\ProductRepository $productRepository,

So, $this->_productRepository = $productRepository;

Assuming the id of the configurable product as '3' and the simple as ,'1' and '2' respectively.

$associatedProductIds = [2,3];//Simple Product ids array

$configurableProduct = $this->_productRepository->getById(3);

$configurableProduct->setAssociatedProductIds($associatedProductIds);

$configurableProduct->save();

Hope this helps

2
  • Thank @richard-valsanlan. I tried you code. it not working.
    – xanka
    Oct 18, 2017 at 8:58
  • Can you share your code ? Oct 18, 2017 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.