Set Default Address for Customers via SQL in Magento

customerMySQL

How can I configure the default address in magento via SQL.

I have over 7000 users, however some of them are not defined.

Each client has ~2 registered address, I would like that the two addresses were marked one as the default billing and another shipping.

Via SQL there is some command?

The default could be the first one found, or the first encountered.

http://i.imgur.com/aV6onTe.jpg

Best Answer

Disclaimer: Please make a backup of your database and/or test on a copy before trying out the query!

To set the first found customer address as default shipping address, use

INSERT INTO customer_entity_int (entity_id, entity_type_id, attribute_id, value)
  SELECT
    e.entity_id,
    1,
    (SELECT
      attribute_id
    FROM eav_attribute
    WHERE attribute_code = 'default_shipping'),
    address.entity_id
  FROM customer_entity e
  LEFT JOIN customer_entity_int v
    ON v.entity_id = e.entity_id
    AND v.attribute_id = (SELECT
      attribute_id
    FROM eav_attribute
    WHERE attribute_code = 'default_shipping')
  INNER JOIN customer_address_entity address
    ON address.parent_id = e.entity_id
  WHERE value_id IS NULL
  GROUP BY e.entity_id

Then repeat with 'default_shipping' replaced by 'default_billing' in both places.

This will probably set the same address as default for both but since it is an arbitrary choice anyway I did not go the extra mile to write a second query that compares the address against the assigned shipping address.

Updated with tested query

Related Topic