6

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 ?

  • You cannot override interface classes. Use extension attribute featured instead. If not you need to create your own interface – Ranganathan Dec 26 '19 at 12:03
  • extension_attribute method is not available inside Custom\Module\Api\Data\AccountInterface so how we can use it ? – rakesh prajapati Dec 26 '19 at 12:06
  • You need to create your new custom interface. There is no option. – Ranganathan Dec 26 '19 at 12:08
1

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

| improve this answer | |
  • extension_attribute method is not available inside Custom\Module\Api\Data\AccountInterface so how we can use it ? – rakesh prajapati Dec 26 '19 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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