0

I want to create product progrmatically. product creating section is done, after creating the product i want to update the product qty and price store wise. I have following array which return me the qty and price store wise.

Array ( [0] => 1-13-45 [1] => 2-12-22 )

In my helper i have below function to update the qty and price store wise.

public function __construct(
\Magento\Catalog\Model\ProductRepository $productRepository) {
$this->_productRepository = $productRepository;
}
public function updateStockAndPricesByStore($product_id,$store_data)
{   
    foreach($store_data as $store){
        $store_datas = explode("-",$store);
        $store_id = $store_datas[0];
        $qty = $store_datas[1];
        $price = $store_datas[2];

        $new_product = $this->_productRepository->getById($product_id, false, $store_id);

        $new_product->setPrice($price);
        $new_product->save();
        $new_product->setStockData(
            array(
            'use_config_manage_stock' => 0, 
            // checkbox for 'Use config settings' 
            'manage_stock' => 1, // manage stock
            'is_in_stock' => 1, // Stock Availability of product
            'qty' => $qty // qty of product
            )
        );
    }//End for each        
}//End of function updateStockAndPricesByStoree

I can get the product name after getById function like this

echo $new_product->getName()

But unable to update the product qty and product price by using this code.

How can i update the product price and stock programatically?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.