7

I have tried with preference to override interface as below in di.xml file

<preference for="Custom\Module\Api\Data\AccountInterface" type="Override\Module\Api\Data\AccountInterface" />

<preference for="Custom\Module\Model\Account" type="Override\Module\Model\Account" />

And My Interface file is like as below

Override\Module\Api\Data\AccountInterface.php

<?php

namespace Override\Module\Api\Data;

interface AccountInterface extends \Custom\Module\Api\Data\AccountInterface {

    const AFFILIATE_ID = 'custom_id';

    /**
     * @return string|null
     */
    public function getCustomId();

    /**
     * @param string $custom_id
     *
     * @return \Custom\Module\Api\Data\AccountInterface
     */
    public function setCustomId($custom_id);
}

Override Model File with new method Logic as below

Override\Module\Model\Account.php

<?php

namespace Override\Module\Model;

use Custom\Module\Api\Data\AccountInterface;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;

class Account extends \Custom\Module\Model\Account {
   /**
 * {@inheritdoc}
 */
public function getCustomId() {
    return $this->_getData('custom_id');
}

/**
 * {@inheritdoc}
 */
public function setCustomId($customId) {
    $this->setData('custom_id', $customId);
    return $this;
}
}

But it's not save the value of custom_id using setter and getter methods...

If use same setter and getter method and Model logic in Custom_Module directly then it's working fine.

Any solution of add custom method in Interface ?

note :- extension_attribute method is not available inside Custom\Module\Api\Data\AccountInterface so how we can use it ?

4
  • You cannot override interface classes. Use extension attribute featured instead. If not you need to create your own interface Dec 26, 2019 at 12:03
  • extension_attribute method is not available inside Custom\Module\Api\Data\AccountInterface so how we can use it ? Dec 26, 2019 at 12:06
  • You need to create your new custom interface. There is no option. Dec 26, 2019 at 12:08
  • @rakeshprajapati, did you found solution for this issue ? Apr 14, 2022 at 4:15

1 Answer 1

2

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

2
  • 1
    extension_attribute method is not available inside Custom\Module\Api\Data\AccountInterface so how we can use it ? Dec 26, 2019 at 12:15
  • it may have been a long time, but I want to know how you solve it if there is no extension attribute in Custom\Module\Api\Data\AccountInterface. Or just add getExtensionAttributes() and setExtensionAttributes() in custom module ? it's possible to do, I think all modules in magento have extension_attribute method
    – Ismynr
    Apr 8, 2022 at 3:53

Your Answer

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

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