Db set default billing and shipping address

-- start default_billing --
 
/** CREATE DEFAULT BILLING INSERTS CHUNK */
 
CREATE TEMPORARY TABLE my_default_billing_customer_entity_int
SELECT 1 AS entity_type_id,
(SELECT attribute_id FROM eav_attribute WHERE attribute_code='default_billing' AND entity_type_id=1) AS attribute_id,
my.magento_id AS entity_id, 
a1.entity_id AS VALUE
FROM mytable my 
INNER JOIN customer_entity c1 ON my.magento_id = c1.entity_id 
INNER JOIN customer_address_entity a1 ON c1.entity_id = a1.parent_id
LEFT JOIN customer_entity_int c2 ON c2.entity_id = c1.entity_id AND c2.entity_type_id = 1 AND c2.attribute_id = 
(SELECT attribute_id FROM eav_attribute WHERE attribute_code='default_billing' AND entity_type_id=1)
WHERE c2.VALUE IS NULL 
GROUP BY entity_id HAVING COUNT(a1.entity_id)=1;
 
 
/** CHECK **/
SELECT * FROM customer_entity_int a
INNER JOIN my_default_billing_customer_entity_int b ON a.entity_id = b.entity_id AND a.attribute_id= 13 AND a.entity_type_id = 1;
 
/** INSERT **/
 
INSERT INTO customer_entity_int (entity_type_id, attribute_id, entity_id, VALUE) 
SELECT * FROM my_default_billing_customer_entity_int;
 
-- end default_billing --
 
 
-- start default_shipping --
 
 
/** CREATE DEFAULT SHIPPING INSERTS CHUNK */
 
CREATE TEMPORARY TABLE my_default_shipping_customer_entity_int
SELECT 1 AS entity_type_id,
(SELECT attribute_id FROM eav_attribute WHERE attribute_code='default_shipping' AND entity_type_id=1) AS attribute_id,
my.magento_id AS entity_id, 
a1.entity_id AS VALUE
FROM mytable my 
INNER JOIN customer_entity c1 ON my.magento_id = c1.entity_id 
INNER JOIN customer_address_entity a1 ON c1.entity_id = a1.parent_id
LEFT JOIN customer_entity_int c2 ON c2.entity_id = c1.entity_id AND c2.entity_type_id = 1 AND c2.attribute_id = 
(SELECT attribute_id FROM eav_attribute WHERE attribute_code='default_shipping' AND entity_type_id=1)
WHERE c2.VALUE IS NULL 
GROUP BY entity_id HAVING COUNT(a1.entity_id)=1;
 
 
/** CHECK **/
SELECT * FROM customer_entity_int a
INNER JOIN my_default_shipping_customer_entity_int b ON a.entity_id = b.entity_id 
AND a.attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code='default_shipping' AND entity_type_id=1) 
AND a.entity_type_id = 1;
 
/** INSERT **/
INSERT INTO customer_entity_int (entity_type_id, attribute_id, entity_id, VALUE) 
SELECT * FROM my_default_shipping_customer_entity_int;
 
-- end default_shipping --

Leave a Reply