Skip to content

Instantly share code, notes, and snippets.

@eduan
Created April 18, 2019 21:52
Show Gist options
  • Save eduan/cbb9fb8e7605b28fb0a8a23e69997dab to your computer and use it in GitHub Desktop.
Save eduan/cbb9fb8e7605b28fb0a8a23e69997dab to your computer and use it in GitHub Desktop.
Duplicate Magento Customer (Basic Info / Address)
DROP PROCEDURE duplicate_customer;
DELIMITER $$
CREATE PROCEDURE duplicate_customer(IN old_entity_id INT, IN new_email VARCHAR(250))
BEGIN
DECLARE cursorlist_done BOOLEAN DEFAULT FALSE;
DECLARE old_address_entity_id INT;
DECLARE old_entity_type_id smallint(5);
DECLARE old_attribute_set_id smallint(5);
DECLARE old_increment_id varchar(50);
DECLARE old_created_at timestamp;
DECLARE old_updated_at timestamp;
DECLARE old_is_active smallint(5);
DECLARE cursorlist CURSOR FOR select entity_id, entity_type_id, attribute_set_id, increment_id, created_at, updated_at, is_active
from customer_address_entity
where parent_id = old_entity_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET cursorlist_done = TRUE;
insert into customer_entity (select null, entity_type_id, attribute_set_id, website_id, new_email, group_id, increment_id, store_id, created_at, updated_at, is_active, gp_entry, net30, gp_cust_id, origin, gp_salesperson_id, gp_pricelevel, company_name, disable_auto_group_change from customer_entity where entity_id = old_entity_id);
set @entity_id := (select max(entity_id) from customer_entity);
SELECT 'New customer ID: ', @entity_id;
insert into customer_entity_datetime (select null, entity_type_id, attribute_id, @entity_id, value from customer_entity_datetime where entity_id = old_entity_id);
insert into customer_entity_decimal (select null, entity_type_id, attribute_id, @entity_id, value from customer_entity_decimal where entity_id = old_entity_id);
insert into customer_entity_int (select null, entity_type_id, attribute_id, @entity_id, value from customer_entity_int where entity_id = old_entity_id);
insert into customer_entity_text (select null, entity_type_id, attribute_id, @entity_id, value from customer_entity_text where entity_id = old_entity_id);
insert into customer_entity_varchar (select null, entity_type_id, attribute_id, @entity_id, value from customer_entity_varchar where entity_id = old_entity_id);
OPEN cursorlist;
loop_List: LOOP
FETCH cursorlist INTO old_address_entity_id, old_entity_type_id, old_attribute_set_id, old_increment_id, old_created_at, old_updated_at, old_is_active;
IF cursorlist_done THEN
LEAVE loop_List;
END IF;
insert into customer_address_entity values (null, old_entity_type_id, old_attribute_set_id, old_increment_id, @entity_id, old_created_at, old_updated_at, old_is_active);
set @new_address_entity_id := (select max(entity_id) from customer_address_entity);
SELECT 'New address entity: ', @new_address_entity_id;
insert into customer_address_entity_datetime (select null, entity_type_id, attribute_id, @new_address_entity_id, value from customer_address_entity_datetime where entity_id = old_address_entity_id);
insert into customer_address_entity_decimal (select null, entity_type_id, attribute_id, @new_address_entity_id, value from customer_address_entity_decimal where entity_id = old_address_entity_id);
insert into customer_address_entity_int (select null, entity_type_id, attribute_id, @new_address_entity_id, value from customer_address_entity_int where entity_id = old_address_entity_id);
insert into customer_address_entity_text (select null, entity_type_id, attribute_id, @new_address_entity_id, value from customer_address_entity_text where entity_id = old_address_entity_id);
insert into customer_address_entity_varchar (select null, entity_type_id, attribute_id, @new_address_entity_id, value from customer_address_entity_varchar where entity_id = old_address_entity_id);
END LOOP loop_List;
CLOSE cursorlist;
END$$
DELIMITER ;
call duplicate_customer(138890, 'anotheremail@gmail.com');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment