我们将我们的时事通讯订阅者存储在一个单独的数据库中给Magento,因此当用户注册一个帐户时,我们需要将他们的地址添加到该数据库。
代码位于:
/app/code/core/Mage/Newsletter/Model/Subscriber.php
相关功能(我认为)是subscribeCustomer。
如果我将代码插入此函数的顶部,它会将用户的电子邮件地址添加到单独的数据库中,这很棒。但无论是否勾选了“订阅简报”框,它都会这样做。
我对PHP很陌生,因此很难弄清楚代码在复选框中的位置。它在这个函数中的某个地方吗?
public function subscribeCustomer($customer)
{
$this->loadByCustomer($customer);
if ($customer->getImportMode()) {
$this->setImportMode(true);
}
if (!$customer->getIsSubscribed() && !$this->getId()) {
// If subscription flag not set or customer is not a subscriber
// and no subscribe below
return $this;
}
if(!$this->getId()) {
$this->setSubscriberConfirmCode($this->randomSequence());
}
/*
* Logical mismatch between customer registration confirmation code and customer password confirmation
*/
$confirmation = null;
if ($customer->isConfirmationRequired() && ($customer->getConfirmation() != $customer->getPassword())) {
$confirmation = $customer->getConfirmation();
}
$sendInformationEmail = false;
if ($customer->hasIsSubscribed()) {
$status = $customer->getIsSubscribed()
? (!is_null($confirmation) ? self::STATUS_UNCONFIRMED : self::STATUS_SUBSCRIBED)
: self::STATUS_UNSUBSCRIBED;
/**
* If subscription status has been changed then send email to the customer
*/
if ($status != self::STATUS_UNCONFIRMED && $status != $this->getStatus()) {
$sendInformationEmail = true;
}
} elseif (($this->getStatus() == self::STATUS_UNCONFIRMED) && (is_null($confirmation))) {
$status = self::STATUS_SUBSCRIBED;
$sendInformationEmail = true;
} else {
$status = ($this->getStatus() == self::STATUS_NOT_ACTIVE ? self::STATUS_UNSUBSCRIBED : $this->getStatus());
}
if($status != $this->getStatus()) {
$this->setIsStatusChanged(true);
}
$this->setStatus($status);
if(!$this->getId()) {
$storeId = $customer->getStoreId();
if ($customer->getStoreId() == 0) {
$storeId = Mage::app()->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
}
$this->setStoreId($storeId)
->setCustomerId($customer->getId())
->setEmail($customer->getEmail());
} else {
$this->setStoreId($customer->getStoreId())
->setEmail($customer->getEmail());
}
$this->save();
$sendSubscription = $customer->getData('sendSubscription') || $sendInformationEmail;
if (is_null($sendSubscription) xor $sendSubscription) {
if ($this->getIsStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
$this->sendUnsubscriptionEmail();
} elseif ($this->getIsStatusChanged() && $status == self::STATUS_SUBSCRIBED) {
$this->sendConfirmationSuccessEmail();
}
}
return $this;
}
答案 0 :(得分:0)
您应该使用事件观察者并避免将代码添加到Magento的核心。
在app/code/local
或app/code/community
中创建一个新模块,并在config.xml
节点下的config > global > events
中添加以下内容。
<newsletter_subscriber_save_commit_after>
<observers>
<newsletter_subscriber_external_db>
<class>Namespace_Module_Model_Observer</class>
<method>subscribeExternalDb</method>
</newsletter_subscriber_external_db>
</observers>
</newsletter_subscriber_save_commit_after>
然后在observer类中添加以下方法:
class Namespace_Module_Model_Observer
{
/**
* Add subscriber to external database
*
* @param Varien_Event_Observer $observer
*/
public function subscribeExternalDb(Varien_Event_Observer $observer)
{
/** @var Mage_Newsletter_Model_Subscriber $subscriber */
$subscriber = $observer->getEvent()->getSubscriber();
// do whatever with subscriber model
}
}