We have been trying to modify the Customer.php file ( import/export ) to get it to send out the new account details automatically as it imports the customers from a CSV file.

We are working in the right area as we dropped a simple mail() call which was called (we received the emails) for every new row. The problem arises when trying to get it to generate a new random password and send out the new account details - it never sends any mail and we cannot figure out why! Code follows ( Edited from app/code/local/Mage/ImportExport/Model/Import/Entity/Customer.php )

 /**
 * Update and insert data in entity table.
 *
 * @param array $entityRowsIn Row for insert
 * @param array $entityRowsUp Row for update
 * @return Mage_ImportExport_Model_Import_Entity_Customer
 */
protected function _saveCustomerEntity(array $entityRowsIn, array $entityRowsUp)
{
    if ($entityRowsIn) {
        $this->_connection->insertMultiple($this->_entityTable, $entityRowsIn);

          // BEGIN: Send New Account Email          
          $cust = Mage::getModel('customer/customer');
          $cust->setWebsiteId(Mage::app()->getWebsite()->getId());              
          foreach($entityRowsIn as $idx => $u){
            // Failed
            $cust->loadByEmail($u['email']);
            $cust->setConfirmation(NULL);
            $cust->setPassword($cust->generatePassword(8));
            $cust->save();
            $cust->sendNewAccountEmail();
            //$cust->sendPasswordReminderEmail(); // this call doesnt work either
          }
          // END: Send New Account Email

    }
    if ($entityRowsUp) {
        $this->_connection->insertOnDuplicate(
            $this->_entityTable,
            $entityRowsUp,
            array('group_id', 'store_id', 'updated_at', 'created_at')
        );
    }
    return $this;
}
有帮助吗?

解决方案

Inorder to improve performance magento 'caches' object, so when trying to load an object in a loop you either need to load a new instance or call it reset() method

$website_id = Mage::app()->getWebsite()->getId();       
foreach($entityRowsIn as $idx => $u){
    $cust = Mage::getModel('customer/customer');
    $cust->setWebsiteId($website_id);
    $cust->loadByEmail($u['email']);
    $cust->setPassword($cust->generatePassword(8));
    $cust->save();
    $cust->sendNewAccountEmail();  // if you are getting 2 email then remove this line
}

If you are load a large number of customers then you may run in to memory issues and my need to look a other technique using reset()

If this code is being run from the Administration section of Magento then you will need to make sure that you set the correct Website ID ( Mage::app()->getWebsite()->getId() will return the incorrect one ).

From the front end this code will work fine but for Admin work you should use '1' or whatever your Front end websites ID is as the getId() method was returning '0' in the Admin area! Small gotcha but had me going for hours!

其他提示

I had found this script on how to Auto generate password for existing customers

$passwordLength = 10;
$customers = Mage::getModel('customer/customer')->getCollection();
foreach ($customers as $customer){
    $customer->setPassword($customer->generatePassword($passwordLength))->save();
    $customer->sendNewAccountEmail();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top