2

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.

  • You can try with $state->setAreaCode('adminhtml') in your script; – Rakesh Jesadiya Dec 16 '16 at 5:09
  • It will solve Frontend & Backend Both issues? No need to setStoreId ? – Ankit Shah Dec 16 '16 at 5:48
  • I havent try with that but try with that so may be generate one entry in table – Rakesh Jesadiya Dec 16 '16 at 5:58
  • Hi @Rakesh have tried. Not working :( I have removed setStoreId & setAreaCode as adminhtml. After Import, if anything i changes on Admin it's not refelcting on Frontend. – Ankit Shah Dec 16 '16 at 6:51
  • Hi @AnkitShah I am facing the same issue. Did you find any solution? – Sarvagya Mar 23 '18 at 6:31
3
$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();
  • So what is solution? – Ankit Shah Dec 23 '16 at 2:37
  • Set the current store to 'admin' once before saving your products in the import process: $objectManager->get('Magento\Store\Model\StoreManagerInterface')->setCurrentStore('admin'); – Sébastien Trottier Dec 23 '16 at 14:15
  • It's giving fatal error Fatal error: Uncaught exception 'ReflectionException' with message 'Class Magento\Store\Model\StoreManagerInterfa‌​ce does not exist' – Ankit Shah Dec 27 '16 at 1:14
  • You save my day. Just saying you should NEVER use object manager but use it as a dependency. this worked for me : $store = $this->_storeManager->getStore(0); $this->_storeManager->setCurrentStore($store); – CompactCode Nov 24 '17 at 18:38
1

Here's one thing to try. Generally in Magento, at least in v1 (and I think the pattern would carry over to v2), the right way to change the area of the store is through emulation. In Magento 2, we use the Magento\Framework\App\State::emulateAreaCode() method for this.

Try using that to run your script, and let me know how it works. Use it like this: $state->emulateAreaCode('frontend', $callable), where $callable is the code you want to run. For this approach, you will have to move your import code into a separate function.

Let me know if that works.

  • Hi @tjons. As u seen in question. I have put the link in beginning. use Magento\Framework\App\Bootstrap; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); What should be value of $callable? – Ankit Shah Dec 20 '16 at 2:56
  • Move your script import process into a separate function in the same file. Then pass the name to it as a string, e.g., emulateAreaCode('frontend', 'yourfuncname');. Let me know if that works... – tjons Dec 20 '16 at 12:59
  • Ok got it @tjons. Let me check & revert back to you. Thanks. Does it affect Enable Single-Store Mode set to "Yes"? – Ankit Shah Dec 21 '16 at 1:42
  • Nope. Still issue remains same :( – Ankit Shah Dec 21 '16 at 1:50
  • @AnkitShah can you add your script to the question? Not a link, but the actual code you're trying to run... – tjons Dec 21 '16 at 2:26
0

Try this before saving product from custom script csv import

$_product->setData('store_id', 0);

$_product->save();

This should be only one record storeId 0 in your database

I have tested and fixed same above issue due to custom csv import module

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.