As per Magento 1, there will be instances when you need to emulate another store to ensure the correct configuration and data is loaded for a specific action. This operation is still similar to how Magento 1 completes this.

This is all handled by the \Magento\Store\Model\App\Emulation class.

To emulate another store you need to require this as a dependency in your construct:

Then later on within the code you’ll simply need to run:

Once you’ve finished any logic that requires the emulated environment you can restore the original environment by running:

Dave Macaulay
Dave Macaulay
I'm an enthusiastic, slightly eccentric Magento developer who bores with the idea of another basic brief.