0

when I am updating product using the following code

$product = $this->productFactory->create()->load($productId);
foreach ($attrData as $key => $value) {
    $product->setDataUsingMethod($key, $value);
} 

$product->setStockData(['qty' => $newqty, 'is_in_stock' => $newss]);

$this->productFactory is object of \Magento\Catalog\Model\ProductFactory

the product updated data in product grid is not getting updated, only data in product edit section is getting updated. In the grid, I am getting older values even after the cache flush and re-indexation.

When I am saving data using the following method

$this->productAction->create()->updateAttributes([$productId], $attrData, $storeId);

$this->productAction is object of \Magento\Catalog\Model\Product\ActionFactory

everything works as expected and the data in product edit section and product grid is same.

Why is it like so and what is the reason for this behaviour?

0

The diffrence that I can see is for reindexing,

In updateAttributes method this indexer will reindex if you set update on save for reindexing

`catalog_product_attribute`

and for product save it will reindex catalog_product_flat

Update

I think problem is in below method in vendor/magento/module-catalog/Model/Product.php

Here Magento reset old data for that attribute so may be that's why you get old values

I am not sure this is the exact issue or not but may be this is the issue

public function addAttributeUpdate($code, $value, $store)
{
    $oldValue = $this->getData($code);
    $oldStore = $this->getStoreId();

    $this->setData($code, $value);
    $this->setStoreId($store);
    $this->getResource()->saveAttribute($this, $code);

    $this->setData($code, $oldValue);
    $this->setStoreId($oldStore);
}

That is the only difference that I saw while saving both

  • yes you are correct but I am running the re-indexation via CLI after executing the code and still, the data is inconsistent. – Anshu Mishra Apr 24 '18 at 6:11
  • @AnshuMishra please check update section and please try and let me know,as I am not sure this is the issue or not – Murtuza Zabuawala Apr 24 '18 at 6:27
  • I have checked by updating the name, but I don't see this method being called in the process. Do you know how and when this method gets called? – Anshu Mishra Apr 24 '18 at 6:47

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.