0

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();
    $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);
}

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.

0
$configurable_product = $this->_objectManager->create('\Magento\Catalog\Model\Product');
$configurable_product->setSku('test-configurable');
$configurable_product->setName('test name configurable');
$configurable_product->setAttributeSetId(4);
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setPrice(11);
$configurable_product->setWebsiteIds(array(1));
$configurable_product->setCategoryIds(array(31));
$configurable_product->setStockData(array(
    '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);

$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->setConfigurableAttributesData($configurableAttributesData);

$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',
    )
);
$configurable_product->setConfigurableProductsData($configurableProductsData);

$configurable_product->save();

try it once

  • 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.