ProductRepository removes category ids when saving products
Steps to reproduce
- Install Magento 2.0.6.
- Add a simple product and assign it to an attributset, then add another simple product and assign it to a different attributset.
- Save both products in the same session by using class ProductRepository. See the scripts below.
Expected result
- The categories of the saved products remain the same.
Actual result
- The categories of the second saved products are removed (attribute
category_idsis empty). Note: this does not happen if both products use the same attributeset.
update_products.inc
<?php
require_once 'bootstrap.inc';
$objectManager = $app->getObjectManager();
$adminStoreId = $objectManager->get(\Magento\Store\Model\StoreManager::class)->getStore('admin')->getId();
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$values = ['skuA' => 'valueA', 'skuB' => 'valueB'];
foreach ($values as $sku => $value)
{
try {
/** @var Magento\Catalog\Model\Product $product */
$product = $productRepository->get($sku);
// we used this script to set values on products,
// but the described bug appears even if you do not change an attribute,
// therefore this is commented out:
// $product->setData('myAttribute', $value);
echo 'Updating product with id "'.$product->getId().'" (sku: "'.$sku.'")'."\n";
$productRepository->save($product);
}
catch (Exception $ex) {
echo 'Exception for product with sku "'.$sku.'": ';
echo $ex->getMessage()."\n";
}
}
bootstrap.inc
<?php
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\State;
use Magento\Framework\Shell\ComplexParameter;
$errorReporting = ini_get('error_reporting');
require __DIR__ . '/../../www/app/bootstrap.php';
$bootstrapParam = new ComplexParameter('bootstrap');
$params = $bootstrapParam->mergeFromArgv($_SERVER, $_SERVER);
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
if (!array_key_exists(State::PARAM_MODE, $params))
$params[State::PARAM_MODE] = State::MODE_DEVELOPER;
$app = Bootstrap::create(BP, $params);
$app->getObjectManager()->get('Magento\Framework\App\State')->setAreaCode('adminhtml');
ini_set('error_reporting', $errorReporting);
I have the same experience when i update all products qty and price.
$filter = $this->filterBuilder->setField('type_id')->setConditionType('eq')->setValue('simple')->create(); $filterGroups[] = $this->filterGroupBuilder->addFilter($filter)->create(); $products = $this->productRepository->getList($this->searchCriteriaBuilder->setFilterGroups($filterGroups)->create())->getItems(); foreach ($products as $product) { $inStock = ($qty > 0) ? 1 : 0; $product->setStockData(['qty' => $qty, 'is_in_stock' => $inStock]);
$product->setPrice($productPrice);
$product->setSpecialPrice($productSpecialPrice);
$product->setSpecialFromDate($productSpecialFromDate);
$product->setSpecialToDate($productSpecialToDate);
$this->productRepository->save($product);
} When i run the script in Console\Command delete all products category_ids If i use $product->save() instead $this->productRepository->save($product); then product get attributes which are not assign to attribute set. Usually get attributes attributes with type select and default value.
As far as I can see, this problem does exist on Magento 2.0.7, but no longer on 2.1.0-rc1.
This happens on 2.1.8. too.
@bh-ref, thank you for your report. Please use the issue reporting guidelines to report an issue. Please, edit issue description and include the preconditions, the steps required to reproduce, the actual result, and the expected result. Please, add the following information:
- [ ] Preconditions: Magento version
- [ ] Steps to reproduce
- [ ] Expected result
- [ ] Actual result
@bh-ref, thank you for your report. The issue is already fixed in 2.2.0