3

I have created an extension attribute called "sms_subscribed" using data patch.

I am trying to get it in observer. is their any way to get it ?

OrderPlaceAfter.php

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class OrderPlaceAfter implements ObserverInterface

public function execute(\Magento\Framework\Event\Observer $observer)
    {
      $observer->getEvent()->getOrder();
    }
1
  • any thoughts on this ?
    – devhs
    Sep 2, 2021 at 6:20

3 Answers 3

1

try below code

 protected $customerFactory;
 protected $customerRepository;

  public function __construct(
            \Magento\Customer\Model\CustomerFactory $customerFactory,
            \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
        ) {
            $this->customerFactory = $customerFactory;
            $this->customerRepository = $customerRepository;
        }
    
        public function execute(Observer $observer)
        {
            $order = $observer->getEvent()->getOrder();
            $customerId = $order->getCustomerId();
            $customer = $this->customerFactory->create()->load($customerId);
            $customerAttribute = $customer->getSmsSubscribed();

            //or

            $customer = $this->customerRepository->getById($customerId);
            $customerAttribute = $customer->getCustomAttribute('sms_subscribed');          
        }
0

How did You created extension Attribute exactly?
Are You sure You created Extension Attributes? For Extension Attributes You need to create extension_attributes.xml file in etc folder (and define there Extension attributes).

If You created DataPatch, I think You created just normal order attribute. In that case You should be able to get it with

$order->getSmsSubscriber()

or

$order->getData('sms_subscribed')

But obviously this attribute can not be empty.

Btw for extension attribute, You can use

$extensionAttribute = $order->getExtensionAttributes()
$extensionAttribute->getSmsSubscriber()

But obviously this extension attribute have to be set.

0

Try below code.

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;

class OrderPlaceAfter implements ObserverInterface
{
    protected $customerRepository;

    public function __construct(
        CustomerRepositoryInterface $customerRepository
    ) {
        $this->customerRepository = $customerRepository;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $observer->getEvent()->getOrder();
        $customer = $this->customerRepository->getById($order->getCustomerId());
        $customerAttribute = $customer->getCustomAttribute('sms_subscribed');
    }
}

Hope this will help you!

Your Answer

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

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