Magento is famous for is its capabilities to develop multi-store and multi-language web stores. Each store can be completely different from its fellow stores. Store Emulation helps developers to emulate any store programmatically.
Store emulation is also know as Area Emulation and App Emulation.
Suppose you want to send custom transactional e-mail for all stores at the same time as cronjob.
When a process run on backend for example cron jobs or email notifications, these processes work under admin store.
But suppose if you want to send transactional e-mail on store wise, then email template need grab from store view.
You can enable App Emulation and perform this action.
Let’s check an quick example which demonstrates store emulation cron running for specific store.
I’m supposing that you have created your module or if not check this tutorial for create a custom module.
Let’s get started !!
Magento has provided a class to do such things, \Magento\Store\Model\App\Emulation this class provides two methods:
1 2 3 4 5 6 7 8 9 |
//1. Start Emulation by passing following params startEnvironmentEmulation( $storeId, $area = \Magento\Framework\App\Area::AREA_FRONTEND, $force = false ) //2. Stop Emulation after completing work stopEnvironmentEmulation() |
An example of how we can use these methods to emulate the store environment :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php declare(strict_types=1); namespace Jigar\CustomModule\Cron; use Magento\Framework\App\Area; use Magento\Store\Model\App\Emulation; class AppEmulationDemo { /** * @var Emulation */ protected $emulation; /** * Constructor * * @param Emulation $emulation */ public function __construct(Emulation $emulation) { $this->emulation = $emulation; } /** * Example function in which we want to emulate a store environment * * @return void */ public function execute() { $storeId = 1; // starting the store emulation by specifying storeId , Area & Force $this->emulation->startEnvironmentEmulation($storeId, Area::AREA_FRONTEND, true); // your code here // discard the emulated environment after doing your work $this->emulation->stopEnvironmentEmulation(); } } |
This way you can emulate area in Magento 2. I hope you like the tutorial, Let me know if you have any query in the comment box or contact me.