0

Below Code is working fine for add configurable product in magento2. but i am facing issue since last two days.. but can't found any proper solution. The qty not added to my configurable product as well as in simple product.. here is my whole code.. can anybody help me.. and thanks in advance

   try{
        $ob = \Magento\Framework\App\ObjectManager::getInstance();

        /** @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepo */
        $attributeRepo =  $ob->get(\Magento\Catalog\Api\ProductAttributeRepositoryInterface::class);

        $attribute = $attributeRepo->get($data['Attribute']);  // color should be in default attribute set

        /** @var \Magento\Catalog\Api\ProductRepositoryInterface $pr */
        $pr = $ob->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
        $ids = [];
        $values = [];
        $options = $attribute->getOptions();
        array_shift($options);
        foreach($options as $option) {
            $id = $option->getValue();
            if(!in_array($id, $attrPostOptions)) continue;
            /** @var \Magento\Catalog\Api\Data\ProductInterface $p */
            $p = $ob->get(\Magento\Catalog\Api\Data\ProductInterface::class);
            $p->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
            $p->setSku($data['Sku'].'-'. $id);
            $p->setName($data['Name'].' #'. $option->getLabel());
            $p->setAttributeSetId(4);
            $p->setPrice($data['Price']);
            $p->setDescription($data['Description']); // description of product
            $p->setShortDescription($data['Description']); // short description of product
            $p->setTypeId('simple');
            $p->setCustomAttribute($attribute->getAttributeCode(), $id);
            $p->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
            $p->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
            $p->setStockData([
                'use_config_manage_stock' => 1,
                'qty' => 1000,
                'is_qty_decimal' => 0,
                'is_in_stock' => 1
            ]);
            $p = $pr->save($p);
            $ids[] = $p->getId();
            /** @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterface $opVal */
            $opVal =  $ob->create(\Magento\ConfigurableProduct\Api\Data\OptionValueInterface::class);
            $opVal->setValueIndex($id);
            $values[] = $opVal;
        }
        /** @var \Magento\Catalog\Api\Data\ProductInterface $cp */
        $cp = $ob->get(\Magento\Catalog\Api\Data\ProductInterface::class);
        $cp->setSku($data['Sku']);
        $cp->setName($data['Name']);
        $cp->setAttributeSetId(4);
        $cp->setPrice($data['Price']);
        $cp->setDescription($data['Description']);
        $cp->setShortDescription($data['Description']);
        $cp->setTypeId(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE);
        $cp->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        $cp->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $cp->setStockData([
            'use_config_manage_stock' => 1,
            'qty' => 1000,
            'is_qty_decimal' => 0,
            'is_in_stock' => 1
        ]);

        $allProductIds = $ids;

        /** @var \Magento\ConfigurableProduct\Api\Data\OptionInterface $option */
        $option = $ob->create(\Magento\ConfigurableProduct\Api\Data\OptionInterface::class);
        $option->setLabel($attribute->getStoreLabel());
        $option->setAttributeId($attribute->getAttributeId());
        $option->setValues($values);

        $exteAttrs = $cp->getExtensionAttributes();
        $exteAttrs->setConfigurableProductLinks($ids);
        $exteAttrs->setConfigurableProductOptions([
            $option
        ]);

        $cp->setExtensionAttributes($exteAttrs);

        $cp = $pr->save($cp);
        array_push($allProductIds,$cp->getId());
        $res['status'] = true;
        // $res['ids'] = json_encode($allProductIds);
    } catch(Exception $e) {
        $res['status'] = false;
        $res['message'] = $e->getMessage();
        $lastId = end($allProductIds);
        $confProdId = $lastId+1;
        array_push($allProductIds, $confProdId);
        // $res['ids'] = json_encode($allProductIds);
        $this->deleteProducts($allProductIds);
    }

1 Answer 1

0

Sorry I found answer by myself... when you fill qty of all simple products of particular configurable product after that this will show as In Stock on storefront.

and here is the code to update simple product

 public function updateProductQty($ids){
    //update simple product quantity
    try{
        if(!empty($ids) && is_array($ids)){
            foreach($ids as $id){
                $qty = 10;
                $ob = \Magento\Framework\App\ObjectManager::getInstance();
                $product = $ob->get('Magento\Catalog\Model\Product')->load($id);
                $this->stockRegistry = $ob->create('\Magento\CatalogInventory\Api\StockRegistryInterface');
                $stockItem = $this->stockRegistry->getStockItemBySku($id);
                // $stockItem->setQty($qty);
                $stockItem->setData('is_in_stock',1); //set updated data as your requirement
                $stockItem->setData('qty',$qty); //set updated quantity 
                $stockItem->setData('manage_stock',1);
                $this->stockRegistry->updateStockItemBySku($id, $stockItem);
            }
        }
    } catch(Exception $e) {
        echo $e->getMessage()."\n";
    }
}

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.