In my system were two store views. "Default Store View" (store_id=1) and "Custom Store View" (store_id=2). Custom view always was default. I've removed unnecessary store view "Default".
When I programmatically import products it stores values to table catalog_product_entity_varchar for store_id=2. In my code I don't set any store id. I tried to set but it gives me no result.
So when I want to change product name for "Main Website" it gives me nothing at frontend (there is the same product name as it was, i.e. for store_id=2), because it edits value only for store_id=0 ("Main WEbsite"), but no changes for store_id=2 or maybe values for it shouldn't exist at all.
There no ability to switch for "Custom Store View" for now when it is only one store view in system.
I have single store mode enabled and I need only one store view with only one value type for each product. I know that I can cleanup some tables with values for store_id=2, but my system does import of products every day, therefore probably I need to turn off storing values for Custom Store View.
Magento 2.1.0
PHP 7.0.11
10.1.17-MariaDB
` function createProduct(){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku($itemSku);
$product->setName($itemName);
$product->setAttributeSetId(4);
$product->setStatus(1);
$product->setTaxClassId(0);
$product->setWebsiteIds([1]);
//$product->setWeight(10);
$product->setVisibility(4);
$product->setTypeId('simple');
$product->setPrice(0.01);
$product->setStockData([
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => 1,
'qty' => 10
]);
$product->save();
}`
At frontend on product page we should see edited value of parameter name "Edited Product".
At frontend on product page we still see "Imported Product" instead of new one "Imported Product".
I have the exact same problem. Using Magento 2.1.3. In my case, there is a default "admin" store with id=0, while the "default" has store id = 1. See below.
Whenever any object (usually products) have store id =1, it will always override the value of the same data with store id =0 on the frontend.
After selecting Single store mode, products are only added/updated with store id = 0 as expected when using the Magento admin panel.
However, if we alter products with PHP scripts or modules, for instance using the code by @antonlazarchenko, the data for the product is duplicated for store id =1. See below.
Assuming we have:
$_product = $objectManager->create('Magento\Catalog\Model\Product');
$_productTierPrice = $objectManager
->create('Magento\Catalog\Api\ProductTierPriceManagementInterface');
It is possible to use
$_product->setStoreId(0);
in order to only affect the data for storeId=0. But this does not apply to every case.
For instance, using
$_productTierPrice->add($sku, 0, $productRetailPromoPrice, 1);
will always duplicate every single attribute of data for that product. And there is no way to specify a store.
Problem is, in Single Store mode, modifying a product will only update its store id=0 values. And so, we have situations like a product shows "disabled" on the backend, but is actually still enabled on the frontend.
So basically, my temporary solution is to erase everything "store id 1" every time I make any modification to a product as such
$_product->setStoreId(1)
->setVisibility(null)
->setName(null)
->setDescription(null)
->setShortDescription(null)
->setCategoryIds(null)
//......for every attribute I know of.
->save();
But I think that if the store is in Single Store mode, Magento should stop entirely to override store id=0 data as it is clearly the one intended to be used.
Please correct me if I'm wrong.
I think I'm having the same issue as @sachalegrand. My issue is first started here: http://magento.stackexchange.com/questions/156717/magento-2-create-product-programmatically-with-different-store-view-values
Following this issue.
I have the same issue with the difference being that I create my base products via REST API from an external DB then manicure the details/information/images, etc in the admin catalog. I also have a cron job that updates the stock/prices via REST API.
This definitely seems to be a bug with however Magento determines which store_id
a product has while Single-Store Mode is enabled. This could be related to the issues brought up about the default setting for Single-Store Mode (#1843, #1885 and #2446) which I and many others seem to think that enabled should be the default setting.
I think it assumes too much about the users of Magento by having multiple stores as the default option. Even WordPress has Single-Site Mode enabled by default.
@veloraven Any news about this? I think it's a rather big issue...
@robbanl I found a workaround if you are using the REST API. Simply include the store_id
of the store you want to update in the URL like so,
http://<domain>/index.php/rest/<store_id>/V1/products/<sku>
Where <store_id>
can be all
or default
. The all
ID is equal to the admin store code 0
, while default
can be variable in multi-store mode equal to the language code - en
, fr
, de
, etc.
If you only have one store, I personally found that using default
in the URL while multi-store is enabled and only having the one default store view works fine. I still would like to see this fixed though as I agree that it is sort of a big issue.
I have the same issue. I create a product through the API and it gets a value of store id 0. But if I update and then save the product through the API, the updates are applied with a store id 1. So I get a different value for the same attribute for store id 0 and store id 1, and I don't want this to happen.
As a workaround, I save the attribute value directly on the product by calling this method where, for example 'price' is the attribute name, $price is the value of the attribute, and 0 is the store id value -
$magentoProduct->addAttributeUpdate('price', $price, 0);
If you are updating custom attributes and you don't know the name of the attributes you can do it in a loop like this
$magentoProduct->setData($attributeName, $attrValue);
$magentoProduct->setData('store_id', 0);
$magentoProduct->setStoreId(0);
$magentoProduct->getResource()->saveAttribute($magentoProduct, $attributeName);
You can leave it like this, and there should be no store id 1 values created, but if you need to then save the product for other updates, it is at this point (on product save) that the store id 1 values will be created. However, if you have already saved the attribute on the product with the new values, it will create 2 attributes at different stores with the same value. I don't know why the Magento API creates a product on store id 0 and then updates the same product with store id 1, any fix or explanation for this issue would be appreciated.
@magento-team @veloraven What's the status on this issue?
@tdgroot I don't think this will be solved for quite some time unfortunately :( The best way to do this is using any of the fixes mentioned in this thread.
Same problem for me.
I wanted to alert you to what I think is a related issue. I've opened ticket #10461 for it.
When updating (remove then add) a product's images through the api, the images are written to catalog_product_entity_media_gallery_value with an incorrect store_id of 1. This is using the $magentoProduct->setMediaGalleryEntries([entries]) function in the API.
Then I think because the store id is incorrect on the media gallery table, the api does not write the image path to the catalog_product_entity_varchar table, with any image attribute, meaning that the images are not displaying in the product UI.
$store = $this->_storeManager->getStore(0);
$this->_storeManager->setCurrentStore($store);
see : https://magento.stackexchange.com/questions/147042/magento-2-import-script-store-id-related-issue
@antonlazarchenko , thank you for your report.
Please try ry to reproduce this issue on fresh and clean installation without any third-party dependencies.
Reopening, due to reports that issue is still reproducible.
@antonlazarchenko, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If someone has some updates, please open a new issue.
This is so weird why is there a store view 0 and store view 1.
They have different values for same API request to update a product...
So the values in Admin e.g. Name or Option Titles don't change only the front end ones do or vice versa depending if you use all or default prefixed in the URL...
This can't be right?
Still a problem on Magento ver. 2.3.2
Fix it fix it fix it fix it.
Most helpful comment
I have the exact same problem. Using Magento 2.1.3. In my case, there is a default "admin" store with id=0, while the "default" has store id = 1. See below.
Whenever any object (usually products) have store id =1, it will always override the value of the same data with store id =0 on the frontend.
After selecting Single store mode, products are only added/updated with store id = 0 as expected when using the Magento admin panel.
However, if we alter products with PHP scripts or modules, for instance using the code by @antonlazarchenko, the data for the product is duplicated for store id =1. See below.
Assuming we have:
It is possible to use
$_product->setStoreId(0);
in order to only affect the data for storeId=0. But this does not apply to every case.
For instance, using
$_productTierPrice->add($sku, 0, $productRetailPromoPrice, 1);
will always duplicate every single attribute of data for that product. And there is no way to specify a store.
Problem is, in Single Store mode, modifying a product will only update its store id=0 values. And so, we have situations like a product shows "disabled" on the backend, but is actually still enabled on the frontend.
So basically, my temporary solution is to erase everything "store id 1" every time I make any modification to a product as such
But I think that if the store is in Single Store mode, Magento should stop entirely to override store id=0 data as it is clearly the one intended to be used.
Please correct me if I'm wrong.