Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    magento-hyperlocal-marketplace-guide

    Magento2 Store Emulation

    In a system like Magento2, it’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()  method like below:


    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’s environment, so changing them like that does not seem a better solution, that’s why Magento has provided a class to do such things, \Magento\Store\Model\App\Emulation this class provides two methods:

    • startEnvironmentEmulation($storeId, $area= \Magento\Framework\App\Area::AREA_FRONTEND, $force=false)
    • stopEnvironmentEmulation()

    An example of how we can use these methods to emulate the store environment:


    Hope this blog will help you in better code implementation. Feel free to ask your doubts in the comment.

    Thanks

     

    . . .

    Comments (2)

    Add Your Comment

  • ashutosh srivastava
    no it does nojt mean to edit the core files, and you can put any code in between the start emulation and stop :
    $this->emulation->startEnvironmentEmulation($storeId, ‘adminhtml’);

    //you can update or save a product attributes here with correct scope or anything else you want to do, perform some test

    // discard the emulated environment after doing your work
    $this->emulation->stopEnvironmentEmulation();

    code can be saving a product, category, get frontend url from admin etc

  • Suman
    Does this means we need to edit core files and check for the operation? Do you have full code how to use this one?
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close