【问题标题】:Magento add newsletter subscribe in checkoutMagento 在结帐时添加时事通讯订阅
【发布时间】:2019-03-24 09:39:03
【问题描述】:

我想让新客户能够在结帐过程中注册时事通讯(如果他创建了新帐户)。

所以我在billing.phtml 文件中放了一个checkbox,如下所示:

<input type="checkbox" name="is_subscribed" 
      title="newsletter_signup" value="1"  class="checkbox"/>
<?php echo $this->__('Sign Up for Newsletter 2') ?>

但是什么都没发生。那没起效。我想我必须做得更多?但我不知道是什么?

【问题讨论】:

  • 我确定名字必须是billing[is_subscribed],你运行的是什么版本的Magento?
  • 你的意思是什么? Magento 1.9.1
  • 输入的名称。
  • 好的,我已经更改了名称,但它不起作用 - 我是否将 xml 或观察者文件包含在另一个现有文件中?这些文件将如何从 billing.phtml 中调用?

标签: magento newsletter


【解决方案1】:

这是您可以遵循的过程 -

在 billing.phtml 中添加以下代码

 <input type="checkbox" name="is_subscribed" title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1"  checked="checked" class="checkbox" />

使用事件checkout_submit_all_after为客户订阅时事通讯

<global>
 <events>
    <checkout_submit_all_after> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_submit_all_after_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento52274/observer</class> <!-- observers class alias -->
            <method>AssignNewletter</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_submit_all_after_handler>
        </observers>
      </checkout_submit_all_after>
    </events>
  </global>

而观察者代码是:

public function AssignNewletter($observer) {
            $event = $observer->getEvent();
            $order = $event->getOrder();
        $Quote =$event->getQuote();
        if (in_array($Quote()->getCheckoutMethod(), array('register','customer'))):

        if Mage::app()->getFrontController()->getRequest()->getParam('is_subscribed')){
        $status = Mage::getModel('newsletter/subscriber')->subscribe($Quote->getBillingAddress()->getEmail());
        }
        endif;
   }

完整模块:

第一步:在app/code/local/Stackexchange/Magento52274/etc/创建config.xml,代码为

<?xml version="1.0"?>
<config>
  <modules>
    <Stackexchange_Magento52274>
      <version>1.0.0</version>
    </Stackexchange_Magento52274>
  </modules>
  <global>
    <models>
      <magento52274>
        <class>Stackexchange_Magento52274_Model</class>
      </magento52274>
    </models>
    <events>
      <checkout_submit_all_after> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_submit_all_after_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento52274/observer</class> <!-- observers class alias -->
            <method>AssignNewletter</method>  <!-- observer's method to be called -->
          </checkout_submit_all_after_handler>
        </observers>
      </checkout_submit_all_after>
    </events>
  </global>
</config> 

第二步:在app/code/local/Stackexchange/Magento52274/Model/创建Observer.php,代码为

<?php
class Stackexchange_Magento52274_Model_Observer
{

    public function AssignNewletter(Varien_Event_Observer $observer)
    {
    $event = $observer->getEvent();
            $order = $event->getOrder();
        $Quote =$event->getQuote();
        if (in_array($Quote()->getCheckoutMethod(), array('register','customer'))):

            if Mage::app()->getFrontController()->getParam('is_subscribed', false)){
        $status = Mage::getModel('newsletter/subscriber')->subscribe($Quote->getBillingAddress()->getEmail());
         }
        endif;
    }

}

Step3:创建Module app/etc/modules/Stackexchange_Magento52274.xml,代码为

<?xml version="1.0"?>
<config>
  <modules>
    <Stackexchange_Magento52274>
      <active>true</active>
      <codePool>local</codePool>
      <version>1.0.0</version>
    </Stackexchange_Magento52274>
  </modules>
</config>

Step4:并且还需要在 billing.phtml a 中添加 newsletter 字段

<input type="checkbox" name="is_subscribed" title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1"  checked="checked" class="checkbox" />

【讨论】:

  • 我已经按照您所说的完成了这些步骤,但它不起作用。我是否将 xml 或观察者文件包含在另一个现有文件中?文件将如何从 billing.phtml 中调用?
  • 你接触到观察者了吗?请检查一次并告诉我。
  • 我认为这是问题所在 - 我如何检查它 - 或者如何到达观察者文件。在 billing.phml 中没有“链接”到观察者或其他 xml 文件 - 所以我不明白它应该如何工作......
  • if (in_array($Quote->getCheckoutMethod(), array('register','customer'))): 以上说法正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-06
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 2015-08-25
相关资源
最近更新 更多