0

I have to save the account edit with customer default shipping address details. After the successful order placement.

I have created the observer but not sure how to call editPost of the customer from that execute method.

<?php
 namespace YX\Customer\Observer;
 use Magento\Framework\Event\Observer;
 use Magento\Framework\Event\ObserverInterface;


class OrderSuccess implements ObserverInterface
{
  protected $customerSession;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->customerSession = $customerSession;
    }

/**
 * @param Observer $observer
 * @return void
 */
public function execute(Observer $observer)
{
    // TODO: Implement execute() method.
    $customer = $this->customerSession->getCustomer();
    $shippingAddress = $customer->getDefaultShippingAddress();
    if ($shippingAddress) {
        $firstname =  $shippingAddress->getFirstname();
        $secondname =  $shippingAddress->getLastname();
        $telephone =  $shippingAddress->getTelephone();
    }

 }
}

Now I need to send these 3 parameter $firstname, $secondname, $telephone to editPost controller.

How can to achieve it.

My code looks something like that

public function execute(Observer $observer)
{
    // TODO: Implement execute() method.
    $customer = $this->customerSession->getCustomer();
    $shippingAddress = $customer->getDefaultShippingAddress();

    if ($shippingAddress) {
        $firstname =  $shippingAddress->getFirstname();
        $secondname =  $shippingAddress->getLastname();
        $telephone =  $shippingAddress->getTelephone();
        $customer->setData('firstname', $firstname);
        $customer->setData('lastname', $secondname);
        $customer->setData('mobile', $telephone);
        $customer->save();
    }
  • from an event call, account edit action is not a good idea. Why you need this editpost – Amit Bera Aug 7 at 13:24
  • I need to update the customer edit form by default shipping address data first , last name and phone , everytime Shipping address changes after order place I need to change the data customer form edit – summu Aug 7 at 13:41
  • Means you want to change the customer default Shipping address( first , last name and phone) by Order Shipping address (first , last name and phone)? – Amit Bera Aug 7 at 13:57
  • No, I want to take the default shipping address first last name and save it to customer edit form which is editPost controller , default shipping address will remain as it is , default shipping address is address book and I want to reflect the changes in customer edit form – summu Aug 7 at 13:59
  • Your comment seems confusion.I want to take the default shipping address first last name and save it to customer edit form which is formEdit , default shipping address will remain as it is.Clarify it – Amit Bera Aug 7 at 14:01
1

In your

Vendor\ModuleName\etc\frontend\events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_onepage_controller_success_action">
        <observer name="custom_frontend_observer" instance="Vendor\ModuleName\Observer\AfterCheckout" />
    </event>
</config>

In your

Vendor\ModuleName\Observer\AfterCheckout.php

 use Magento\Framework\Event\Observer;
 use Magento\Framework\Event\ObserverInterface;


class AfterCheckout implements ObserverInterface
{
  protected $customerSession;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\CustomerFactory $customerSession
    )
    {
        $this->customerSession = $customerSession;
        $this->customer        = $customer;
    }

/**
 * @param Observer $observer
 * @return void
 */
public function execute(Observer $observer)
{
    // TODO: Implement execute() method.
    $customerId = $this->customerSession->getCustomer()->getId();
    $order = $observer->getOrder();
    $customer   = $this->customer->create()->load($customerId);
    $customer->setData('firstname',$order->getFirstName );
    $customer->setData('lastname', $order->getLastName);
    $customer->setData('mobile',$order->getMobile );
    $customer->save();

}
  • But I only need to save the first name and last name and phone number not whole address as customer edit form doesn't, have that many fields as the address book and I am not sure if I get default shipping address from that $order->getData('address') – summu Aug 8 at 3:25
  • check my update answer – Waqar Ali Aug 8 at 3:39
  • how do you find the setData keys, like when i wrote the code before your answer , i wrote customer->setData('telephone',$order->getMobile ); – summu Aug 8 at 3:53
  • I have updated my code, see my updated question – summu Aug 8 at 3:55
  • if you have access to your Database find table customer_entity you will get All keys there – Waqar Ali Aug 8 at 4:45

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.