1

I'm running into the following problem: I have a list of emails in a file. I want to loop through these and create customers, then I want to do something with the customer. However in my script it's only loading the last customer.

I have the following code:

require_once("./app/Mage.php");
Mage::app();

$file = fopen("Test.txt", "r");
if ($file) {
    $emails = file("Test.txt");
    foreach($emails as $email) {

    echo $email . "<br/>"; // < shows all emails

    // load up the customer 
    $customer = Mage::getModel('customer/customer')
    ->setWebsiteId(1)
    ->loadByEmail($email);

    echo $customer->getId() . "<br/>"; // < returns blank except for the last email line

    // if we found the customer
    if ($customer->getId()){
        echo $customer->getId() . "adsf<br/>"; // < only shown on last line
        //.. do something
    }
    }

    fclose($file);
} else {
    // error opening the file.
}

I have also tried

while(!feof($file)){
    $email = fgets($file);

and

while (($email = fgets($file)) !== false)

but same result, so it appears it's not something wrong with the php code. Can someone help me?

2
  • have you check this email is exits in admin
    – Amit Bera
    Apr 29, 2015 at 14:27
  • yes they exist. I tested it also by putting a different email last in the file, then it always takes the last email
    – Robin
    Apr 29, 2015 at 14:28

1 Answer 1

1

Below should read your file line by line and load the customers. I've had issues before where text files saved on Windows had unreadable line endings. Make sure it doesn't see all the email addresses as one line

if ($file = fopen("Test.txt", "rfile)) {
    while (($email = fgets($handle)) !== false) {
        echo $email . "<br/>"; // < shows all emails

        // load up the customer 
        $customer = Mage::getModel('customer/customer')
           ->setWebsiteId(1)
           ->loadByEmail(trim($email));

         echo $customer->getId() . "<br/>"; // < returns blank except for the last email line

         // if we found the customer
         if ($customer->getId()){
            echo $customer->getId() . "adsf<br/>"; // < only shown on last line
            //.. do something
         }
    }

    fclose($file);
}
0

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