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="http://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);
        }
    }
}