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 Magento\InventoryConfigurableProductFrontendUi\Plugin\ConfigurableProduct\Block\Product\View\Type\AddAdditionalInfo::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, \Magento\Framework\App\Area::AREA_FRONTEND, true);
$this->state->emulateAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND, function() use ($productCollection, $customerId, $field, &$response) {
// ... getSpConfig ...
});
$this->_appEmulation->stopEnvironmentEmulation();
Should this work or does the app emulation not cover DI ?