【问题标题】:How to send new password from Magento CSV import of Customers如何从客户的 Magento CSV 导入发送新密码
【发布时间】:2012-11-14 22:00:45
【问题描述】:

我们一直在尝试修改 Customer.php 文件 (import/export),使其在从 CSV 文件导入客户时自动发送新帐户详细信息。

我们在正确的领域工作,因为我们放弃了一个简单的 mail() 调用,该调用针对每个新行调用(我们收到了电子邮件)。当试图让它生成一个新的随机密码并发送新的帐户详细信息时会出现问题 - 它从不发送任何邮件,我们无法弄清楚为什么!代码如下(从 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;
}

【问题讨论】:

    标签: php email magento csv-import


    【解决方案1】:

    我找到了这个关于如何为现有客户自动生成密码的脚本

    $passwordLength = 10;
    $customers = Mage::getModel('customer/customer')->getCollection();
    foreach ($customers as $customer){
        $customer->setPassword($customer->generatePassword($passwordLength))->save();
        $customer->sendNewAccountEmail();
    }
    

    【讨论】:

    • 这很酷 - 但我们需要一个解决方案,既可以从导入的 csv 文件创建帐户,又可以发送新生成的凭据。如果您希望我弹出代码作为示例,然后发表评论 - 它作为上面的章节有所帮助,因为问题是我在导入时使用了不正确的站点 ID 号 - 将其硬编码到正确的那个清除了我的问题!
    【解决方案2】:

    为了提高 magento 'caches' 对象的性能,因此当尝试在循环中加载对象时,您要么需要加载新实例,要么调用它的 reset() 方法

    $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
    }
    

    如果您加载大量客户,那么您可能会遇到内存问题,我需要使用 reset() 寻找其他技术

    如果此代码是从 Magento 的管理部分运行的,那么您需要确保设置了正确的网站 ID(Mage::app()->getWebsite()->getId() 将返回不正确的 ID)。

    从前端,此代码可以正常工作,但对于管理员工作,您应该使用“1”或任何您的前端网站 ID,因为 getId() 方法在管理区域返回“0”!小问题,但让我走了几个小时!

    【讨论】:

    • 同意这个答案;你不能使用loadByEmail,除非你之前指定了网站
    • 这个解决方案仍然没有发送任何电子邮件 :( 很好地注意对象缓存!
    • 只是澄清一下 - 导入仍然成功进行 - 它只是没有向 CSV 中的任何人发送“新帐户”电子邮件。当前的 CSV 只有五个要导入的测试帐户,我们通过在每次尝试导入之前清除整个客户列表来确保它们被导入。
    • 如果您转到 Admin -> customer 并使用 csv 文件中的电子邮件编辑一位客户(发送新密码检查),您会收到电子邮件吗?
    • 是的,我们可以 - 刚刚通过编辑我自己的姓名并选中“新生成的密码”框进行确认,几秒钟后收到了电子邮件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    相关资源
    最近更新 更多