0

I need to import 40,000 product from xls so that reason I wrote an importer, I already did for magento 1.x but we switch our web site magento 2, I can create simple and configurable product programatically but I cant associate simple product to configurable product, This is the best documentation I followed,

Programmatically create a configurable product and assign the simple product to configurable product in Magento2 product

But I stuck configurable product options always throw an error,

Call to a member function getId() on array in .../vendor/magento/module-configurable-product/Model/Plugin/AroundProductRepositorySave.php on line 109

Here is my code,

$configurableProductOptions = [
[
"attribute_id" =>  137,
"label" => 'size',
"position" => 0,
"values" => [
[
"value_index" =>  333,
],
[
"value_index" =>  334,
]
],
]
];

$product->setCanSaveConfigurableAttributes(true);
$product->setCanSaveCustomOptions(true);
$productExtensionFactory= $objectManager->create('\Magento\Catalog\Api\Data\ProductExtensionFactory');
$productExtension = $productExtensionFactory->create();
$productExtension->setConfigurableProductLinks($sipmle_product_ids);
$productExtension->setConfigurableProductOptions([
                                                $configurableProductOptions
                                                 ]);
$product->setExtensionAttributes($productExtension);
$productRepository = $objectManager->create(Magento\Catalog\Api\ProductRepositoryInterface::class);

$productRepository->save($product,true);

I m totaly sure something wrong in '$configurableProductOptions' tried almost everything but didnt succeed any ideas or anyone can share code to associate simple product as a configurable I can figured out from that code.

2 Answers 2

2

Looks like you need to add StockItem extension attribute to your configurable product.

See how Magento developer do it in the test fixture product_configurable.php

6
  • Hi Kandy, Thanks for your time this is That s what I m looking for! I used same code with objectmanager but $optionsFactory = $ObjectManager->create(Factory::class); throws an error, 'Magento\Framework\Exception\LocalizedException' with message 'Source class "\Magento\ConfigurableProduct\Helper\Product\Options" for "Magento\ConfigurableProduct\Helper\Product\Options\Factory" generation does not exist. I really stucked with this I m gonna accept your answer,I m new in here should I open new question for it ? Commented Jun 23, 2016 at 20:57
  • Try to use \Magento\ConfigurableProduct\Api\Data\OptionInterfaceFactory
    – KAndy
    Commented Jun 23, 2016 at 21:05
  • Still same I m gonna write new question with more explanation can you check that one for me ? Commented Jun 23, 2016 at 21:09
  • Looks like Magento\ConfigurableProduct\Helper\Product\Options\Factory available only in 2.1 version
    – KAndy
    Commented Jun 23, 2016 at 21:11
  • magento.stackexchange.com/questions/122577/… can u check my new question its more clear, thanks for your time again Commented Jun 23, 2016 at 21:20
-1
/* Associate simple product to configurable*/

    $associatedProductIds = array($simplProductId);// array of all simple product ids
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($configProductId); // Load Configurable Product
    $attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
    $position = 0;
    $attributes = array($attributeColorId, $attributeSizeId); // Super Attribute Ids Used To Create Configurable Product
    foreach ($attributes as $attributeId) {
        $data = array('attribute_id' => $attributeId, 'product_id' => $configProductId, 'position' => $position);
        $position++;
        $attributeModel->setData($data);//->save();
    }
    $product->setTypeId("configurable");
    $product->setAffectConfigurableProductAttributes($attributeSetId);
    $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $product);
    $product->setNewVariationsAttributeSetId($attributeSetId);
    $product->setAssociatedProductIds($associatedProductIds);// Setting Associated Products
    $product->setCanSaveConfigurableAttributes(true);
    $product->save();

It worked fine for me.

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.