4

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();
 $shippingAddress = $order->getShippingAddress(); // shipping address
 $email = $shippingAddress->getEmail();
 $customer = $order->getCustomer();
 $customer_id = $order->getCustomerId();
 $gstno = $customer->getResource()->getAttribute('gst_no')->getFrontend()->getValue($customer);
 $order->setGstNo($customer_id);
 $order->save();

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

2
  • If you are not getting the value then load the customer and then try to get custom values. Commented Aug 3, 2018 at 9:14
  • please check my answer and accept if it's helpful for you :) Commented Aug 3, 2018 at 9:43

6 Answers 6

6

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');
}
7
  • this is not worked for me rohan
    – Devidas
    Commented Aug 3, 2018 at 9:54
  • what report or error generate? can you please tell me? Commented Aug 3, 2018 at 10:03
  • i think after place order not get customer data in observer
    – Devidas
    Commented Aug 3, 2018 at 10:40
  • $this->logger->info("gstno==>".$cattrValue."<"); i have debug data but not getting value in $cattrvalue variable
    – Devidas
    Commented Aug 3, 2018 at 10:42
  • see my updated answer. remove generated folder and cache clean. Commented Aug 3, 2018 at 11:02
5

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.

1
  • Hey it worked for me.
    – Avesh Naik
    Commented Aug 3, 2018 at 9:22
2

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();
1
  • 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. Commented Aug 8, 2018 at 1:37
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.

0

Try below code

    namespace Vendor\Modelename\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;
    use Magento\Catalog\Model\ProductFactory;

 
class Dropshipprice implements ObserverInterface
{
    protected $customerSession;
    protected $customerRepositoryInterface;
    protected $productFactory;

    public function __construct(\Magento\Customer\Model\SessionFactory $customerSession, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface, ProductFactory $productFactory)
    {
        $this->customerSession = $customerSession;
        $this->customerRepositoryInterface = $customerRepositoryInterface;
        $this->productFactory = $productFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $this->customerSession->create();
        if ($customer->isLoggedIn() && $customer->getCustomerGroupId() == 4) {
            $id = $customer->getCustomerId();
            $customerData = $this->customerRepositoryInterface->getById($id);
            $dropshitDiscount = $customerData->getCustomAttribute('dropshit_discount')->getValue();

            $item = $observer->getEvent()->getData('quote_item');
            $productId = $item->getProduct()->getId();
            $_product = $this->productFactory->create()->load($productId);
            $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            $finalprice = $_product->getFinalPrice();

            if (!empty($_product->getPriceDropship()) && $_product->getPriceDropship() != '0') {
                $dropshipfinalprice = $_product->getPriceDropship();
            } else {
                $dropshitDiscount = $dropshitDiscount;
                $dropshipfinalprice = $finalprice * (1 - ($dropshitDiscount / 100));
            }

            $price = $dropshipfinalprice;
            $item->setCustomPrice($price);
            $item->setOriginalCustomPrice($price);
            $item->getProduct()->setIsSuperMode(true);
        }
    }
}

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.