Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot programmatically set product stock after product creation in 2.1.0 #5771

Closed
Jrb1x opened this issue Jul 22, 2016 · 7 comments
Closed

Comments

@Jrb1x
Copy link

Jrb1x commented Jul 22, 2016

Preconditions

  1. Clean install of Magento 2.1.0
  2. PHP 7
  3. MySQL 5.6
  4. Code works fine on 2.0.4

Steps to reproduce

  1. Create a product
  2. Load product $product->load(1)
  3. Attempt to update product stock:
/**
 * @var \Magento\Catalog\Model\Product
 */
$product->load(1);

/**
 * @var \Magento\CatalogInventory\Api\StockRegistryInterface
 */
$productStockData = $this->stockRegistry->getStockItem($product->getId());

$productStockData->setData('is_in_stock', 1);
$productStockData->setData('qty', 5);
$productStockData->setData('manage_stock', 1);

$productStockData->save();
$product->save();

Expected result

  1. The product should update its stock data and set to manage stock as true.

Actual result

  1. The product does not change stock data and does not throw an exception or warning.

I'm able to set the products weight and other attributes just fine with similar code, but cannot change its stock data after the product has been created.

@juanmrad
Copy link

juanmrad commented Jul 25, 2016

I have the same problem.

I am using:

public function updateProductStock($productId) {
        $product=$this->_product->load($productId); 
        $stockItem=$this->_stockRegistry->getStockItem($item['product_id']);
        $stockItem->setData('is_in_stock',$stockData['is_in_stock']); 
        $stockItem->setData('qty',$stockData['qty']); 
        $stockItem->setData('manage_stock',$stockData['manage_stock']);
        $stockItem->setData('use_config_notify_stock_qty',1);
        $stockItem->save();
        $product->save();
    }

@juanmrad
Copy link

I was able to fix my issue like this:

        $stockData = $this->getTotalStock($productId);//Load product stock.
        $product=$this->_product->load($productId); //load product which you want to update stock
        $product->setStockData(['qty' => $stockData, 'is_in_stock' => $stockData]);
        $product->setQuantityAndStockStatus(['qty' => $stockData, 'is_in_stock' => $stockData]);
        $product->save();

@Jrb1x
Copy link
Author

Jrb1x commented Aug 22, 2016

Can you show $this->getTotalStock($productId)?

@juanmrad
Copy link

It is a custom function on a custom module I created to load the stock of the product.

My module is to add stock as a Lot instead of manually. so admin adds a product lot ID with a lot stock and the function looks for all the lots that are attached to the product and returns the total stock of all lots for that product.

Basically $stockData is just a qty number.

@degaray
Copy link

degaray commented Dec 13, 2016

I have the same issue when creating a product for the first time programatically. I create the product with its factory and set all the values. For stock I create a ProductExtension object with the stockItem information and set it. It does save all the data, but it does not manage the stock. I use the same values in a json and post it using the api and it works well the problem is programatically doing so.
Magento 2.0.10, PHP7

@Jrb1x Jrb1x closed this as completed Jan 9, 2017
@joesken
Copy link

joesken commented Jan 10, 2017

Hi @Jrb1x I was just wondering why you closed this as we were having the same issue and were following this issue. Was there a resolution this? Thanks.

@juanmrad
Copy link

@joesken I showed the solution to the issue above.
you can see my code how I was able to set product stock programatically getting a total stock product stock which is just a number ex. 5, then loading the product and setting the stock data and the quantity and stock status then saving the product.

magento-engcom-team pushed a commit that referenced this issue Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants