cancel
Showing results for 
Search instead for 
Did you mean: 

Multistore product save

Multistore product save

Hi,

 

i've run into a problem with saving a product in a multistore environment.

 

The Magento version is 2.1.2 and I've setup two store views in two different webistes:

 

Website 1 -> Store 1 -> Store View 1 (with id 1)

Website 2 -> Store 2 -> Store View 2 (with id 2)

  

I have one simple product (with id 1) enabled in both websites with the following data in the given stores

Store           - Name          - Use Default Value
Admin (id: 0)   - Testproduct   - NA
Store 1 (id: 1) - -----         - Yes
Store 2 (id: 2) - Testproduct 2 - No

In a custom controller in the backend i try to load the product, change some value other than the name and save it back to the database. But I get very strange results. Even if I won't change a value in the product I get these results:

If I execute the following code (_productRepository is injected via the constructor)

 

$product = $this->_productRepository->getById(1);
$this->_productRepository->save($product);

 I expected that no value changes, but I get the following result:

 

Store           - Name          - Use Default Value
Admin (id: 0)   - Testproduct   - NA
Store 1 (id: 1) - Testproduct   - No
Store 2 (id: 2) - Testproduct 2 - No 

 As you can see the Store 1 no longer uses the default value. What's even stranger is when i execute this code after the previous:

$product = $this->_productRepository->getById(1,true, 2);
$this->_productRepository->save($product);

I once again I expect nothing to change but this is what I get:

 

Store           - Name          - Use Default Value
Admin (id: 0)   - Testproduct   - NA
Store 1 (id: 1) - Testproduct 2 - No
Store 2 (id: 2) - Testproduct 2 - No

 As you can see the name of Store 1 has changed.

 

I must be doing something terrible wrong. All I want to do is load a product, change some (maybe store specific) value and save it back to the database without affecting any not changed values or their default values.

 

Can anyone point me in the right direction? And what is the purpose of the $editMode parameter in the getById method of the ProductRepository?

 

Regards

Jens