Magento event observer for customer registration success
If you are looking for how to execute some code when customer successfully sign up in your website, you can use below code to check if the registration was successful. Note, this will NOT check if customer was registered from checkout page, if you are looking for that please go to this post.
In your xml file:
<events> | |
<customer_register_success> | |
<observers> | |
<namespace_module_customer_register_success> | |
<type>singleton</type> | |
<class>Namespace_Module_Model_Observer</class> | |
<method>customerRegisterSuccess</method> | |
</namespace_module_customer_register_success> | |
</observers> | |
</customer_register_success> | |
</events> |
And in your Observer.php file:
class Namespace_Module_Model_Observer { | |
public function customerRegisterSuccess(Varien_Event_Observer $observer) { | |
$event = $observer->getEvent(); | |
$customer = $event->getCustomer(); | |
$email = $customer->getEmail(); | |
if($email) { | |
//code to handle if customer is successfully registered | |
} | |
} | |
} |
Magento check if customer registered in checkout page
If you want to check if the customer is guest, registered or just register to the site when they place the order, below script will help you identify that in success.phtml file.
You can find success / order confirmation phtml file at:
app/design/frontend/[package]/[theme]/template/checkout/success.phtml
Just at the end of this file put below lines of code:
<?php $order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId()); | |
$quoteId = $order->getQuoteId(); | |
$quote = Mage::getModel('sales/quote')->load($quoteId); | |
$method = $quote->getCheckoutMethod(true); | |
$customer_email = $order->getCustomerEmail(); | |
if ($method == 'register'){ ?> | |
//code to handle if customer just registered to your site | |
<?php } elseif($method == 'guest') {?> | |
//code to handle if customer is guest | |
<?php } else { ?> | |
//code to handle for logged in customer | |
<?php } ?> |
In the same file, success.phtml, you can request for order number, customer email, customer id, subtotal, grandtotal, order ID just created etc. like this:
$_customerId = Mage::getSingleton('customer/session')->getCustomerId(); | |
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId(); | |
$order = Mage::getSingleton('sales/order'); | |
$order->load($lastOrderId); | |
$_totalData =$order->getData(); | |
$_sub = $_totalData['subtotal']; | |
$_orderEmail = $_totalData['customer_email']; | |
$_orderNumber = $_totalData['increment_id']; |
Magento: Register guest user to website if email provided
If your Magento website have feature where guest user can place an order without registering themself in your site, you can add them as a customer in your database. It helps in grouping the orders w.r.t email address in backend sales order. The only condition is that he should provide his email address while placing an order, which is quite obvious.
So here is a snippet of code you can insert where you are asking user to enter his email address. Just check if the user is already not registered in your site.
$e = $email; //provided by guest user | |
$store = Mage::app()->getStore(); | |
$websiteId = Mage::app()->getWebsite()->getId(); | |
$customerObj = Mage::getModel('customer/customer'); | |
$customerObj->website_id = $websiteId; | |
$customerObj->setStore($store); | |
$prefix = "mag"; | |
$pwd = uniqid($prefix); | |
$session = Mage::getSingleton('checkout/session'); | |
$fname = $session->getFirstname(); | |
$lname = $session->getLastname(); | |
$customerObj->setEmail($e); | |
$customerObj->setFirstname('Guest'); | |
$customerObj->setLastname('Guest'); | |
$customerObj->setPassword($pwd); | |
$customerObj->save(); | |
$customerObj->sendNewAccountEmail('confirmed'); //auto confirmed |
UPDATE: It depends where you want to keep this functionality on your site. I have put it in saveShippingMethodAction() function of Mage/Checkout/controllers/OnepageController.php file after extending. Don’t forget, this code should only be run if customer is guest.
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)