Magento 2.X – “Invalid Customer Address Id” comes randomly for the customers on checkout

By | March 26, 2024
Spread the love

We’ve faced an issue in Magento 2.2.9 says "Invalid Customer address id XXXX" randomly for the registered customers on the website. I have checked this further and found that this issue is because of the customer quote which is active and does not have an entry in the quote_address table.

I’ve checked it further and fixed this issue by overriding Magento’s validateForCart function. The issue is with the customer Id passing in the function is null for the customers facing this issue.

Create sample module and add below line in di.xml

<?xml version="1.0"?>
<config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\QuoteAddressValidator">
        <plugin name="disableguestcheck" type="Techgroup\Addon\Plugin\QuoteAddressValidatorPlugin"/>
    </type>
</config>

Create Plugin class Techgroup\Addon\Plugin\QuoteAddressValidatorPlugin.php

<?php
namespace Techgroup\Addon\Plugin;

use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\QuoteAddressValidator;

class QuoteAddressValidatorPlugin
{
    /**
     * @param QuoteAddressValidator $subject
     * @param CartInterface $cart
     * @param AddressInterface $address
     */
    public function beforeValidateForCart(
        QuoteAddressValidator $subject,
        CartInterface $cart,
        AddressInterface $address
    ): void {
        if ($cart->getCustomer()->getId()) {
            $cart->setCustomerIsGuest(0);
        }
    }
}

 

Cleanup your database by following queries:

$connection->query(“update customer_entity ce set default_billing = null where default_billing is not null and default_billing != 0 and default_billing not in (select entity_id from customer_address_entity where parent_id = ce.entity_id);”);

$connection->query(“update customer_entity ce set default_shipping = null where default_shipping is not null and default_shipping != 0 and default_shipping not in (select entity_id from customer_address_entity where parent_id = ce.entity_id);”);