magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

ProductRepository removes category ids when saving products

Open bh-ref opened this issue 10 years ago • 5 comments

Steps to reproduce

  1. Install Magento 2.0.6.
  2. Add a simple product and assign it to an attributset, then add another simple product and assign it to a different attributset.
  3. Save both products in the same session by using class ProductRepository. See the scripts below.

Expected result

  1. The categories of the saved products remain the same.

Actual result

  1. The categories of the second saved products are removed (attribute category_ids is 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);

bh-ref avatar May 19 '16 15:05 bh-ref

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.

bkristl avatar May 26 '16 08:05 bkristl

As far as I can see, this problem does exist on Magento 2.0.7, but no longer on 2.1.0-rc1.

bh-ref avatar Jun 02 '16 14:06 bh-ref

This happens on 2.1.8. too.

tomasinchoo avatar Aug 16 '17 08:08 tomasinchoo

@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

magento-engcom-team avatar Nov 22 '17 09:11 magento-engcom-team

@bh-ref, thank you for your report. The issue is already fixed in 2.2.0

magento-engcom-team avatar Nov 29 '17 11:11 magento-engcom-team