Magento2: Product Repository saves attribute values for existing product always on "Default Store Level"

Created on 7 Dec 2016  Â·  21Comments  Â·  Source: magento/magento2


Preconditions


  1. Magento 2.1.2
  2. PHP 7
  3. Mysql 5.6

Steps to reproduce

  1. Create a CLI command to create/update a production
  2. Execute the comment (new product)
  3. Execute the comment (update product)

Expected result

  1. New Product: values are saved on "All Store Views"
  2. Update Product: values are saved on "All Store Views"

Actual result

  1. New Product: values are saved on "All Store Views"
  2. Update Product: values are saved on "Default Store View"

Seems like the product repository class overrides the product store id and always assign default store id

app/code/Magento/Catalog/Model/ProductRepository.php#L530

$productDataArray['store_id'] = (int)$this->storeManager->getStore()->getId();

The only way to work around this issue is by getting the existing product with explicit parameter:
$product = $this->_productRepository->get($sku, true, 0);

and use the deprecated save method of Magento\Framework\Model\AbstractModel class at line 629.

Catalog Fixed in 2.2.x Fixed in 2.4.x Clear Description Confirmed Format is valid Ready for Work P2 done Reproduced on 2.4.x S3 bug report

Most helpful comment

One could argue that this is not an issue but a feature: what Magento does: is when saving a product, it looks at the current store to determine where to save the attributes to. You could solve this by using emulation when using CLI:

/** @var \Magento\Store\Model\App\Emulation $this->emulation */
$this->emulation->startEnvironmentEmulation($product->getStoreId(), Area::AREA_ADMINHTML);
$this->productRepository->save($product);
$this->emulation->stopEnvironmentEmulation();

Wrapping your save()-method between these 2 lines also fixes the issue. However, I do agree that re-saving a products saves attributes for all stores and unchecks the 'Use Default Value' is unintended behavior so it would still classify as a bug.

All 21 comments

This is driving me crazy. Updating a product in the backend always saves attributes with store_id of 0 and then on the frontend it looks up attributes using store_id of 1. So the only way to update anything is to do so directly in the database. This is with single store mode enabled, which should mean that there should only be ONE store not two.

I have this issue as well. I fixed it with a hack but is something going to be done about this?

I can confirm that this is an issue for me too still. Magento 2.1.6

What kind of hack did you use? This is really a debilitating issue and the
developers don't seem to care at all.

  • Matt

On Mar 27, 2017 2:24 PM, "leitot" notifications@github.com wrote:

I have this issue as well. I fixed it with a hack but is something going
to be done about this?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/magento/magento2/issues/7720#issuecomment-289536701,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAUf75koyoYhVYhjfucIs3xidcrCeJxHks5rp_uNgaJpZM4LHJ7x
.

I wrote a before plugin for the save() method for the type Magento\Catalog\Model\ResourceModel\Product. Inside beforeSave() I set the store ID correctly on the product (after the ProductRepository's save() method messes it up)

Thanks, I'll give that a shot. I have the same problem with the category
repository too.

  • Matt

On May 22, 2017 9:03 PM, "leitot" notifications@github.com wrote:

I wrote a before plugin for the save() method for the type
Magento\Catalog\Model\ResourceModel\Product. Inside beforeSave() I set the
store ID correctly on the product (after the ProductRepository's save()
method messes it up)

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/magento/magento2/issues/7720#issuecomment-303269134,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAUf75oBD3hoHTgjSLK6Qrr9hU7PmiZoks5r8j5bgaJpZM4LHJ7x
.

I have this issue in CE 2.1.7.

The save method in the ProductRepository is incorrectly setting the store_id property on the product.

The fix is to check if the store_id is already set on the product, and if not, set it. Add the if-statement around the line below:

if (!isset($productDataArray['store_id'])) {
    $productDataArray['store_id'] = (int)$this->storeManager->getStore()->getId();
}

@itaymesh, thank you for your report.
We've created internal ticket(s) MAGETWO-83254 to track progress on the issue.

One could argue that this is not an issue but a feature: what Magento does: is when saving a product, it looks at the current store to determine where to save the attributes to. You could solve this by using emulation when using CLI:

/** @var \Magento\Store\Model\App\Emulation $this->emulation */
$this->emulation->startEnvironmentEmulation($product->getStoreId(), Area::AREA_ADMINHTML);
$this->productRepository->save($product);
$this->emulation->stopEnvironmentEmulation();

Wrapping your save()-method between these 2 lines also fixes the issue. However, I do agree that re-saving a products saves attributes for all stores and unchecks the 'Use Default Value' is unintended behavior so it would still classify as a bug.

I'm not following the argument that this is a feature. The inner workings for saving a product are not intuitive and IMHO not correct: When I'm given the option of getting a product for a specific store, and I update that product, I would expect that a save operation would update that exact product (based on its store id).

If store emulation needs to be used, then the API would need to indicate this as well. Getting a product for a specific storeview via a parameter on the 'get' method should hence not be possible (which would be a pity).

Hi @itaymesh
we have a Pull Request for this issue. We are testing the solution right now.

Hi @itaymesh. Thank you for your report.
The issue has been fixed in magento-engcom/magento2ce#967 by @p-bystritsky in 2.2-develop branch
Related commit(s):

The fix will be available with the upcoming 2.2.5 release.

I am on 2.2.2 and if I save a product from the backend the ProductRepository save method is not called at all. I see vendor/magento/module-catalog/Controller/Adminhtml/Product/Save.php is calling $product->save().

Am I missing something or how can this fix work for you @cherreman, @okorshenko?

@bpoiss Our context is that we are importing products from an external system. We use the ProductRepository to save the products in the code of our import script.

@cherreman thanks for your response. Do you have any idea how this issue can be fixed also for saving products via backend?

Still present on 2.2.6 EE adding new product from API using catalogProductRepository

This fix was never ported to 2.3 - why?

Hi @engcom-Charlie. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

:white_check_mark: Confirmed by @engcom-Charlie
Thank you for verifying the issue. Based on the provided information internal tickets MC-30488 were created

Issue Available: @engcom-Charlie, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

Hi @engcom-Charlie. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


Was this page helpful?
0 / 5 - 0 ratings