Reading list Switch to dark mode

    How to join extension attribute in Magento 2

    Updated 16 February 2024

    In this article, We will learn about extension attribute join in Magento 2.

    In Magento, an extension attribute is a customization mechanism that allows developers to add extra fields or properties to the existing data structures of entities like products, customers, or orders. These attributes extend the core functionality without modifying the core code.

    Extension attributes are particularly useful when you want to associate additional information with these entities without altering the database schema. They enable smoother upgrades and compatibility with other extensions. Developers define extension attributes in the XML configuration, providing a flexible way to enhance data models without compromising the integrity of the core system.

    Extension attribute joins in Magento developers allow to efficiently retrieve additional data from related tables when querying entities like products or orders. This enhances data retrieval and customization capabilities without modifying the core database structure.

    To join extension attributes in Magento 2, follow these steps:

    Start your headless eCommerce
    now.
    Find out More

    Step 1:- Define Extension Attribute

    Create your extension attribute using XML in your module’s etc/extension_attributes.xml file. Specify the entity you want to extend (e.g., product, order) and define the attribute structure.

    <?xml version="1.0"?>
    <!--
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_ExtentionAttribute
     * @author    Webkul Software Private Limited
     * @copyright Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
        <extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
            <attribute code="custom_col1" type="string">
                <join reference_table="custom_table" join_on_field="entity_id" reference_field="order_id">
                    <field column="custom_col1">custom_col1</field>
                </join>
            </attribute>
            <attribute code="custom_col2" type="string">
                <join reference_table="custom_table" join_on_field="entity_id" reference_field="order_id">
                    <field column="custom_col2">custom_col1</field>
                </join>
            </attribute>
        </extension_attributes>
    </config>

    Step 2:- Create db_schema.xml at app/code/Webkul/ExtentionAttribute/etc

    <?xml version="1.0"?>
    <!--
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_ExtentionAttribute
     * @author    Webkul Software Private Limited
     * @copyright Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    -->
    <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
        <table name="custom_table" engine="innodb">
            <column xsi:type="int" name="order_id"  padding="10" unsigned="true" nullable="false"/>
            <column xsi:type="varchar" name="custom_col1" nullable="false" default="First"/>
            <column xsi:type="varchar" name="custom_col2" nullable="false" default="Second"/>
            <constraint xsi:type="primary" referenceId="PRIMARY">
                <column name="order_id"/>
            </constraint>
            <constraint xsi:type="foreign" referenceId="CUSTOM_TABLE_ORDER_ID_SALES_ORDER_ENTITY_ID"
                    table="custom_table" column="order_id" referenceTable="sales_order"
                    referenceColumn="entity_id" onDelete="CASCADE"/>
        </table>
    </schema>

    Step 3:- Create webapi.xml at app/code/Webkul/ExtentionAttribute/etc

    <?xml version="1.0" ?>
    <!--
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_ExtentionAttribute
     * @author    Webkul Software Private Limited
     * @copyright Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    -->
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    	<route method="GET" url="/V1/getOrderList">
    		<service class="Webkul\ExtentionAttribute\Api\OrderManagementInterface" method="getList"/>
    		<resources>
    			<resource ref="anonymous"/>
    		</resources>
    	</route>
    </routes>

    Step 4:- Create  di.xml at app/code/Webkul/ExtentionAttribute/etc

    <?xml version="1.0" ?>
    <!--
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_ExtentionAttribute
     * @author    Webkul Software Private Limited
     * @copyright Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <preference for="Webkul\ExtentionAttribute\Api\OrderManagementInterface" type="Webkul\ExtentionAttribute\Model\OrderManagement"/>
    </config>

    Step 5:- Create  OrderManagementInterface.php at app/code/Webkul/ExtentionAttribute/Api

    <?php
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_ExtentionAttribute
     * @author    Webkul Software Private Limited
     * @copyright Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    namespace Webkul\ExtentionAttribute\Api;
     
    interface OrderManagementInterface {
    
     /**
      * Get order List
      * 
      * @return array
      */
     public function getList();
    }

    Step 6:-Create OrderManagement.php at app/code/Webkul/ExtentionAttribute/Model

    <?php
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_ExtentionAttribute
     * @author    Webkul Software Private Limited
     * @copyright Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    namespace Webkul\ExtentionAttribute\Model;
     
    class OrderManagement {
     
     /**
      * Construct function
      *
      * @param \Magento\Sales\Api\OrderRepositoryInterface $repository
      * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
      * @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
      * @param \Magento\Framework\Api\Filter $filter
      */
        public function __construct(
            \Magento\Sales\Api\OrderRepositoryInterface $repository,
            \Magento\Framework\Api\SearchCriteriaInterface  $searchCriteria,
            \Magento\Framework\Api\Search\FilterGroup $filterGroup,
     \Magento\Framework\Api\Filter $filter
        ) {
            $this->repository = $repository;
            $this->searchCriteria = $searchCriteria;
            $this->filterGroup = $filterGroup;
     $this->filter = $filter;
        }
    
     /**
      * @inheritdoc
      */
     public function getList()
     {
     $returnArr = [];
     $repository = $this->repository;
     $searchCriteria = $this->searchCriteria;
     $filterGroup = $this->filterGroup;
     $filter = $this->filter;
     $filter->setField('entity_id');
     $filter->setValue('10');
     $filter->setConditionType('eq'); 
     $filterGroup->setFilters([$filter]);
     $searchCriteria->setFilterGroups([$filterGroup]);
     $orderList = $repository->getList($searchCriteria);
     foreach ($orderList->getItems() as $order) {
               $extensionAttribute = $order->getExtensionAttributes();
        $returnArr = [
         $extensionAttribute->getCustomCol1(),
         $extensionAttribute->getCustomCol2()
        ];
            }
     return $returnArr;
     }
    }

    Step 7:- Load Extension Attribute Data After the join,

    Join

    However, in case of any query or questions regarding the Magento 2 Extensions, you can create a ticket at webkul.uvdesk.com or contact us at store.webkul.com/contacts/ to let us know your views to make the plugin better.

    Hope this will help you.

    Thanks 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home