【问题标题】:Magento - Customising the Newsletter SubscriptionMagento - 自定义时事通讯订阅
【发布时间】:2015-05-05 09:24:49
【问题描述】:

我们将时事通讯订阅者存储在 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;
}

【问题讨论】:

    标签: php magento


    【解决方案1】:

    您应该使用事件观察器并避免将代码添加到 Magento 的核心。

    app/code/localapp/code/community 中创建一个新模块,并在config.xmlconfig > 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>
    

    然后在观察者类中添加如下方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 2015-08-25
      相关资源
      最近更新 更多