簡體   English   中英

Magento-定制新聞訂閱

[英]Magento - Customising the Newsletter Subscription

我們將通訊訂閱者存儲在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;
}

您應該使用事件觀察器,並避免在Magento的核心中添加代碼。

app/code/localapp/code/community創建一個新模塊,並在config > global > events節點下的config.xml添加以下內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM