Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vasilii-b/3dcac2fc66c78bd7f805d3c90d9c7962 to your computer and use it in GitHub Desktop.
Save vasilii-b/3dcac2fc66c78bd7f805d3c90d9c7962 to your computer and use it in GitHub Desktop.
Magento 2 (adobe commerce) find customers with wrong region ID in the address
select
cae.entity_id as "Address ID",
cae.parent_id as "Customer ID",
c.firstname,
c.lastname,
c.email,
cae.country_id,
cae.city,
cae.postcode,
cae.region as "Current Address Region",
cae.region_id as "Current Address Region ID",
r.default_name as "Proper Region",
r.region_id as "Proper Region ID"
from customer_address_entity as cae
left join customer_entity as c
on cae.parent_id = c.entity_id
left join directory_country_region as r
on cae.region = r.default_name and cae.country_id = r.country_id
where
(cae.region_id is not null OR cae.region_id != 0 or cae.region is not null) AND # region for country is required
cae.region in (
select name from directory_country_region_name
) AND
cae.region_id != r.region_id
order by cae.country_id ASC
-- limit 100;
@vasilii-b
Copy link
Author

The fix for the region ID mismatch (customer address vs country region table)

update customer_address_entity as cae
left join directory_country_region as r
	on cae.region = r.default_name and cae.country_id = r.country_id
set
	cae.region_id = r.region_id
where
	(cae.region_id is not null OR cae.region_id != 0 or cae.region is not null) AND # region for country is required
	cae.region in (
		select name from directory_country_region_name
	) AND
	cae.region_id != r.region_id
	

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment