Skip to content

Does area emulation also influence DI / plugins?

We are reading the Magento configurable configuration inside an API call:

   private function getSpConfig($product)
    {
        if ($product->getTypeId() != 'configurable') {
            return null;
        }

        /**
         * @var ConfigurableBlock
         */
        $configurableBlock = $this->_layoutInterface->createBlock(ConfigurableBlock::class);
        $configurableBlock->setData('product', $product);
        $spConfig = json_decode($configurableBlock->getJsonConfig(), true);

        usort($spConfig['attributes'], function ($a, $b) {
            return $a['position'] <=> $b['position'];
        });

        return $spConfig;
    }

It turns out that the sku field is missing, which is set in MagentoInventoryConfigurableProductFrontendUiPluginConfigurableProductBlockProductViewTypeAddAdditionalInfo::afterGetJsonConfig

It is defined in the frontend’s di.xml

The call is wrapped into emulation:

        $storeId = $this->_storeManager->getStore()->getId();
        $this->_appEmulation->startEnvironmentEmulation($storeId, MagentoFrameworkAppArea::AREA_FRONTEND, true);

        $this->state->emulateAreaCode(MagentoFrameworkAppArea::AREA_FRONTEND, function() use ($productCollection, $customerId, $field, &$response) {
   
//              ... getSpConfig ...

        });

        $this->_appEmulation->stopEnvironmentEmulation();

Should this work or does the app emulation not cover DI ?