I am trying to learn Magento 2 (community edition v.2.3.4) but I seem to have hit a brick wall while dealing with extension attributes. I did everything according to the documentation https://devdocs.magento.com/guides/v2.3/extension-dev-guide/extension_attributes/adding-attributes.html and https://github.com/magento/magento2-samples/tree/master/sample-external-links but the generated by magento compiler code (class name "AffiliateMemberExtension") does not have the method to add my custom attribute named "sample".
The exact error message I'm getting from REST endpoint is
Fatal Error: 'Uncaught Error: Call to undefined method SimplifiedMagento\\Database\\Api\\Data\\AffiliateMemberExtension::setSample() in \/home\/azumi\/work\/magento2\/app\/code\/SimplifiedMagento\/Attribute\/Model\/Plugin\/CodeAttributeExtension.php:37
My full source code is located here: https://gitlab.com/naevus19/my-magento/-/tree/master/app/code/SimplifiedMagento/Attribute
Any help would be greatly appreciated. I don't know how to debug this further :(
extension_attributes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="\SimplifiedMagento\Database\Api\Data\AffiliateMemberInterface">
<attribute code="sample" type="string" />
</extension_attributes>
</config>
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="\SimplifiedMagento\Database\Api\AffiliateMemberRepositoryInterface">
<plugin name="attribute_code_plugin" type="\SimplifiedMagento\Attribute\Model\Plugin\CodeAttributeExtension" />
</type>
</config>
module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="SimplifiedMagento_Attribute" setup_version="0.1.0" />
</config>
plugin code CodeAttributeExtension.php
<?php
/**
* Created by PhpStorm.
* User: kamil
* Date: 07.05.20
* Time: 16:39
*/
namespace SimplifiedMagento\Attribute\Model\Plugin;
use SimplifiedMagento\Database\Api\AffiliateMemberRepositoryInterface;
use \SimplifiedMagento\Database\Api\Data\AffiliateMemberExtensionFactory;
use SimplifiedMagento\Database\Api\Data\AffiliateMemberInterface;
use SimplifiedMagento\Database\Model\AffiliateMember;
use \SimplifiedMagento\Database\Model\AffiliateMemberRepository;
class CodeAttributeExtension
{
protected $extensionFactory;
public function __construct(AffiliateMemberExtensionFactory $extensionFactory)
{
$this->extensionFactory = $extensionFactory;
}
public function afterGetAffiliateMemberById
(
AffiliateMemberRepositoryInterface $subject,
AffiliateMember $entity
) {
$entity->setCustomAttribute('sample', "Code #" . $entity->getId());
$extensionAttributes = $entity->getExtensionAttributes();
if (empty($extensionAttributes)) {
$extensionAttributes = $this->extensionFactory->create();
}
$extensionAttributes->setSample("Code #" . $entity->getId());
$entity->setExtensionAttributes($extensionAttributes);
return $entity;
}
}