1

My requirement

I Have created custom shipping method for allowing customers to choose Stores to pickup the orders. When the customers choose the stores pickup option, I need to update the selected store address as shipping address for the relevant order.

Checkout section and save the store id part is successfully implemented.

I tried to update the shipping address using observer checkout_onepage_controller_success_action like below.

public function execute(\Magento\Framework\Event\Observer $observer)
{   
    $order = $observer->getEvent()->getOrder();
    try{ 
        $quote = $this->quoteRepository->get($order->getQuoteId());
        
        $order->setPickupStoreName($quote->getPickupStoreName());

        //set shipping address here
        $shippingAddress = array(
            'customer_address_id' => '',
            'prefix' => '',
            'firstname' => "Mujahidh",
            'middlename' => '',
            'lastname' => "Haseem",
            'suffix' => '',
            'company' => '',
            'street' => array(
                '0' => 'Address 1',
                '1' => 'Address 2'
            ),
            'city' => 'Culver City',
            'country_id' => 'US',
            'region' => 'California',
            'region_id' => '12',
            'postcode' => '90232',
            'telephone' => '888-888-8888',
            'fax' => ''
        );
        //-------
        $quote->getShippingAddress()->addData($shippingAddress);
        $quote->save();
        $order->save();
        return $this;
    } catch(\Exception $e){
        $this->logger->info('Error in execute method in sales order place observer :'.$e->getMessage());
    }

}

Using this way i am unable to save it. How can i update the shipping address in this scenario?

1 Answer 1

0

Would it not make sense to persist the shipping address to the quote using the rest API endpoint during checkout? Then the address will automatically be persisted from the quote to the order on checkout success? This is how core Magento persists addresses to quotes/orders.

1
  • I couldn't understand what you are trying to explain me. Can you please explain little more?
    – Mujahidh
    Commented Nov 2, 2022 at 4:17

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.