2

Scenario:
A user placed an order as guest, next time when he placing an order he is registering as customer.

I managed to manually assign his old orders to new registered account based on his email address

Now my question is:
How can I assign the old addresses to his new account?

If any of you have done that previously, please give any pointers to achieve this. Please, feel free to provide other better alternatives, if my thoughts are wrong somewhere. I'll greatly appreciate any help, thanks in advance.

  • Could you please update your question with information on how and when (which events, controllers, etc...) you have added code to assign the old orders to the new account? Maybe someone knows how to add code there for the addresses... – 7ochem May 13 '15 at 8:04
  • Hi , i am using onepagecheckout , when the user clicks on checkout , i am checking for his email in sales_flat_order for orders , assigning them to new customer account and updating guest customer status to 'NO' – Sree May 13 '15 at 8:14
  • So you're doing this manually? – 7ochem May 13 '15 at 8:14
  • yes , i am because magento don't do that automatically – Sree May 13 '15 at 8:22
1

I solved it like this

$orders = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('customer_email', $_orderEmail);

$i = 1;
foreach ($orders as $sorder) {
    $bAddress = $sorder->getBillingAddress();
    $bAddressData = $bAddress->getData();
    $cbAddress = Mage::getModel("customer/address");
    $cbAddress->setData($bAddressData);
    $cbAddress->setCustomerId($custId)
        ->setFirstname($bAddress->getFirstname())
        ->setMiddleName($bAddress->getMiddlename())
        ->setLastname($bAddress->getLastname())
        ->setRegion($bAddress->getRegion())
        ->setRegionId($bAddress->getRegionId())
        ->setPostcode($bAddress->getPostcode())
        ->setPostalcodeId($bAddress->getPostalcodeId())
        ->setStreet($bAddress->getStreet())
        ->setStreetnumber($bAddress->getStreetnumber())
        ->setCity($bAddress->getCity())
        ->setTelephone($bAddress->getTelephone())
        ->setCell($bAddress->getCell())
        ->setCountryId($bAddress->getCountryId())
        ->setAddressType($bAddress->getAddressType())
        ->setPrefix($bAddress->getPrefix())
        ->setSuffix($bAddress->getSuffix())
        ->setCompany($bAddress->getCompany())
        ->setGiftegistryItemId($bAddress->getGiftegistryItemId())
        ->setVatId($bAddress->getVatId())
        ->setVatIsValid($bAddress->getVatIsValid())
        ->setVatRequestId($bAddress->getVatRequestId())
        ->setVatRequestDate($bAddress->getVatRequestDate())
        ->setVatRequestSuccess($bAddress->getVatRequestSuccess())
        ->setIsDefaultBilling('1')
        ->setSaveInAddressBook('1');
    $cbAddress->save();
}
0

Old user is registered user

You should search the table customer_entity by email address. Now write down the old entity_id and the new entity_id (= 'customer ID').

Go to the table customer_address_entity and search for the old 'customer ID' you've written down in the column parent_id. Now edit the records found and change the parent_id from the old 'customer ID' to the new one.

Old user was a guest

If the old account was a guest checkout, then you'll need to get the address data from sales_flat_order_address and put it into the customer_address_entity* tables.

You could do this by running this SQL:

SET @orderID=123; /* The order's `entity_id` */
SET @custID=123; /* Your new `customer_entity`'s `entity_id` */

/* Start with billing address: */
INSERT INTO `customer_address_entity` (`entity_type_id`, `parent_id`) VALUES (2, @custID);
SET @addrID = LAST_INSERT_ID();
INSERT INTO `customer_address_entity_int` (`entity_type_id`, `attribute_id`, `entity_id`, `value`) SELECT 2, 29, @addrID, `region_id` FROM `sales_flat_order_address` WHERE `parent_id` = @orderID AND `address_type` = 'billing';
INSERT INTO `customer_address_entity_text` (`entity_type_id`, `attribute_id`, `entity_id`, `value`) SELECT 2, 25, @addrID, `street` FROM `sales_flat_order_address` WHERE `parent_id` = @orderID AND `address_type` = 'billing';

You can repeat this for shipping address by replacing 'billing' for 'shipping' in the WHERE clause.

The new customer already has a default billing and shipping address set, but you can replace them with the ones from the guest by (for billing):

REPLACE INTO `customer_entity_int` SET `value` = @addrID WHERE `entity_type_id` = 1 AND `attribute_id` = 13 AND `customer_id` = @custID;

Repeat this for shipping address with ```attribute_id` = 14``.

You could also add more attributes to be added (see SELECT * FROM `eav_attribute` WHERE `entity_type_id` = 2, for example company, middlename, fax, suffix, etc...)

  • there is no record in customer_entity if the user placed an order as guest – Sree May 13 '15 at 8:49
  • I have updated my answer – 7ochem May 13 '15 at 9:30
  • @Sree did this work out for you? – 7ochem May 18 '15 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.