cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 : Override core interface and model

Magento 2 : Override core interface and model

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" /> 
 <!-- <preference for="Magento\Customer\Model\Group\Edit" type="ZeroCool\AdvancedReviews\Block\Adminhtml\Group\Edit" />   -->
</config>
	  
	 

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

 

Magento stack exchange

 

Thanks in advance.

 

 

5 REPLIES 5

Re: Magento 2 : Override core interface and model

I see that in your code this line is missing

use Magento\Framework\Api\ExtensibleDataInterface;

Please try below code.

<?php

namespace ZeroCool\AdvancedReviews\Api\Data\GroupInterface;

use Magento\Framework\Api\ExtensibleDataInterface;
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);

}


Please give Kudos if it helps and Accept it as a solution

Re: Magento 2 : Override core interface and model

You can use extension attributes to add your custom additional attribute in the interface.

 

Create extension_attributes.xml at following location

 

app/code/Vendor/Module/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\Customer\Api\Data\GroupInterface"> <attribute code="custom_shipping_method" type="string"/> </extension_attributes> </config>

 

Now you can get and set your custom shipping method using following code.

 

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$groupInterface = $objectManager->create('Magento\Customer\Api\Data\GroupInterface');
$extAttributes = $groupInterface->getExtensionAttributes();
 
$selectedShipping = $extAttributes->getCustomShippingMethod(); //get custom_shipping_method data.

 

 

If you find my answer useful, Please click Kudos & Accept as Solution.

Re: Magento 2 : Override core interface and model

You can use extension attributes to add the custom attribute in the interface.

Create extension_attributes.xml at following location

 

app/code/Vendor/Module/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\Customer\Api\Data\GroupInterface">
        <attribute code="custom_shipping_method" type="string"/>
    </extension_attributes>
</config>

 

 

Now you can get and set your extension attributes using following code.

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$groupInterface = $objectManager->create('Magento\Customer\Api\Data\GroupInterface');
$extAttributes = $groupInterface->getExtensionAttributes();

$customShippingMethod = $extAttributes->getCustomShippingMethod(); //get custom_shipping_method data.

 

If you find my answer useful, Please click Kudos & Accept as Solution.

Re: Magento 2 : Override core interface and model

Please find below answer which I'm using Magento\Quote\Api\Data\ShippingMethodInterface and in this interface I need 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>

 

Re: Magento 2 : Override core interface and model

<b>Fatal error</b>:  Uncaught Error: Call to undefined method Magento\Quote\Api\Data\ShippingMethodExtension::setMethodDescription() in ...

Everything is done as shown by you. Why this method is "undefined"?