For some unique customization, we need to create customer extension attribute to fulfil client requirements in magento2.
In this blog, I created extension attribute with different datatype.
is_happy_customer boolean
happy_message string
happy_number int
happy_list array
happy_object object
First create file 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\CustomerInterface">
<attribute code="is_happy_customer" type="boolean"/>
<attribute code="happy_message" type="string"/>
<attribute code="happy_number" type="int"/>
<attribute code="happy_list" type="array"/>
<attribute code="happy_object" type="MageDad\Module\Api\Data\HappyObjectInterface[]"/>
</extension_attributes>
</config>
Now we need to create interface and model for HappyObject
Create interface at app/code/MageDad/Module/Api/Data/HappyObjectInterface.php
<?php
declare(strict_types=1);
namespace MageDad\Module\Api\Data;
use Magento\Framework\Api\ExtensibleDataInterface;
interface HappyObjectInterface extends ExtensibleDataInterface
{
public const ENTITY_ID = 'entity_id';
public const HAPPY_MESSAGE = 'happy_message';
/**
* @return int|null
*/
public function getId(): ?int;
/**
* @param int|null $entityId
* null required for create queries @see \Magento\Framework\Model\ResourceModel\Db\AbstractDb::isObjectNotNew
* @return $this
*/
public function setId(?int $entityId): static;
/**
* @return string
*/
public function getHappyMessage(): string;
/**
* @param string $country
* @return $this
*/
public function setHappyMessage(string $happyMessage): static;
}
Create model at app/code/MageDad/Module/Model/HappyObject.php
<?php
declare(strict_types=1);
namespace MageDad\Module\Model;
use Magento\Framework\Model\AbstractModel;
use MageDad\Module\Api\Data\HappyObjectInterface;
class HappyObject extends AbstractModel implements HappyObjectInterface
{
/**
* @inheritDoc
*/
public function getId(): ?int
{
return (int)$this->getData(HappyObjectInterface::ENTITY_ID);
}
/**
* @inheritDoc
*/
public function setId($entityId): static
{
return $this->setData(HappyObjectInterface::ENTITY_ID, $entityId);
}
/**
* @inheritDoc
*/
public function getHappyMessage(): string
{
return (string)$this->getData(HappyObjectInterface::HAPPY_MESSAGE);
}
/**
* @inheritDoc
*/
public function setHappyMessage(string $happyMessage): static
{
return $this->setData(HappyObjectInterface::HAPPY_MESSAGE, $happyMessage);
}
}
Now we create plugin for bind attribute value of extension attribute.
Create di.xml at app/code/MageDad/Module/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\Api\CustomerRepositoryInterface">
<plugin name="magedad_module_happy_customer"
type="MageDad\Module\Plugin\CustomerPlugin"/>
</type>
<preference for="MageDad\Module\Api\Data\HappyObjectInterface" type="MageDad\Module\Model\HappyObject" />
</config>
Create plugin at app/code/MageDad/Module/Plugin/CustomerPlugin.php
<?php
declare(strict_types=1);
namespace MageDad\Module\Plugin;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\CustomerExtensionInterface;
use Magento\Framework\Api\ExtensionAttributesFactory;
use MageDad\Module\Model\HappyObject;
class CustomerPlugin
{
public function __construct(
private ExtensionAttributesFactory $extensionFactory,
private HappyObject $happyObject,
) {
}
public function afterGetById(CustomerRepositoryInterface $subject, CustomerInterface $customer)
{
$extensionAttributes = $customer->getExtensionAttributes();
if ($extensionAttributes === null) {
/** @var CustomerExtensionInterface $extensionAttributes */
$extensionAttributes = $this->extensionFactory->create(CustomerInterface::class);
$customer->setExtensionAttributes($extensionAttributes);
}
if ($extensionAttributes?->getIsHappyCustomer() === null) {
$extensionAttributes->setIsHappyCustomer(true);
}
if ($extensionAttributes?->getHappyMessage() === null) {
$extensionAttributes->setHappyMessage('I am so happy');
}
if ($extensionAttributes?->getHappyNumber() === null) {
$extensionAttributes->setHappyNumber(1);
}
if ($extensionAttributes?->getHappyList() === null) {
$extensionAttributes->setHappyList([
"I",
"Am"
"Happy"
]);
}
if ($extensionAttributes?->getHappyObject() === null) {
$happy = $this->happyObject;
$happy->setId(1);
$happy->setHappyMessage('We are happy');
$extensionAttributes->setHappyObject([$happy]);
}
return $customer;
}
public function afterGetList(CustomerRepositoryInterface $subject, SearchResults $searchResults): SearchResults
{
foreach ($searchResults->getItems() as $customer) {
$extensionAttributes = $customer->getExtensionAttributes();
if ($extensionAttributes === null) {
/** @var CustomerExtensionInterface $extensionAttributes */
$extensionAttributes = $this->extensionFactory->create(CustomerInterface::class);
$customer->setExtensionAttributes($extensionAttributes);
}
$extensionAttributes->setIsHappyCustomer(true);
$extensionAttributes->setHappyMessage('I am so happy');
$extensionAttributes->setHappyNumber(1);
$extensionAttributes->setHappyList([
"I",
"Am"
"Happy"
]);
$happy = $this->happyObject;
$happy->setId(1);
$happy->setHappyMessage('We are happy');
$extensionAttributes->setHappyObject([$happy]);
}
return $searchResults;
}
}
Above code in line $happy = $this->happyObject;this is sample object. Instead of that we need to create repository with getList method which is return $items object of MageDad\Module\Api\Data\HappyObjectInterface[] And set this response to $extensionAttributes->setHappyObject($item); This is standard way of implementation.
We can see extension attribute data like below in repository getById method
We can test using api /rest/V1/customers/:customerId
{
.
.
.
"extension_attributes": {
"is_happy_customer": true,
"happy_message": "I am so happy",
"happy_number": 1,
"happy_list": [
"I",
"Am",
"Happy"
],
"happy_object": [
{
"id": 1,
"happy_message": "We are happy"
}
]
}
.
.
.
}
That’s all 😍
I hope this blog is useful to create customer extension attribute in magento2. I missed anything or need to add some more information, Don’t heisted to leave a comment in this blog, I’ll get back with some positive approach.
Keep loving ❤️ Keep inspiring 🤩 Keep liking 👍 No sharing 😂
33 Comments
**breathe**
breathe is a plant-powered tincture crafted to promote lung performance and enhance your breathing quality.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
https://t.me/s/Top_BestCasino/9
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.info/register?ref=IHJUI7TF
Your article helped me a lot, is there any more related content? Thanks! https://www.binance.info/register?ref=IXBIAFVY
Your article helped me a lot, is there any more related content? Thanks! https://www.binance.info/pt-PT/register?ref=KDN7HDOR
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://accounts.binance.info/si-LK/register-person?ref=LBF8F65G
Không phải ngẫu nhiên mà 888slots game lại chiếm được lòng tin của nhiều người chơi đến vậy, để làm được điều này nhà cái đã không ngừng nỗ lực và cải thiện chất lượng dịch vụ của mình để mang lại những thứ tốt nhất dành cho người chơi. TONY12-12
8CasinoBet, mmmh, you know, not the most beautiful website I’ve see, but hey! Don’t judge the book by it’s cover, some cool bonus over there and smooth experience. 8cassinobet
Trong quá trình sử dụng dịch vụ, sản phẩm giải trí do trang chủ 66b cung cấp, nếu thắc mắc hay gặp phải vấn đề bất cập, bạn hoàn toàn có thể liên hệ ngay với đội ngũ CSKH để được giải đáp và xử lý hiệu quả thông qua: Live Chat, Hotline, Facebook, Telegram và Zalo. TONY12-16
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://www.binance.info/register?ref=IXBIAFVY
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Yo, jililuckapp is worth a shot. The interface is smooth, and I haven’t had any issues with the app. Give it a whirl, guys! More info at: jililuckapp
What’s up, people? Been messing around on 33bet8 lately. The site’s easy to use, and they’ve got some cool promotions running. Definitely a good spot to check out if you’re looking for some action. More info on 33bet8!
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://accounts.binance.info/sl/register?ref=I3OM7SCZ
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Nhờ giao diện hiện đại, bảo mật cao, cùng chiến lược toàn cầu hóa thông minh, 66b club không chỉ nổi bật mà còn trở thành biểu tượng mới của sự đẳng cấp và an toàn trong ngành cá cược trực tuyến. TONY01-29O
**balmorex**
balmorex is an exceptional solution for individuals who suffer from chronic joint pain and muscle aches.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Tính công bằng trong mỗi ván bài tại game 188v được bảo đảm bởi thuật toán ngẫu nhiên hiện đại, loại bỏ hoàn toàn các yếu tố can thiệp từ bên ngoài. TONY02-11O
Mình có lần lướt đọc mấy trao đổi trên mạng thì thấy nhắc tới b52 đăng nhập trong lúc mọi người đang bàn về cách truy cập và tìm hiểu nội dung trên các nền tảng trực tuyến, nên cũng tò mò mở ra xem thử cho biết. Mình không tìm hiểu quá sâu, chỉ xem qua trong thời gian ngắn để quan sát bố cục, cách chia các mục và cách trình bày nội dung tổng thể. Cảm giác là các phần được trình bày khá gọn, thông tin rõ ràng nên đọc lướt cũng không bị rối, với mình như vậy là đủ để nắm được những nội dung cơ bản rồi.
Mình có lần lướt đọc mấy trao đổi trên mạng thì thấy nhắc tới Go88 Live trong lúc mọi người đang bàn về các nền tảng trò chơi trực tuyến, nên cũng tò mò mở ra xem thử cho biết. Mình không tìm hiểu quá sâu, chỉ xem qua trong một thời gian ngắn để quan sát bố cục và cách chia các mục nội dung tổng thể. Cảm giác là các phần được trình bày khá gọn và rõ ràng nên đọc lướt cũng không bị rối, với mình như vậy là đủ để nắm được những nội dung cơ bản rồi.
Mình có lần lướt đọc mấy trao đổi trên mạng thì thấy nhắc tới tai hitclub trong lúc mọi người đang bàn về các ứng dụng và nền tảng trò chơi trực tuyến, nên cũng tò mò mở ra xem thử cho biết. Mình không tìm hiểu quá sâu, chỉ xem qua trong thời gian ngắn để quan sát bố cục và cách chia các mục nội dung tổng thể. Cảm giác là mọi thứ được trình bày khá gọn và rõ ràng nên đọc lướt cũng không bị rối, với mình như vậy là đủ để nắm được những nội dung cơ bản rồi.
Mình có lần lướt đọc mấy trao đổi trên mạng thì thấy nhắc tới tài xỉu sunwin trong lúc mọi người đang bàn về các trò chơi trực tuyến, nên cũng tò mò lướt xem thử cho biết. Mình không tìm hiểu quá sâu, chỉ xem qua trong một thời gian ngắn để quan sát bố cục và cách chia các mục nội dung tổng thể. Cảm giác là các phần được trình bày khá gọn và rõ ràng nên đọc lướt cũng không bị rối, với mình như vậy là đủ để nắm được những nội dung cơ bản rồi.
Mình có lần lướt đọc mấy trao đổi trên mạng thì thấy nhắc tới tải rikvip trong lúc mọi người đang bàn về các ứng dụng và nền tảng trò chơi trực tuyến, nên cũng tò mò mở ra xem thử cho biết. Mình không tìm hiểu quá sâu, chỉ xem qua trong thời gian ngắn để quan sát bố cục và cách chia các mục nội dung tổng thể. Cảm giác là các phần được trình bày khá gọn và rõ ràng nên đọc lướt cũng không bị rối, với mình như vậy là đủ để nắm được những nội dung cơ bản rồi.
Mình có lần lướt đọc mấy trao đổi trên mạng thì thấy nhắc tới b52 đăng nhập trong lúc mọi người đang bàn về cách truy cập và tìm hiểu các nền tảng trò chơi trực tuyến, nên cũng tò mò mở ra xem thử cho biết. Mình không tìm hiểu quá sâu, chỉ xem qua trong thời gian ngắn để quan sát bố cục và cách chia các mục nội dung tổng thể. Cảm giác là bố cục được trình bày khá rõ ràng, các phần dễ theo dõi nên đọc lướt cũng không bị rối, với mình như vậy là đủ để nắm được những thông tin cơ bản rồi.
Mình có lần lướt đọc mấy trao đổi trên mạng thì thấy nhắc tới play iwin trong lúc mọi người đang bàn về các nền tảng trò chơi trực tuyến, nên cũng tò mò mở ra xem thử cho biết. Mình không tìm hiểu quá sâu, chỉ xem qua trong thời gian ngắn để quan sát bố cục và cách chia các mục nội dung tổng thể. Cảm giác là bố cục được trình bày khá rõ ràng, các phần dễ theo dõi nên đọc lướt cũng không bị rối, với mình như vậy là đủ để nắm được những thông tin cơ bản rồi.
Mình có lần lướt đọc mấy trao đổi trên mạng thì thấy nhắc tới tài xỉu sunwin trong lúc mọi người đang bàn về các trò chơi trực tuyến, nên cũng tò mò mở ra xem thử cho biết. Mình không tìm hiểu quá sâu, chỉ xem qua trong một thời gian ngắn để quan sát bố cục và cách chia các mục nội dung tổng thể. Cảm giác là mọi phần được trình bày khá gọn và rõ ràng nên đọc lướt cũng không bị rối, với mình như vậy là đủ để nắm được những nội dung cơ bản rồi.
Mình có lần lướt đọc mấy trao đổi trên mạng thì thấy nhắc tới hitclub com trong lúc mọi người đang bàn về các nền tảng trò chơi trực tuyến, nên cũng tò mò mở ra xem thử cho biết. Mình không tìm hiểu quá sâu, chỉ xem qua trong thời gian ngắn để quan sát bố cục và cách chia các mục nội dung tổng thể. Cảm giác là bố cục được trình bày khá gọn và rõ ràng nên đọc lướt cũng không bị rối, với mình như vậy là đủ để nắm được những nội dung cơ bản rồi.