在结算表单中添加时事通讯复选框


11

当用户选择直接在结​​帐过程中进行注册时,我想在开票表格中添加复选框时事通讯。我该怎么做?


2
您是否有任何计划利用MailChimp满足您的时事通讯邮寄需求?ezbizmarts的MageMonkey扩展具有配置选项,可将电子报注册添加到结帐流程。
达伦·费尔顿

Answers:


15

您好,您可以通过在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()->getCustomerEmail());
        }
        endif;
   }

完整模块:

步骤1:在处创建config.xml,app/code/local/Stackexchange/Magento52274/etc/代码为

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

第2步:Observer.phpapp/code/local/Stackexchange/Magento52274/Model/和创建代码为

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

}

第三步:创建模块 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>

第四步:同时还需要在增加通讯领域的billing.phtml

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

2
这可能需要一些清理工作-至少,“完整模块”下的某些代码与答案前面的代码不匹配。
迈克

2
很好,只是-> getBillingAddress()-> getEmail()并不总是获取电子邮件,尤其是涉及到贝宝电子邮件时。getCustomerEmail更安全。
Claudiu Creanga '16

请忽略此答案。我不知道这怎么能得到这么多的支持。这行不通!您无法在checkout_submit_all_after观察者中访问开票表单字段。请看看这个实现:magento.stackexchange.com/questions/219460/...
迈克尔Thessel
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.