cancel
Showing results for 
Search instead for 
Did you mean: 

How to create ConfigurableProduct?

How to create ConfigurableProduct?

How do I create a configurable product? I am looking at Magento\ConfigurableProduct\Api and still have no idea how to actually create a configurable product... Looking at StackOverflow, seems like lots of people are using object manager, and I also read that using object manager isn't recommended now, so how do I do this?

 

Is there any documentation and code examples how to create configurable product in codes, without using object manager?

 

About the Api folder, these also confuses me:

  1. LinkManagementInterface has addChild, but ConfigurableProductManagementInterface has generateVariation, both seems to do the same thing - that is, add a variation to a simple product. So what do I need to use, in what order, how?
  2. Then, what about OptionRepositoryInterface? I assume we need to use this to create a configurable product since it has the most similar functionality to ProductRepositoryInterface used to create product, but it's taking in OptionInterface which doesn't have any fields related to attribute??

tldr: This is all so confusing, can someone just show how to create a configurable product?

 

Below is as far as my understanding managed to take me in creating a simple product, I'm still clueless how to create a configurable product.

public function __construct(ProductInterfaceFactory $factory,
                            ProductRepositoryInterface $repository)
{
    $this->repository = $repository;
    $this->factory = $factory;
}

public function setEntity($source)
{
    $product = $this->factory->create();
    $product->setSku($source->getId());
    $product->setName($source->getDescription());
    $product->setPrice($source->getUnitPrice());
    $product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    $this->repository->save($product);
}
4 REPLIES

Re: How to create ConfigurableProduct?

Hey,

You might want to have a look here: 

https://magento.stackexchange.com/questions/198778/create-configurable-products-in-magento-2-program...

 

Did it work for you?

 

---------------------------------------------------------

If you've found one of our answers useful, please give 'Kudos' or 'Accept as Solution'.

Re: How to create ConfigurableProduct?

As I said, without using object manager. I can't figure out how to do it without using object manager.. :\

Re: How to create ConfigurableProduct?

public function __construct(
  \Magento\Catalog\Model\ProductFactory $productFactory
) {
  $this->productFactory = $productFactory;
}

Now instead of using objectManager, You can use as below way,

$configproduct = $this->productFactory->create();
$configproduct->setPrice();
$configproduct->setSku();

If Issue Solved, Click Kudos/Accept As solutions.

Re: How to create ConfigurableProduct?

Still can't work this out.

Here is the problem:
setUsedProductAttributeIds, setUsedProductAttributeIds - are now in Configurables, instead of Product? But how do I use this and relate everything together?
 
setCanSaveConfigurableAttributes, setConfigurableAttributesData, setConfigurableProductsData - all missing altogether. Who uses this function anymore? If this is removed, what's the replacement, how do I even do stuffs now?

I'm using Magento 2.2.3. No option to downgrade, please don't mention anything that uses prior version.