December 12, 2023

Magento 2 Create a Product Extension Attribute

Embark on a journey of refined customization in Magento development, a strategic imperative for tailoring solutions to meet your unique business requirements. Our team of seasoned Magento 2 experts, available for hire, is ready to guide you through seamless integration, ensuring an optimal balance between flexibility and platform integrity. As a reputable Magento Development Company, we specialize in Magento for eCommerce development and Magento website development.

 

There are two types of Attributes:

  • Scalar Attributes: Attributes that can only contain scalar values, that is, boolean, int, float, or string values.
  • Non-scalar Attributes: Attributes that can use objects to have more complex types. You will require to create a custom interface for these attributes.

Extension Attribute Configuration file :

 /vendor/module-quote/etc/extension_attributes.xml

 

				
					<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
        <attribute code="additional_price" type="int" />
    </extension_attributes>
</config>
				
			

The extension_attribute.xml file is allowed to declare the extension attribute code and the type of that entity we want to add for them.

Basically, the ExtensibleDataInterface interface is used as a parent interface for an entity interface.
The \Magento\Catalog\Api\Data\ProductInterface interface provides setExtensionAttributes() and getExtensionAttributes() methods in addition to all other getter and setter methods that help to operate with customer-related data.

So We need at least two plugins to use the getExtensionAttributes() and setExtensionAttributes().

  • Add Plugin To Product Repository to Get Extension Attribute

We are creating a plugin in di.xml.

				
					<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Api\ProductRepositoryInterface">
        <plugin name="get_after_product_get_our_additional_price" type="Vendor\Module\Plugin\ProductAdditionalGetPrice" />
    </type>
</config>
				
			
  • Add Plugin To Product Repository to Set Extension Attribute
				
					<?php
namespace Vendor\Module\Plugin;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Vendor\Module\Helper\Data;
use Vendor\Module\Api\OurRepositoryInterface;

class ProductAdditionalGetPrice
{
    const ADDITIONAL_PRICE ='additional_price';
    /**
     * @var Data
     */
    protected $_helperData;

    /**
     * @var OurRepositoryInterface
     */
    protected $ourRepositoryInterface;

    /**
     * @var \Magento\Framework\App\Request\Http
     */
    protected $request;
    /**
     * @param ProductExtensionFactory $extensionFactory
     */
    public function __construct(
        Data $helperData,
        OurRepositoryInterface $ourRepositoryInterface,
        \Magento\Framework\App\Request\Http $request
    ) {
        $this->_helperData = $helperData;
        $this->ourRepositoryInterface = $ourRepositoryInterface;
        $this->request = $request;
    }
    public function afterSave
    (
        ProductRepositoryInterface $subject,
        ProductInterface $result, /** result from the save call **/
        ProductInterface $entity  /** original parameter to the call **/
        /** other parameter not required **/
    ) {
        $extensionAttributes = $entity->getExtensionAttributes(); /** get original extension attributes from entity **/
        $ourCustomData = $extensionAttributes->getAdditionalPrice();
        $this->ourRepositoryInterface->save($ourCustomData);

        $resultAttributes = $result->getExtensionAttributes(); /** get extension attributes as they exist after save **/
        $resultAttributes->setAdditionalPrice($ourCustomData); /** update the extension attributes with correct data **/
        $result->setExtensionAttributes($resultAttributes);

        return $result;
    }
}
				
			

Note : We have only use the getAfter and saveAfter you can also pass the extention attribute data in the getList call.

The Result looks likes as:

				
					{
      "sku": "Jeans",
      "price": "4220.00",
      "description": "New design",
      "extension_attributes": {
        "additional_price": 10
      }
    }
				
			

Unlock the full potential of extension attributes to enhance your online store’s functionalities, deliver unparalleled user experiences, and achieve your business objectives with finesse. Collaborate with our team and let’s elevate your Magento 2 development to new heights.

Author

Share this post:
Facebook
Twitter
LinkedIn
WhatsApp

Discover more articles