每当有新客户注册时,我想向我商店的联系电子邮件地址发送电子邮件通知。
我不想购买任何类型的扩展,所以请帮我这样做
提前致谢
最佳实践是使用 Magento 的事件系统。
应用程序/etc/modules/Your_Module.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Your_Module>
<active>true</active>
<codePool>local</codePool>
</Your_Module>
</modules>
</config>
app/core/local/Your/Module/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<global>
<models>
<your_module>
<class>Your_Module_Model</class>
</your_module>
</models>
</global>
<frontend>
<events>
<customer_save_after>
<observers>
<your_module>
<type>model</type>
<class>your_module/observer</class>
<method>customerSaveAfter</method>
</your_module>
</observers>
</customer_save_after>
</events>
</frontend>
</config>
app/code/local/Your/Module/Model/Observer.php
<?php
class Your_Module_Model_Observer
{
public function customerSaveAfter(Varien_Event_Observer $o)
{
//Array of customer data
$customerData = $o->getCustomer()->getData();
//email address from System > Configuration > Contacts
$contactEmail = Mage::getStoreConfig('contacts/email/recipient_email');
//Mail sending logic here.
/*
EDIT: AlphaCentauri reminded me - Forgot to mention that
you will want to test that the object is new. I **think**
that you can do something like:
*/
if (!$o->getCustomer()->getOrigData()) {
//customer is new, otherwise it's an edit
}
}
}
编辑:请注意代码中的编辑 - 正如 AlphaCentauri 指出的那样,该customer_save_after
事件会为插入和更新而触发。_origData
条件逻辑应该允许您合并他的邮件逻辑。_origData 将是null
.
它可以用 Magento 事件/观察者系统完美地完成。首先,注册你的模块。
应用程序/etc/modules/Namespace_Modulename.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Namespace_Modulename>
<active>true</active>
<codePool>local</codePool>
</Namespace_Modulename>
</modules>
</config>
比为它写一个配置文件。
应用程序/代码/本地/命名空间/模块名/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Modulename>
<version>0.0.1</version>
</Namespace_Modulename>
</modules>
<frontend>
<events>
<customer_register_success>
<observers>
<unic_observer_name>
<type>model</type>
<class>unic_class_group_name/observer</class>
<method>customerRegisterSuccess</method>
</unic_observer_name>
</observers>
</customer_register_success>
</events>
<helpers>
<unic_class_group_name>
<class>Namespace_Modulename_Helper</class>
</unic_class_group_name>
</helpers>
</frontend>
<global>
<models>
<unic_class_group_name>
<class>Namespace_Modulename_Model</class>
</unic_class_group_name>
</models>
<template>
<email>
<notify_new_customer module="Namespace_Modulename">
<label>Template to notify administrator that new customer is registered</label>
<file>notify_new_customer.html</file>
<type>html</type>
</notify_new_customer>
</email>
</template>
</global>
</config>
这里发生了一些事情:
customer_register_success
(它在第 335 行发送Mage_Customer_AccountController
) 。frontend/events
它比使用 更好customer_save_after
,因为每次保存客户时都会触发最后一个,不仅在他注册时;global/template/email
。允许我们使用它发送自定义电子邮件。接下来创建一个电子邮件模板(文件)。
app/locale/en_US/template/notify_new_customer.html
Congratulations, a new customer has been registered:<br />
Name: {{var name}}<br />
Email: {{var email}}<br />
...<br />
之后定义一个观察者方法。
app/code/local/Namespace/Modulename/Model/Observer.php
class Namespace_Modulename_Model_Observer
{
public function customerRegisterSuccess(Varien_Event_Observer $observer)
{
$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('notify_new_customer');
$emailTemplate
->setSenderName(Mage::getStoreConfig('trans_email/ident_support/name'))
->setSenderEmail(Mage::getStoreConfig('trans_email/ident_support/email'))
->setTemplateSubject('New customer registered');
$result = $emailTemplate->send(Mage::getStoreConfig('trans_email/ident_general/email'),(Mage::getStoreConfig('trans_email/ident_general/name'), $observer->getCustomer()->getData());
}
}
编辑:正如@benmarks 指出的那样,如果客户在结账时注册,此解决方案将不起作用。此处描述了此行为的解决方案。但是,我认为,最好使用_origData
@benmarks 建议的功能。因此,以他的回答为指导来实现您的需求。
有用的链接:
作为基于事件的方法的替代方案,您可以运行一个单独的基于 API 的脚本来获取新的(或更新的)客户并将他们通过电子邮件发送给您,您可能希望或可能不希望获得一个每天一次的列表,而不是也比每个客户的电子邮件。
好处:
这是我最近使用的一个例子,它几乎正是你想要的,这就是为什么它引起了我的注意。代码可在此处获得。
$client =
new SoapClient('http://www.yourstore.com/magento/api/soap?wsdl');
$session = $client->login('TEST_USER', 'TEST_PASSWORD');
$since = date("Y-m-d", strtotime('-1 day'));
// use created_at for only new customers
$filters = array('updated_at' => array('from' => $since));
$result = $client->call($session, 'customer.list', array($filters));
$email = "New customers since: $since\n";
foreach ($result as $customer) {
$email .= $customer["firstname"] ." ".
$customer["lastname"] . ", " .
$customer["email"] . "\n";
}
mail("customer-manager@yourstore.com", "Customer report for: $since", $email);
您可以扩展Mage/Customer/Resource/Customer.php
- 受保护的功能_beforeSave(Varien_Object $customer)
if ($result) {
throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('This customer email already exists'), Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS);
} else {
// SEND EMAIL - Use a custom template
}
您可以尝试使用此扩展程序获取每个新客户注册的通知电子邮件,包括可自定义的电子邮件模板。 http://www.magentocommerce.com/magento-connect/customer-registration-notification.html
您可以尝试使用此扩展程序获取每个新客户注册的通知电子邮件,包括可自定义的电子邮件模板。http://www.magentocommerce.com/magento-connect/customer-registration-notification.html
这是用于向管理员发送新客户电子邮件的代码,还将
文件覆盖\app\code\core\Mage\Customer\Model\Customer.php
到本地
\app\code\local\Mage\Customer\Model\Customer.php
替换下面的函数
protected function _sendEmailTemplate($template, $sender, $templateParams = array(), $storeId = null)
{
/** @var $mailer Mage_Core_Model_Email_Template_Mailer */
$mailer = Mage::getModel('core/email_template_mailer');
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($this->getEmail(), $this->getName());
$mailer->addEmailInfo($emailInfo);
// Set all required params and send emails
$mailer->setSender(Mage::getStoreConfig($sender, $storeId));
$mailer->setStoreId($storeId);
$mailer->setTemplateId(Mage::getStoreConfig($template, $storeId));
$mailer->setTemplateParams($templateParams);
$mailer->send();
return $this;
}
至
protected function _sendEmailTemplate($template, $sender, $templateParams = array(), $storeId = null)
{
/** @var $mailer Mage_Core_Model_Email_Template_Mailer */
$mailer = Mage::getModel('core/email_template_mailer');
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($this->getEmail(), $this->getName());
if($template="customer/create_account/email_template"){
$emailInfo->addBcc(Mage::getStoreConfig('trans_email/ident_general/email'), $this->getName());
//Add email address in Bcc you want also to send
}
$mailer->addEmailInfo($emailInfo);
// Set all required params and send emails
$mailer->setSender(Mage::getStoreConfig($sender, $storeId));
$mailer->setStoreId($storeId);
$mailer->setTemplateId(Mage::getStoreConfig($template, $storeId));
$mailer->setTemplateParams($templateParams);
$mailer->send();
return $this;
}