1

When I programmatically create configurable and simples product, all is created but simple products do not add to configurable. The code works well, without errors in log. Can you help me?

    /*************  simple **************/    
$n_product = $objectManager->create('Magento\Catalog\Model\Product');
for($vel=35; $vel<43; $vel++) 
    {
    if(!$n_product->getIdBySku($sku."-".$vel))
        {
        if ($vel == 35) { $vel_id = 549; }
        if ($vel == 36) { $vel_id = 550; }
        if ($vel == 37) { $vel_id = 551; }
        if ($vel == 38) { $vel_id = 552; }
        if ($vel == 39) { $vel_id = 553; }
        if ($vel == 40) { $vel_id = 554; }
        if ($vel == 41) { $vel_id = 555; }
        if ($vel == 42) { $vel_id = 556; }
        $n_product->setSku($sku."-".$vel);
        $n_product->setName($sku."-".$vel);
        $n_product->setAttributeSetId($atIDS);
        $n_product->setWebsiteIds(array(1));
        $n_product->setStatus(1);
        $n_product->setWeight(1000);
        $n_product->setVisibility(2);
        $n_product->setTaxClassId(2);
        $n_product->setTypeId('simple');
        $n_product->setPrice($cena);
        $n_product->setSpecialPrice($cena_akcni);
        $n_product->setStockData(array('use_config_manage_stock' => 0,'manage_stock' => 1,'is_in_stock' => 1,'qty' => 1));
        $n_product->setSize($vel_id);
        $n_product->save();
        $simpleProductArr[] = $n_product->getId();
        $logger->info("Simple product ".$vel.": ".$simpleProductArr[]);
        }
    }
/*************  configurable **************/
if(!$n_product->getIdBySku($sku)) 
    {
    $n_product = $objectManager->create('\Magento\Catalog\Model\Product');
    if ($kategorie == 'Dámské válenky') { $n_product->setAttributeSetId($atIDS); $n_product->setCategoryIds(array(3,4,324)); }
    if ($kategorie == 'Dámské lodičky') { $n_product->setAttributeSetId($atIDS); $n_product->setCategoryIds(array(3,4,29)); }
    if ($kategorie == 'Dámské tenisky') { $n_product->setAttributeSetId($atIDS); $n_product->setCategoryIds(array(3,4,30)); }               
    if ($kategorie == 'Dámské sandále') { $n_product->setAttributeSetId($atIDS); $n_product->setCategoryIds(array(3,4,31)); }
    $n_product->setSku($sku);
    $n_product->setName($nazev);
    $n_product->setWebsiteIds(array(1));
    $n_product->setStatus(1);
    $n_product->setWeight(1000);
    $n_product->setVisibility(2);
    $n_product->setTaxClassId(2);
    $n_product->setTypeId('configurable');
    $n_product->setPrice($cena);
    $n_product->setSpecialPrice($cena_akcni);
    $n_product->setStockData(array('use_config_manage_stock' => 0,'manage_stock' => 1,'is_in_stock' => 1,'qty' => 1));
    $n_product->setMetaTitle($nazev);
    $n_product->setMetaKeyword($popis);
    $n_product->setMetaDescription($popis);
    $n_product->getTypeInstance()->setUsedProductAttributeIds(array(206),$n_product);
    $configurableAttributesData = $n_product->getTypeInstance()->getConfigurableAttributesAsArray($n_product);
    $n_product->setCanSaveConfigurableAttributes(true);
    $n_product->setConfigurableAttributesData($configurableAttributesData);
    $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
    $simpleProducts = $productCollection->addIdFilter($simpleProductArr)->addAttributeToSelect('size')->addAttributeToSelect('price');
    $configurableProductsData = array();
    foreach ($simpleProducts as $simple)
        {
        $logger->info("Info: ".$simple->getAttributeText('size')."-".$simple->getSize()."-".$simple->getPrice()."-".$simple->getId());
        $productData = array(
            'label' => $simple->getAttributeText('size'),
            'attribute_id' => 206,
            'value_index' => (int) $simple->getSize(),
            'is_percent' => 0,
            'pricing_value' => $simple->getPrice());
        $configurableProductsData[$simple->getId()] = $productData;
        }
    $n_product->setConfigurableProductsData($configurableProductsData);
    $n_product->save();
    }
3
  • What is the magento version your using? Commented Sep 4, 2018 at 10:57
  • See the namespace this instantly tells you its magento 2 something. the version is irrelivant as long as you use the factories and interfaces not the models which are deprecated. Commented Sep 4, 2018 at 11:02
  • Magento ver. 2.2.2 Commented Sep 4, 2018 at 15:11

1 Answer 1

0

bellow is the quickest way to add configurable product link to a product loaded via product repository, so save your base configurable product first.

   $attributeValues[] = [
                'label' => $optionLabel,
                'attribute_id' => $attribute->getId(),
                'value_index' =>  $config['product']->getData($attribute->getAttributeCode()),
            ];

            $configurableAttributesData = [
                [
                    'attribute_id' => $attribute->getId(),
                    'code' => $attribute->getAttributeCode(),
                    'label' => $attribute->getStoreLabel(),
                    'position' => '0',
                    'values' => $attributeValues,
                ],
            ];

From your simple products populate two arrays like above to pass to the configurable product.

  $extensionConfigurableAttributes = $p->getExtensionAttributes();
        $childrenIds = $p->getTypeInstance()->getChildrenIds( $p->getId());

        if(is_array($childrenIds)) {
            if(isset($childrenIds[0])) {
                $associatedProductIds = array_unique(array_merge($associatedProductIds, $childrenIds[0]));
            }
        }

        // die(var_dump($associatedProductIds, $extensionConfigurableAttributes->getConfigurableProductLinks()));
        $configurableOptions = $this->optionsFactory->create($configurableAttributesData);

        $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
        $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);


        $p->setExtensionAttributes($extensionConfigurableAttributes);

And then simply save the product like below. This will work for both create and update, and will not create duplicate groups.

   $p = $this->productRepository->save($p);
1
  • Sorry man, I dont understand this. Can you explain me? Commented Sep 4, 2018 at 15:47

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.