PHP code example of elcommerce / magento2-app-emulation
1. Go to this page and download the library: Download elcommerce/magento2-app-emulation library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
elcommerce / magento2-app-emulation example snippets
declare(strict_types=1);
namespace Vendor\Module\Model;
use Magento\Framework\App\Area;
use Magento\Store\Model\App\Emulation;
class StorefrontOperation
{
private Emulation $appEmulation;
public function __construct(Emulation $appEmulation)
{
$this->appEmulation = $appEmulation;
}
public function execute(int $storeId): void
{
$this->appEmulation->startEnvironmentEmulation(
$storeId,
Area::AREA_FRONTEND,
true
);
try {
// Run code that needs the selected store's design and locale.
// Services called here may start their own emulation safely.
} finally {
$this->appEmulation->stopEnvironmentEmulation();
}
}
}
$emulation->startEnvironmentEmulation($storeA);
$emulation->startEnvironmentEmulation($storeB);
$emulation->stopEnvironmentEmulation(); // Restores store A.
$emulation->stopEnvironmentEmulation(); // Restores the original environment.