2

We want update product quantity programatically. If product status is Enable then quantity update properlly. But which product status is Disable quantity not update in it. We create one custom module for it. Any one help me for sort out this.

Here is my module Block/Index/index.php file code.

public function updateStock($productId,$productQty)
{
    $product = $this->productFactory->create()->load($productId);

    if($product->getStatus() == 2){ // 2 => Disable , 1 => Enable

        $product->setStatus(1);

        if($product->getStatus() == 1){
            $product->setStockData([
                //'use_config_manage_stock' => 0, //'Use config settings' checkbox
                //'manage_stock' => 1, //manage product stock
                'is_in_stock' => 1, //Stock Availability
                'qty' => $productQty 
            ]);

            $product->setQuantityAndStockStatus([
                'qty' => $productQty,
                'is_in_stock' => 1
            ]); 
        }

        $product->setStatus(2);

        try {
            $product->save(); 
            echo 'Quantity update of SKU => '.$product->getSku().'<br/>'; 
        } catch (Exception $e) {
            echo $e->getException();
        }

    }else{

        $product->setStockData([
            //'use_config_manage_stock' => 0, //'Use config settings' checkbox
            //'manage_stock' => 1, //manage product stock
            'is_in_stock' => 1,
            'qty' => $productQty
        ]);

        $product->setQuantityAndStockStatus([
            'qty' => $productQty,
            'is_in_stock' => 1
        ]); 

        try {
            $product->save(); 
            echo 'Quantity update of SKU => '.$product->getSku().'<br/>'; 
        } catch (Exception $e) {
            echo $e->getException();
        }
    }

1 Answer 1

-1

We have tested its work. You can used also Objectmanager.

        <?php
        public function updateStock($productId,$productQty)
        {
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);


            if($product->getStatus() == 2){ // 2 => Disable , 1 => Enable

                $product->setStatus(1);

                if($product->getStatus() == 1){
                    $product->setStockData([
                        //'use_config_manage_stock' => 0, //'Use config settings' checkbox
                        //'manage_stock' => 1, //manage product stock
                        'is_in_stock' => 1, //Stock Availability
                        'qty' => $productQty 
                    ]);

                    $product->setQuantityAndStockStatus([
                        'qty' => $productQty,
                        'is_in_stock' => 1
                    ]); 
                }

                $product->setStatus(2);

                try {
                    $product->save(); 
                    echo 'Quantity update of SKU => '.$product->getSku().'<br/>'; 
                } catch (Exception $e) {
                    echo $e->getException();
                }

            }else{

                $product->setStockData([
                    //'use_config_manage_stock' => 0, //'Use config settings' checkbox
                    //'manage_stock' => 1, //manage product stock
                    'is_in_stock' => 1,
                    'qty' => $productQty
                ]);

                $product->setQuantityAndStockStatus([
                    'qty' => $productQty,
                    'is_in_stock' => 1
                ]); 

                try {
                    $product->save(); 
                    echo 'Quantity update of SKU => '.$product->getSku().'<br/>'; 
                } catch (Exception $e) {
                    echo $e->getException();
                }
            }
        }
        ?>
3
  • We put it but not update product quantity which products are disable. And second thing in my code enable product already save than why we need to use object manager ?
    – Anil
    Commented Jun 27, 2019 at 7:45
  • If productfactory not working then try this for disbaled product its may be working because i have try your code its work may conflict other inventory module. Commented Jun 27, 2019 at 11:17
  • Yes my code is working but not update product quantity, which product status is disable.
    – Anil
    Commented Jun 27, 2019 at 13:15

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.