1

I'm trying CLI coding. I just tried to get all Users, want to load them and list data or do some actions.

<?php

require_once 'abstract.php';

class Shell_List extends Mage_Shell_Abstract {
    public function run() {
        $users = Mage::getModel('customer/customer') -> getCollection() -> getData();
        foreach ($users as $u) {
            $user = mage::getModel('customer/customer') -> loadByEmail($email =  $u['email']);
            echo $email, ' ', $user -> getName(), "\n";
        }
    }
}

$shell = new Shell_List();
$shell -> run();

Well, the loadByEmail() methods throws an exception with the message

Customer website ID must be specified when using the website scope

That's logical, a user identified email can exist in more websites. How do I set the website, and maybe, list all users of the website?

2 Answers 2

2

I'm pretty sure you can just do this:

    $users = Mage::getModel('customer/customer')-> setWebsiteId(1)->getCollection() -> getData();

Replace 1 with your store code ID in setWebsiteId() to change the store. I'm away from my system so I didn't get a chance to try it.

0
1

Here is an example of how it should work without having to set the website ID manually:

$email = “[email protected]”;
$customer = Mage::getModel(“customer/customer”);
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($email);

On another note, you could simply get the website_id via the store_id which is saved in the customer data (in your case it would be u['store_id']):

$storeId = u['store_id'];
$website_id = Mage::getModel('core/store')->load($storeId)->getWebsiteId();
2
  • In a CLI script using shell/abstract.php, Mage::app()->getWebsite()->getId() will return 0.
    – Artisan72
    Commented Feb 9, 2015 at 14:12
  • But it's helpful for frontend code, thank you :)
    – Artisan72
    Commented Feb 9, 2015 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.