1

I am able to updated the product price and Quantity. before that i wanted to check if product is available in store or not.

Here is the code of updating Price and Stock.

public function updateProduct($product_id, $price, $qty1){

       ini_set('max_execution_time', 0);
       $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
       $objectManager = $bootstrap->getObjectManager();
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       $state = $objectManager->get('\Magento\Framework\App\State');
       $state->setAreaCode('frontend');
       $storeId = '1'; //Store ID

       $productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');

       $product = $productFactory->create();
       $prduct_id_sku = $product->load($product->getIdBySku($product_id));

       $oldprice = $product->getPrice();
       $productName = $product->getName();

       $oldstock = $StockState->getStockQty($product->getId());

       if (($oldprice != $price) || $oldstock != $qty1 ) {

          $product->setPrice($price); 

          if ($oldstock != $qty1) {
            $product->setStockData(array(
                    'use_config_manage_stock' => 0,
                    'manage_stock' => 1,
                    'is_in_stock' => 1,
                    'qty' => $qty1 ));   
        $product->setQuantityAndStockStatus(['qty' => $qty1, 'is_in_stock' => 1]);
          }else{
            //echo " \n oldStock & new stock are equal ".$qty;
         }
         $product->save();

        }else {
          //echo " \n oldprice & new price are equal ".$price;       
        }
        unset($storeIds, $websiteObj, $_websiteId);
        unset($product);
        }

Please guide me. if there is no product available with SKU what will be in

$prduct_id_sku = $product->load($product->getIdBySku($product_id));

$prduct_id_sku will be null or not. I was not able to debug to test this out.

1 Answer 1

1

Add this line.

    $prduct_id_sku = $product->load($product->getIdBySku($product_id));
    $oldprice = $product->getPrice();
    $productName = $product->getName();
    $oldSku = $product->getSku();
    if ((($oldprice != $price) || $oldstock != $qty1) && !$oldSku) {
        $product->setPrice($price);
        if ($oldstock != $qty1) {
            $product->setStockData(array(
                'use_config_manage_stock' => 0,
                'manage_stock' => 1,
                'is_in_stock' => 1,
                'qty' => $qty1 ));
            $product->setQuantityAndStockStatus(['qty' => $qty1, 'is_in_stock' => 1]);
        } else {
            //echo " \n oldStock & new stock are equal ".$qty;
        }
        $product->save();
    } else {
        //echo " \n oldprice & new price are equal ".$price;
    }
    unset($storeIds, $websiteObj, $_websiteId);
    unset($product);
}
11
  • by adding this like && $oldstock if condition, When i have product listed and stock has reduced to zero, then it is not updating the value. Any idea how to recognize available product with 0 stock.? Commented Jun 5, 2018 at 4:30
  • Replace getStockQty() on getIsInStock() Commented Jun 5, 2018 at 4:37
  • This i got Fatal error: Call to undefined method Magento\CatalogInventory\Model\StockState::getIsInStock() in C:\xampp\htdocs\Magento-CE\app\code\Autosynch\synchProduct\Controller\Page\Service.php on line 113 Commented Jun 5, 2018 at 4:39
  • This need get from a product ( you can use product->getQty(), getIsInStock) From product. Commented Jun 5, 2018 at 4:43
  • Example && product->getQty() == 0 Commented Jun 5, 2018 at 4:50

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.