Currently when I am creating a new configurable product, it is by default set to out of stock, even though I have added quantity to each of the configurable products. To set the product as in stock I have goto each of the products and change the stock status. Is there any way to set the configurable product as in stock while creating the product itself. I am using Magento 2.3.3
Try creating like this.
// \Magento\Catalog\Model\ProductFactory $product $product = $this->product->create(); //set data you want to set here $product->setMetaTitle($name); $product->setMetaKeyword($name); $product->setMetaDescription($description); $product->setPrice((int)$price); $product->setStockData([ 'use_config_manage_stock' => 1, 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => (int)$qty ]); $product->getResource()->save($product); $productId = $product->getEntityId();
Hope this will help you !!