This is as far as it gets me trying to create product:

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

public function setEntity($source)
    $product = $this->factory->create();

Now, how do I create a configurable product and link it to this product? There is no ConfigurableProductRepository, so how do I achieve this?

For the record: I see lots of Google results do something like this, but it feels like less elegant than the Factory/Repository pattern to do CRUD. Mainly because I need to work directly against a concrete class rather than against an interface...

Is there no other way to create ConfigurableProduct?

I'm using version 2.2.3.

$configurable_product = $this->_objectManager->create('\Magento\Catalog\Model\Product');
$configurable_product->setName('test name configurable');
    'use_config_manage_stock' => 0, //'Use config settings' checkbox
    'manage_stock' => 1, //manage stock
    'is_in_stock' => 1, //Stock Availability

$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(152),$configurable_product); //attribute ID of attribute 'size_general' in my store
$configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product);


$configurableProductsData = array();
$configurableProductsData[$simple_product_id] = array( //[$simple_product_id] = id of a simple product associated with this configurable
    '0' => array(
        'label' => 'S', //attribute label
        'attribute_id' => '152', //attribute ID of attribute 'size_general' in my store
        'value_index' => '193', //value of 'S' index of the attribute 'size_general'
        'is_percent'    => 0,
        'pricing_value' => '10',


try it once

| improve this answer | |
  • I'm sorry, have you tested it to be working on version 2.2.3? The function setUsedProductAttributeIds, setUsedProductAttributeIds are not in Product anymore. setCanSaveConfigurableAttributes, setConfigurableAttributesData, setConfigurableProductsData are all not there anymore. – Farid Jun 6 '18 at 6:15
  • Also: Product doesn't have save() function anymore? (raw.githubusercontent.com/magento/magento2/2.2-develop/app/code/…), probably to encourage people to use Repository rather than working against the Product class directly. – Farid Jun 6 '18 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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