Magento 2 Override – How to Override Core Interface and Model

interfacemagento2overrides

I am trying to override core interface of Customer Group.

I add my custom get and set methods to core Api interface.

Vendor\magento\customer-module\Api\Data\GroupInterface.php

Where its working fine.

But when i am trying to override it its doesn't seems to be work.

Here i am sharing my module code.

ZeroCool\AdvacedReviews\Api\Data\GroupInterface.php

<?php

namespace ZeroCool\AdvancedReviews\Api\Data\GroupInterface;


interface GroupInterface extends ExtensibleDataInterface
{   
const SHIPPING_METHOD = 'shipping_method';

/**
 * Get shipping method
 *
 * @return string
 */
public function getShippingMethod();

/**
 * Set shipping method
 *
 * @param int $shippingMethod
 * @return $this
 */
public function setShippingMethod($shippingMethod);

}

ZeroCool\AdvancedReviews\etc\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\Customer\Block\Adminhtml\Group\Edit\Form">
    <plugin name="add_form_field" type="ZeroCool\AdvancedReviews\Model\Plugin\Form" sortOrder="1"/>
</type>
<type name="Magento\Customer\Controller\Adminhtml\Group\Save">
    <plugin name="save_field" type="ZeroCool\AdvancedReviews\Model\Plugin\Form" sortOrder="1"/>
</type>
<preference for="Magento\Customer\Model\Data\Group" type="ZeroCool\AdvancedReviews\Model\Data\Group" /> 
<preference for="Magento\Customer\Api\Data\GroupInterface" type="ZeroCool\AdvancedReviews\Api\Data\GroupInterface" /> 

There's also one question available but it doesn't have any answers till now.

Stack Exchange

Thanks in advance.

Best Answer

Below solution worked for me.

As per MIKE suggested we can't directly override magento's core interfaces.

So than there's comes role of Extension Attributes.

etc/extension_attributes.xml

<?xml version="1.0"?>

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

<extension_attributes for="Magento\Customer\Api\Data\GroupInterface">
    <attribute code="custom_shipping_amount" type="string"/>
</extension_attributes>

</config>

Execute : php bin/magento setup:di:compile

After that your get and set method will generated under below path.

magento_root/generated/code/Magento/Customer/Api/Data/GroupExtensionInterface.php

Now you can use those methods.

$groups->getCustomShippingAmount();
$groups->setCustomShippingAmount();

Hope this will help you.

Related Topic