Browsing articles tagged with "customer Archives - Page 2 of 2 - Kalpesh Mehta"
Apr 25, 2013
kalpesh

Magento: Check if any particular customer is currently logged in

Let’s say you want to check if any particular customer is currently logged in to your site or not. Or let’s check how many customers with their customer IDs and other activities are online on your store.

This little script will help you in finding all the currently active OR any particular customer(s) active in your store.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require "app/Mage.php";
umask(0);
Mage::app();

$collection = Mage::getModel('log/visitor_online')->prepare()->getCollection();

//Get all the customers that are logged in......
foreach($collection->getData() as $cust) {
        echo 'Customer ID: '.$cust['customer_id'] . '<br/>';
        echo 'Last URL visited: '.$cust['last_url'] . '<br/>';
        echo 'First visit: '.$cust['first_visit_at'] . '<br/>';
        echo 'Last visit: '.$cust['last_visit_at'] . '<br/>';
        echo '======================<br/>';
}

//Get any particular customer, if he's currently logged in or not.....
$collection->addFieldToFilter('customer_id', 5)->addCustomerData(); //5 is customer ID of customer you want to check
if($collection->count()) {
    echo 'Customer is logged in';
} else {
    echo 'Customer is NOT logged in';
}
Dec 31, 2011
kalpesh

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$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.

Jun 19, 2011
kalpesh

Magento: Get customer details : customer id, name, email

Magento get the logged in customer details like customer id, name, email, etc. Check if customer is already logged in or not, if they are logged in then get all the data using customer session object and use it as per the requirement.

1
2
3
4
5
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customer = Mage::getSingleton('customer/session')->getCustomer();
    $customerData = Mage::getModel('customer/customer')->load($customer->getId())->getData();
    Mage::log($customerData);
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Array
(
    [entity_id] => 1
    [entity_type_id] => 1
    [attribute_set_id] => 0
    [website_id] => 1
    [email] => john.doe@example.com
    [group_id] => 1
    [increment_id] => 000000001
    [store_id] => 1
    [created_at] => 2007-08-30 23:23:13
    [updated_at] => 2008-08-08 12:28:24
    [is_active] => 1
    [firstname] => John
    [lastname] => Doe
    [password_hash] => 204948a4020ed1d0e4238db2277d5:eg
    [prefix] => 
    [middlename] => 
    [suffix] => 
    [taxvat] => 
    [default_billing] => 274
    [default_shipping] => 274
)

Getting customer details from the $customerData object:

1
2
3
4
5
$customerID = $customerData->getId(); //entity_id can be used as id
$name = $customerData->getFirstname() . ' ' . $customerData->getLastname();
$isActive = $customerData->getIsActive();
$email = $customerData->getEmail();
$incrId = $customerData->getIncrementId();

Similarly, we can get lastname, email, etc. ( [website_id] = getWebsiteId() )

Pages:«12

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications