Magento 2 : Override core Interface and Model

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.


Magento 2 Preference does not allow us to override the interfaces. Preferences are used to specify the implementation classes for the interfaces and overriding implementation classes. It does not allow to override interface with an interface

Then, how can we override the interfaces to send our extra informations over the interface? That's where the beautiful concept comes in - Extensions Attributes.

From the official doc:

Extension attributes are new in Magento 2. They are used to extend functionality and often use more complex data types than custom attributes. These attributes do not appear on the GUI.

In your case, if you check the \Magento\Customer\Api\Data\GroupInterface interface, you can find these 2 methods at the bottom, which helps you to send your shipping information through this methods.

 /**
 * Retrieve existing extension attributes object or create a new one.
 *
 * @return \Magento\Customer\Api\Data\GroupExtensionInterface|null
 */
public function getExtensionAttributes();

/**
 * Set an extension attributes object.
 *
 * @param \Magento\Customer\Api\Data\GroupExtensionInterface $extensionAttributes
 * @return $this
 */
public function setExtensionAttributes(\Magento\Customer\Api\Data\GroupExtensionInterface $extensionAttributes);

For more information on how to add extension attributes, http://devdocs.magento.com/guides/v2.0/extension-dev-guide/extension_attributes/adding-attributes.html