I have a site with 3 inventory sources. I want to add and update product inventory to each source by product sku.
Please help me
Magento 2.3 – How to Set Qty for Multiple Sources Programmatically
magento2.3multi-source-inventoryproductqtysave
Related Solutions
$sourceItemsBySku = $this->getSourceItemsBySku->execute($product->getSku());
$config['stockData'][$productId] = [];
foreach ($sourceItemsBySku as $sourceItem) {
$source = $sourceItem->getSourceCode();
$quantity = $sourceItem->getQuantity();
$config['stockData'][$productId][$source] = $quantity;
}
Can you please set stock data this way in Magento 2.3
Also please inject product and productRepository like following
$_product->setStockData(
[
'use_config_manage_stock' => 0,
'qty' => $productData['stock'],
'is_qty_decimal' => 0,
'manage_stock' => 1,
'is_in_stock' => $productData['is_in_stock']
]
);
UPDATE
I have tried this way and its working fine for me, Check this screenshot for more information : https://prnt.sc/me62gf and this https://prnt.sc/me637c
Check my controller below.
namespace Vendor\Module\Block\Index;
class Index extends \Magento\Framework\View\Element\Template
{
protected $_productFactory;
/**
* Constructor
*
* @param \Magento\Framework\View\Element\Template\Context $context
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ProductFactory $productFactory,
array $data = []
) {
parent::__construct($context, $data);
$this->_productFactory = $productFactory;
}
public function FunctionName()
{
$_product = $this->_productFactory->create();
$mainImage = "";
$imagesToImport = "";
$urlKey = $this->geturlKey('url_key');
$_product->setName('name');
$_product->setTypeId('simple');
$_product->setAttributeSetId(4);
$_product->setCategoryIds([]);
$_product->setSku('psku');
$_product->setStatus(2);
$_product->setUrlKey('url_key');
$_product->setWebsiteIds([1]);
$_product->setVisibility(4);
$_product->setDescription('description');
$_product->setShortDescription('short_description');
$_product->setPrice(30.3);
//if (isset($productData['special_price'])) {
$_product->setSpecialPrice(20.6);
$_product->setSpecialFromDate(''); //special price from (MM-DD-YYYY)
$_product->setSpecialToDate(''); //special price to (MM-DD-YYYY)
//}
$_product->setMetaTitle('meta_title');
$_product->setMetaKeyword('meta_keyword');
$_product->setWeight('1.0');
$_product->setMetaDescription('meta_description');
$_product->setColor('color');
$_product->setManufacturer('manufacturer');
$_product->setStockData(
[
'use_config_manage_stock' => 0, //'Use config settings' checkbox
'manage_stock' => 1, //manage stock
'is_in_stock' => 1, //Stock Availability
'qty' => 100, //qty
]
);
//echo "<pre>";print_r($_product->debug()); exit();
$_product->save();
}
}
Best Answer
I don't know you guys get solution or not but here you go.
Here is the solution https://magento.stackexchange.com/a/262873/49826