Magento – Magento 2: Import Script Store Id Related Issue

customimportmagento2product

I'm using Magento 2 CE Version 2.1.3

Magento 2: Save all product data outside Magento with Images

Once I imported data from CSV using above script.

If put as ->setStoreId(0), then it will add two values for same Field In Database Table. One as Store Id 0 & Second As Store Id 1.

Example: catalog_product_entity_varchar

value_id    attribute_id store_id entity_id     value
100           70           0       10           Test
101           70           1       10           Test <- NOT NEEDED ENTRY

attribute_id 70 is for name (Product Name) from Table eav_attribute

Due to 2 values for Same Field. Each time have to delete Store Id 1 related value from Database respective Table, then it works fine on Frontend.

At the end data is not displaying properly as needed. Have to do each time Delete Query for Store Id in 5-6 tables.

Go to Admin -> Stores -> Configuration -> General -> General -> Single Store Mode

It always been issue. On Admin side it's taking Store Id 0 value & on frontend displays it's taking Store Id 1 Value.

Enable Single-Store Mode set to "Yes"

On Admin -> All Store Views -> Store Id = 0 -> Admin Using This
On Admin -> Default Store Views -> Store Id = 1 -> Frontend Using This

What i'm still missing in Script? Does Enable Single-Store Mode set to "Yes" helps?

Other workaround will be set Admin to Default Store Views. In my case i have only 1 store. So may be works.

While we do Default Magento Import/Export. It only uses Store Id 0 & it works fine.

Best Answer

$objectManager->get('Magento\Store\Model\StoreManagerInterface')->setCurrentStore('admin');

before importing the products will have the products use store id 0 ('admin') only

Looking at module-catalog\Model\ProductRepository.php:save() (Magento 2.1.2) it seems that the store id is retrieved from the current store on save, and not the one set via Product::setStoreId():

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