Skip to content

Instantly share code, notes, and snippets.

@paliarush
Last active May 25, 2018 14:36
Show Gist options
  • Save paliarush/23a5e8dff4b5bb42f6d207435c61810d to your computer and use it in GitHub Desktop.
Save paliarush/23a5e8dff4b5bb42f6d207435c61810d to your computer and use it in GitHub Desktop.
No more boilerplate code for extension attributes object instantiation
<?php
// Before Magento 2.3
$extensionAttributes = $product->getExtensionAttributes();
if (null === $extensionAttributes) {
/** @var \Magento\Catalog\Api\Data\ProductExtensionInterface $extensionAttributes */
$extensionAttributes = $this->extensionAttributesFactory->create(ProductInterface::class);
$product->setExtensionAttributes($extensionAttributes);
}
$myExtensionAttribute = $extensionAttributes->getMyExtensionAttribute();
// After Magento 2.3
$myExtensionAttribute = $product->getExtensionAttributes()->getMyExtensionAttribute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment