11

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.

2

3 Answers 3

11

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.

5
  • How can I override Interface and Mode. Please let me know ? Magento\Catalog\Api\Data\ProductCustomOptionValuesInterface.php Magento\Catalog\Model\Product\Option\Value.php Oct 18, 2019 at 10:12
  • Could you please help me on this question. magento.stackexchange.com/questions/293336/… Oct 31, 2019 at 5:47
  • as you said get and set methods will be generated in generated folder. they automatically be generated in that folder or we have to generate them?
    – Asad Ullah
    Jul 2, 2020 at 19:58
  • Read carefully... :) I added that after executing bin/magento setup:di:compile you'll get those methods.
    – anonymous
    Jul 3, 2020 at 5:48
  • @anonymous in which file i can use getCustomShippingAmount and setCustomShippingAmount methods?
    – m2dubai
    Aug 24, 2021 at 14:18
10

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

3
  • How can I override Interface and Mode. Please let me know ? Magento\Catalog\Api\Data\ProductCustomOptionValuesInterface.php Magento\Catalog\Model\Product\Option\Value.php Oct 18, 2019 at 10:17
  • What about the interfaces that does not have these two functions. but I still need to push some data on those objects in API call?
    – Nahid
    Feb 24, 2020 at 10:35
  • @Nahid did you get any solution for that, I am also in same situation where my core interface doesn't have extension attribute functions
    – Arjun
    Feb 16, 2022 at 6:28
0

Please find below answer to add custom description as an extension attribute.

/etc/extension_attributes.xml should be like this:

<?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\Quote\Api\Data\ShippingMethodInterface">
        <attribute code="method_description" type="string" />
    </extension_attributes>
</config>

In the etc/di.xml file add a plugin for overriding modelToDataObject() in Magento\Quote\Model\Cart\ShippingMethodConverter as below:

<?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\Quote\Model\Cart\ShippingMethodConverter">
        <plugin name="add_description_to_carrier" type="<Vendor>\<module>\Plugin\Carrier\Description" disabled="false" sortOrder="30"/>
    </type>
</config>

The plugin file Vendor\module\Plugin\Carrier\Description.php should be like this:

<?php

namespace Vendor\module\Plugin\Carrier;

use Magento\Quote\Api\Data\ShippingMethodExtensionFactory;

/**
 * Class Description
 * 
 */
class Description
{
    /**
     * @var ShippingMethodExtensionFactory
     */
    protected $extensionFactory;

    /**
     * Description constructor.
     * @param ShippingMethodExtensionFactory $extensionFactory
     */
    public function __construct(
        ShippingMethodExtensionFactory $extensionFactory
    )
    {
        $this->extensionFactory = $extensionFactory;
    }

    /**
     * @param $subject
     * @param $result
     * @param $rateModel
     * @return mixed
     */
    public function afterModelToDataObject($subject, $result, $rateModel)
    {
        $extensionAttribute = $result->getExtensionAttributes() ?
            $result->getExtensionAttributes()
            :
            $this->extensionFactory->create()
        ;
        $extensionAttribute->setMethodDescription($rateModel->getMethodDescription());
        $result->setExtensionAttributes($extensionAttribute);
        return $result;
    }
}

After all this you will get that description on fronend as below:

<div data-bind="text: method.extension_attributes.method_description"></div>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.