1

I want to add stock data on a product but it doesn't work. In my function, I have a product created by :

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

Then, I set some data like price, status, name, ... And eventually, I add a code for the stock :

$product->setStockData(array(
            'use_config_manage_stock' => (bool)0, //'Use config settings' checkbox
            'manage_stock' => (bool)1, //manage stock
            'min_sale_qty' => 0, //Minimum Qty Allowed in Shopping Cart
            'max_sale_qty' => 0, //Maximum Qty Allowed in Shopping Cart
            'is_in_stock' => (bool)1, //Stock Availability
            'qty' => 0 //qty
            )
        );

I don't want that the product inherit the config settings and I absolutly want a quantity different of null (0 or greater).

But, when the function is launched, the product is created, every data is ok exept the stock data.

enter image description here

0

You need to give intval for your qty to set int value of qty.

       $product->setStockData(array(
            'use_config_manage_stock' => (bool)0, //'Use config settings' checkbox
            'manage_stock' => (bool)1, //manage stock
            'min_sale_qty' => 0, //Minimum Qty Allowed in Shopping Cart
            'max_sale_qty' => 0, //Maximum Qty Allowed in Shopping Cart
            'is_in_stock' => (bool)1, //Stock Availability
            'qty' => (intval)0 //qty
            )
        )
      try {
            $product->save(); 
        } catch (Exception $e) {
            echo $e->getException();
        }
1
  • Sorry, but still doesn't work because of the "use_config_manage_stock" set as true despite the 'use_config_manage_stock' => (bool)0 – Erlaunis Mar 8 '18 at 14:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.