Load customer by ID in Magento

#magento #snippet

Here is a quick example of how to load customer by its ID in Magento. It is a standard load method that is used across all Magento models. Be sure - you will see this very often!


Get customer by ID in Magento

<?php

// The ID of customer you want to load
$customerId = 10000;

// Instance of customer loaded by the given ID
$customer = Mage::getModel('customer/customer')->load($customerId);

Keep in mind that load() method always returns model instance - even if customer with specified ID does not exist. Therefore, the proper way of doing an existence check is via getId() method.

<?php

if ($customer) { // Object always evaluates to TRUE
    echo "Bad check!";
}

if ($customer->getId()) { // TRUE if customer with given ID exists
    echo "Good check!";
}

That is it - you have successfully loaded customer by its ID and you should now be able to access all the desired customer data. Cheers & have fun!

<?php

if ($customer->getId()) {
    var_dump($customer->getId()); // Get customer ID
    var_dump($customer->getData()); // Get all customer data
    var_dump($customer->getEmail()); // Get customer email address
    var_dump($customer->getFirstname()); // Get customer firstname
}

If you are looking for the way of how to load customer by email or by custom customer attribute, go ahead and check these posts: load customer by email in Magento and load customer by custom attribute in Magento.

⇐ All Blog Posts
Tweet Share