{"id":85445,"date":"2017-06-03T10:11:01","date_gmt":"2017-06-03T10:11:01","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=85445"},"modified":"2024-03-06T09:17:51","modified_gmt":"2024-03-06T09:17:51","slug":"magento2-store-emulation","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/","title":{"rendered":"Magento2 Store Emulation"},"content":{"rendered":"\n<p>In a system like Magento2, it&#8217;s quite difficult to handle some situation while coding like updating the data according to correct scope, people end up with the ideas like changing the store scope by using setCurrentStoreId() \u00a0method like below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">$currentStoreId = $this-&gt;_storeManager-&gt;getStore()-&gt;getId();\n\/\/ Magento\\Store\\Model\\StoreManagerInterface\n$this-&gt;_storeManager-&gt;setCurrentStoreId($storeId);\n\n\/\/ ...\n\/\/ your code \n\/\/ ....\n\n$this-&gt;_storeManager-&gt;setCurrentStoreId($currentStoreId);<\/pre>\n\n\n\n<p>and then after doing their work again setting that value back to the old one, but this is not the efficient way since there can be many other things that you need to change to create the store&#8217;s environment, so changing them like that does not seem a better solution, that&#8217;s&nbsp;why Magento has provided a class to do such things, <span class=\"crayon-sy\">\\<\/span><span class=\"crayon-v\">Magento<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-v\">Store<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-v\">Model<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-v\">App<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-i\">Emulation this class provides two methods:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>startEnvironmentEmulation($storeId, $area= \\Magento\\Framework\\App\\Area::AREA_FRONTEND, $force=false)<\/li>\n\n\n\n<li>stopEnvironmentEmulation()<\/li>\n<\/ul>\n\n\n\n<p>An example of how we can use these methods to emulate the store environment:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/**\n  * \\Magento\\Store\\Model\\App\\Emulation\n  *\/\n protected $emulation;\n\npublic function __construct(\\Magento\\Store\\Model\\App\\Emulation $emulation) {\n   $this-&gt;emulation = $emulation;\n}\n\n\/**\n * example function in which we want to emulate a stores environment  \n *\/\npublic function example($storeId, $area = &#039;frontend&#039;) {\n  \n   \/\/starting the store emulation with area defined for admin\n   $this-&gt;emulation-&gt;startEnvironmentEmulation($storeId, &#039;adminhtml&#039;);\n   \n   \/\/you can update or save a product attributes here with correct scope or anything else you want to do, perform some test\n   \n   \/\/ discard the emulated environment after doing your work\n   $this-&gt;emulation-&gt;stopEnvironmentEmulation();\n   \n}<\/pre>\n\n\n\n<p>Hope this blog will help you in better code implementation. Feel free to ask your doubts in the comment.<\/p>\n\n\n\n<p>Thanks<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a system like Magento2, it&#8217;s quite difficult to handle some situation while coding like updating the data according to correct scope, people end up with the ideas like changing the store scope by using setCurrentStoreId() \u00a0method like below: and then after doing their work again setting that value back to the old one, but <a href=\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":33,"featured_media":80140,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302],"tags":[],"class_list":["post-85445","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Magento2 Store Emulation - Webkul Blog<\/title>\n<meta name=\"description\" content=\"it&#039;s quite difficult to handle some situation while coding like updating the data according to correct scope\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Magento2 Store Emulation - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"it&#039;s quite difficult to handle some situation while coding like updating the data according to correct scope\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-03T10:11:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-06T09:17:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/04\/Magneto-Code-Snippet.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ashutosh Srivastava\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashutosh Srivastava\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/\"},\"author\":{\"name\":\"Ashutosh Srivastava\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970\"},\"headline\":\"Magento2 Store Emulation\",\"datePublished\":\"2017-06-03T10:11:01+00:00\",\"dateModified\":\"2024-03-06T09:17:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/\"},\"wordCount\":160,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/04\/Magneto-Code-Snippet.png\",\"articleSection\":[\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/\",\"url\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/\",\"name\":\"Magento2 Store Emulation - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/04\/Magneto-Code-Snippet.png\",\"datePublished\":\"2017-06-03T10:11:01+00:00\",\"dateModified\":\"2024-03-06T09:17:51+00:00\",\"description\":\"it's quite difficult to handle some situation while coding like updating the data according to correct scope\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/04\/Magneto-Code-Snippet.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/04\/Magneto-Code-Snippet.png\",\"width\":825,\"height\":260},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Magento2 Store Emulation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970\",\"name\":\"Ashutosh Srivastava\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Ashutosh Srivastava\"},\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/ashutosh\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Magento2 Store Emulation - Webkul Blog","description":"it's quite difficult to handle some situation while coding like updating the data according to correct scope","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/","og_locale":"en_US","og_type":"article","og_title":"Magento2 Store Emulation - Webkul Blog","og_description":"it's quite difficult to handle some situation while coding like updating the data according to correct scope","og_url":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2017-06-03T10:11:01+00:00","article_modified_time":"2024-03-06T09:17:51+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/04\/Magneto-Code-Snippet.png","type":"image\/png"}],"author":"Ashutosh Srivastava","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Ashutosh Srivastava","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/"},"author":{"name":"Ashutosh Srivastava","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970"},"headline":"Magento2 Store Emulation","datePublished":"2017-06-03T10:11:01+00:00","dateModified":"2024-03-06T09:17:51+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/"},"wordCount":160,"commentCount":2,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/04\/Magneto-Code-Snippet.png","articleSection":["Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/magento2-store-emulation\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/","url":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/","name":"Magento2 Store Emulation - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/04\/Magneto-Code-Snippet.png","datePublished":"2017-06-03T10:11:01+00:00","dateModified":"2024-03-06T09:17:51+00:00","description":"it's quite difficult to handle some situation while coding like updating the data according to correct scope","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/magento2-store-emulation\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/04\/Magneto-Code-Snippet.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/04\/Magneto-Code-Snippet.png","width":825,"height":260},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/magento2-store-emulation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Magento2 Store Emulation"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970","name":"Ashutosh Srivastava","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Ashutosh Srivastava"},"sameAs":["http:\/\/webkul.com"],"url":"https:\/\/webkul.com\/blog\/author\/ashutosh\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/85445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=85445"}],"version-history":[{"count":8,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/85445\/revisions"}],"predecessor-version":[{"id":426244,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/85445\/revisions\/426244"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/80140"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=85445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=85445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=85445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}