2

I have created a customer custom attribute like(gst_no). In my Observer where I get my customer data I want to get the value of this attribute too.

 $order = $observer->getEvent()->getOrder();
 $order_id = $order->getIncrementId();
 $this->logger->info("order_id=========".$order_id."===========");
 $shippingAddress = $order->getShippingAddress(); // shipping address
 $email = $shippingAddress->getEmail();
 $this->logger->info($email);
 $customer = $order->getCustomer();
 //$this->logger->info("customerId==>".$customer_no."<");
 $customer_id = $order->getCustomerId();
 $this->logger->info("customerId==>".$customer_id."<");
 $gstno = $customer->getResource()->getAttribute('gst_no')->getFrontend()->getValue($customer);
 //$this->logger->info("customerId==>".$customer_no."<");
 $order->setGstNo($customer_id);
 $order->save();

But I am not getting attribute value.,can anyone help?

  • If you are not getting the value then load the customer and then try to get custom values. – Sukumar Gorai Aug 3 '18 at 9:14
  • please check my answer and accept if it's helpful for you :) – Rohan Hapani Aug 3 '18 at 9:43
4

Try to use this below code :

/**
 *  @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
 *  @var \Magento\Sales\Model\Order $order
*/
protected $_customerRepositoryInterface;
protected $_order;

public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
    \Magento\Sales\Model\Order $order,
)
{
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
    $this->_order = $order;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{ 
    $order = $observer->getEvent()->getOrder();
    $orderdetails = $this->_order->load( $order->getId());
    $customerId = $orderdetails ->getCustomerId();
    $customeratt = $this->_customerRepositoryInterface->getById($customerId);
    $cattrValue = $customeratt->getCustomAttribute('gst_no');
}
  • this is not worked for me rohan – Devidas Aug 3 '18 at 9:54
  • what report or error generate? can you please tell me? – Rohan Hapani Aug 3 '18 at 10:03
  • i think after place order not get customer data in observer – Devidas Aug 3 '18 at 10:40
  • $this->logger->info("gstno==>".$cattrValue."<"); i have debug data but not getting value in $cattrvalue variable – Devidas Aug 3 '18 at 10:42
  • see my updated answer. remove generated folder and cache clean. – Rohan Hapani Aug 3 '18 at 11:02
3

You can use the below code -

$gstNoAttribute = $customer->getCustomAttribute('gst_no');
if(isset($gstNoAttribute)) {
    $gstno = $gstNoAttribute->getValue();
}
2

Use customer repository.

The repo injection will give you the customer custom attributes.

  • Hey it worked for me. – Avesh Naik Aug 3 '18 at 9:22
1

Custom attributes does not added into the select list with the customer data retrieved from Order object. You can only use this $customer->getResource()->getAttribute('gst_no')->getFrontend()->getValue($customer); when you load the customer object through repository.

For your situation, from the order object, get the customer data and from that, get customer Id and then use the customerrepostory to get full information (including the custom attributes) and then use $customer->getData('gst_no') to get the details of custom attribute.

1

This is correct answer which worked for me.

$order = $observer->getEvent()->getOrder();
//$customer = $this->_customerRepositoryInterface->getById($order->getCustomerId());
$customer = $this->_customerRepositoryInterface->get($order->getCustomerEmail());
$gstno = $customer->getCustomAttribute('gst_no')->getValue();
$this->logger->info("gstno==>".$gstno."<");
$order->setGstNo($gstno);
$order->save();
  • this only works when you have the flat tables created for your customer entity and your custom attribute is already been added into the columns of those flat tables data. normally flat tables for eav attributes are created through indexer. – Zeeshan Khuwaja Aug 8 '18 at 1:37

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.