0

I am using Magento 2.4.1, made observer. Event name is checkout_submit_all_after. I want to update order data, customer data, customer address data. Updating Order data is succeeded, but Other data failed. I use "$customer = $this->customerRepository->getById($customerId);" to get customer data, but returned nodata. Please teach me I can't get customer data.

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

class OrderCreateObserver implements ObserverInterface
{
    protected $request;
    protected $customerRepository;

    public function __construct(
        RequestInterface $request,
        CustomerRepositoryInterface $customerRepository,

    ){
        $this->request = $request;
        $this->customerRepository = $customerRepository;
    }

    public function execute(Observer $observer)
    {
        $params = $this->request->getParams();

        $customerId = $order->getCustomerId();
        
        $customer = $this->customerRepository->getById($customerId);
        $customer->setCustomAttribute('updatecolumn', $params['updatecolumn']);

        $this->customerRepository->save($customer);

    }
}

updated

$customer->getCustomAttribute('updatecolumn')->getValue()

was worked, after

$customer->setCustomAttribute('updatecolumn', $params['updatecolumn']);

but data was not saved

1 Answer 1

0

if you want to made these changes after order submit better you use this event

<event name="sales_order_place_after">
    <observer name="order_create_observer" instance="Example\Details\Observer\OrderCreateObserver" / >
</event>

instead of

<event name="checkout_submit_all_after">
    <observer name="order_create_observer" instance="Example\Details\Observer\OrderCreateObserver" / >
</event>
1
  • thank you. observer worked, but customer data did not update(customer_entity table did not update.)
    – user94472
    Feb 15, 2021 at 8:03

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.