Skip to content

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

Closed
@Jrb1x

Description

@Jrb1x

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions