loadLayout() ->_setActiveMenu('customer/guesttoreg_adminform') ->_addBreadcrumb( Mage::helper('adminhtml')->__('Guests To Registered Customers'), Mage::helper('adminhtml')->__('Guests To Registered Customers') ); return $this; } /** * Index Action */ public function indexAction() { $this->_title($this->__('Customers'))->_title($this->__('Guests To Registered Customers')); $this->_initLayout() ->_addContent($this->getLayout()->createBlock('GuestToReg/adminhtml_customers')) ->renderLayout(); } public function massConvertAction() { $orderIds = $this->getRequest()->getPost('order_ids'); $groupId = $this->getRequest()->getPost('group_id'); if (! $orderIds) { Mage::getSingleton('adminhtml/session')->addError($this->__('No Order ID found to convert')); $this->_redirect('*/*/index'); return; } foreach ($orderIds as $orderId) { $this->convertAction($orderId, $groupId, true); } $this->_redirect('*/*/index'); } public function convertAction($orderId = NULL, $groupId = NULL, $isMass = false) { if($orderId == "") { $orderId = $this->getRequest()->getParam('order_id'); } if($groupId == "") { $groupId = $this->getRequest()->getParam('group_id'); } /** @var $order Mage_Sales_Model_Order */ $order = Mage::getModel('sales/order')->load($orderId); //UPDATE FOR NEWSLETTER START $resource = Mage::getSingleton('core/resource'); $prefix = Mage::getConfig()->getNode('global/resources/db/table_prefix'); $write = $resource->getConnection('core_write'); $read = $resource->getConnection('core_read'); $select_qry5 = $read->query("SELECT subscriber_status FROM `".$prefix."newsletter_subscriber` WHERE subscriber_email = '". $order->getCustomerEmail() ."'"); $newsletter_subscriber_status = $select_qry5->fetch(); //UPDATE FOR NEWSLETTER END if (! $order->getId()) { Mage::getSingleton('adminhtml/session')->addError($this->__('No Order ID found to convert')); $this->_redirect('*/*/index'); return $this; } /** @var $customer Mage_Customer_Model_Customer */ $customer = Mage::getModel('customer/customer')->setWebsiteId($order->getStore()->getWebsiteId())->loadByEmail($order->getCustomerEmail()); if ($customer->getId()) { //UPDATE FOR DOWNLOADABLE PRODUCTS $write_qry = $write->query("UPDATE `".$prefix."downloadable_link_purchased` SET customer_id = '". $customer->getId() ."' WHERE order_id = '". $order->getId() ."'"); //UPDATE FOR NEWSLETTER START if($newsletter_subscriber_status['subscriber_status'] !="" && $newsletter_subscriber_status['subscriber_status'] > 0) { $write_qry = $write->query("UPDATE `".$prefix."newsletter_subscriber` SET subscriber_status = '". $newsletter_subscriber_status['subscriber_status'] ."' WHERE subscriber_email = '". $order->getCustomerEmail() ."'"); } //UPDATE FOR NEWSLETTER END Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The customer (%s) already exists. So the customer has been merged', $order->getCustomerEmail())); } else { //create a new customer based on the order $customer->addData(array( "prefix" => $order->getCustomerPrefix(), "firstname" => $order->getCustomerFirstname(), "middlename" => $order->getCustomerMiddlename(), "lastname" => $order->getCustomerLastname(), "suffix" => $order->getCustomerSuffix(), "email" => $order->getCustomerEmail(), "group_id" => $groupId, "taxvat" => $order->getCustomerTaxvat(), "website_id" => $order->getStore()->getWebsiteId(), 'default_billing'=> '_item1', 'default_shipping'=> '_item2', )); //Billing Address /** @var $billingAddress Mage_Sales_Model_Order_Address */ $billingAddress = $order->getBillingAddress(); /** @var $customerBillingAddress Mage_Customer_Model_Address */ $customerBillingAddress = Mage::getModel('customer/address'); $billingAddressArray = $billingAddress->toArray(); unset($billingAddressArray['entity_id']); unset($billingAddressArray['entity_type_id']); unset($billingAddressArray['parent_id']); unset($billingAddressArray['customer_id']); unset($billingAddressArray['customer_address_id']); unset($billingAddressArray['quote_address_id']); #print_r($billingAddressArray); $customerBillingAddress->addData($billingAddressArray); $customerBillingAddress->setPostIndex('_item1'); $customer->addAddress($customerBillingAddress); //Shipping Address /** @var $shippingAddress Mage_Sales_Model_Order_Address */ $shippingAddress = $order->getShippingAddress(); /** @var $customerShippingAddress Mage_Customer_Model_Address */ $customerShippingAddress = Mage::getModel('customer/address'); if(!empty($shippingAddress)) { $shippingAddressArray = $shippingAddress->toArray(); unset($shippingAddressArray['entity_id']); unset($shippingAddressArray['entity_type_id']); unset($shippingAddressArray['parent_id']); unset($shippingAddressArray['customer_id']); unset($shippingAddressArray['customer_address_id']); unset($shippingAddressArray['quote_address_id']); $customerShippingAddress->addData($shippingAddressArray); $customerShippingAddress->setPostIndex('_item2'); $customer->addAddress($customerShippingAddress); } #exit; //Save the customer $customer->setIsSubscribed(false); $customer->setPassword($customer->generatePassword()); $customer->save(); $disable_new_customer_email = (bool)Mage::getStoreConfig('guesttoreg/guesttoreg/disable_new_customer_email'); if ($disable_new_customer_email != true) { $customer->sendNewAccountEmail(); } #$billingAddress->setCustomerAddressId($customer->getDefaultBillingAddress()->getId())->save(); #$shippingAddress->setCustomerAddressId($customer->getDefaultShippingAddress()->getId())->save(); //UPDATE FOR DOWNLOADABLE PRODUCTS $write_qry = $write->query("UPDATE `".$prefix."downloadable_link_purchased` SET customer_id = '". $customer->getId() ."' WHERE order_id = '". $order->getId() ."'"); //UPDATE FOR NEWSLETTER START if($newsletter_subscriber_status['subscriber_status'] !="" && $newsletter_subscriber_status['subscriber_status'] > 0) { $write_qry = $write->query("UPDATE `".$prefix."newsletter_subscriber` SET subscriber_status = '". $newsletter_subscriber_status['subscriber_status'] ."' WHERE subscriber_email = '". $order->getCustomerEmail() ."'"); } //UPDATE FOR NEWSLETTER END Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The guest (%s) is converted to customer', $order->getCustomerEmail())); } $order->setCustomerId($customer->getId()); $order->setCustomerIsGuest('0'); $order->setCustomerGroupId($groupId); $order->save(); Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The order (%s) has been been assigned to the customer (%s)', $order->getIncrementId(), $order->getCustomerEmail())); if (! $isMass) $this->_redirect('*/*/index'); return $this; } }