1

I'm having trouble with getting customer address Id as it returns a null value.

This is what I have tried:

$checkout = $this->_sessionCheckout->getQuote();
if ($checkout) {
   $shippingAddress = $checkout->getShippingAddress();
   if ($shippingAddress) {
       $addressId = $shippingAddress->getCustomerAddressId();
       $this->_logger->log(100, print_r('address Id: ' . $addressId , true)); //Returns null 
       /** @var \Magento\Customer\Api\Data\AddressInterface $address */
       $address = $this->_addressRepository->getById($addressId);
       $address->setCity($city_name);
       $this->_addressRepository->save($address);
}

I just need to get customer address Id in order to update the city. I don't know why it returns a null value.


Edited Details:

The below image shows the saved shipping addresses:

shipping-addresses

What I want to know is How to know the customer address id of each of those shipping addresses. So I can modify any details I want.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63

2 Answers2

1

When you place an order as a guest user, you have to add shipping address on checkout page at that time there will be not a customer reference object, so you get value of customer address Id to null.

When you place an order as a registered customer, you should have default shipping address, then only you can get the value of customer address Id.

This happen because customer_address_id is a reference to customer_address table and customer_address table reference to customer_entity table.

Dhara Bhatti
  • 239
  • 2
  • 8
  • Hi thanks for the information. But my problem is that for a registered customer, I'm still getting customer address Id as null. – Bernedette Perera Feb 17 '19 at 10:22
  • Do you know any other way to get customer_address_id of a registered customer? – Bernedette Perera Feb 17 '19 at 14:36
  • You can get registered customer_address_id on checkout page, not on cart page. Have you checked that? – Dhara Bhatti Feb 18 '19 at 06:01
  • Yes. So When I do the following way: $addressId = $this->_sessionCheckout->getQuote()->getShippingAddress()->getCustomerAddressId(); The $addressId returns null value – Bernedette Perera Feb 18 '19 at 06:02
  • Have you added default shipping address from My account section? – Dhara Bhatti Feb 18 '19 at 08:38
  • hi, I have edited my question above, please look at it. I'm showing you an image of the saved shipping addresses and the first address is the default shipping address – Bernedette Perera Feb 18 '19 at 08:56
  • Ok. You can get mostly customer_address_id using same above code in checkout page if you have default shipping address for registered customer. Can you please check in quote_address table for customer_address_id column? Can you check any Id placed or not? – Dhara Bhatti Feb 19 '19 at 06:06
1

You can get the order as follows Wherever your want to invoke this information add following in the Constructor if not already being used.

protected $checkoutSession;
public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        \Psr\Log\LoggerInterface $logger
    )
    {
        
        $this->checkoutSession = $checkoutSession;
        $this->logger = $logger;
    }


    $order = $session->getLastRealOrder();
    $orderdata = $order->getData();
    $shipping_address_id = $orderdata['shipping_address_ID'];
   

This address is the final shipping address selected during payment stage of the checkout process.

Mukesh
  • 7,630
  • 21
  • 105
  • 159
PhantomS
  • 420
  • 4
  • 14