Question

My main question is to convert guest to customer

Here is the case: Assumption: a guest placed an order, he successfully paid for that, when he is on the success page then I want to ask him

Do you want to register

If yes please enter your password. Then he clicks create account

Now I want convert this guest to customer and assign the orders to his new customer id

Please let me know if there is an easy way than using plugins

Was it helpful?

Solution

According magento,there are 3 sales table where magento is save customer related

  • sales_flat_order
  • sales_flat_order_address
  • sales_flat_order_grid

sales_flat_order table

customer_id: here customer id save.for guest user it save as NULL.You need to update this column by customer id.

customer_is_guest: it save as 1 when guest is saved an order and it value should be 0 when customer is register.

customer_group_id: Need update customer group by customer group id .

sales_flat_order_grid table

customer_id: here customer id save.for guest user it save as NULL.You need to update this column by customer id.

sales_flat_order_address

customer_address_id: for guest user it value is Null. You need update by customer address id whenever try transfer order

customer_id: here customer id save.for guest user it save as NULL.You need to update this column by customer id.

Code like:

$CustomerId=5;
$customerAddId=20;
$custommergrop=3;
$orderIncrementId=$this->getLastOrderId();
$orderObject=Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$billingId    = $orderObject->getBillingAddress()->getId();
$shipping   = $orderObject->isVirtual() ? null : $orderObject->getShippingAddress();

$billingObjct=Mage::getModel('sales/order_address')->load($billingId);
$billingObjct->setData('customer_id',$CustomerId);
$billingObjct->setData('customer_address_id',$customerAddId);
$billingObjct->save();

if($shipping){  
 $shippingAddressid=$orderObject->getShippingAddress()->getId();
$shippingAddressObject=Mage::getModel('sales/order_address')->load($shippingAddressid);
$shippingAddressObject->setData('customer_id',$CustomerId);
$shippingAddressObject->setData('customer_address_id',$customerAddId);
$shippingAddressObject->save();
}

$orderObject->setData('customer_id',$CustomerId);
$orderObject->setData('customer_is_guest',NULL); /*if null not work then try 0  instead of NULL*/
$orderObject->setData('customer_group_id',$custommergrop);
$orderObject->save();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top