我想让新客户在结账过程中注册时事通讯(如果他创建一个新帐户)。
所以我在checkbox
文件中添加了billing.phtml
,如下所示:
<input type="checkbox" name="is_subscribed"
title="newsletter_signup" value="1" class="checkbox"/>
<?php echo $this->__('Sign Up for Newsletter 2') ?>
但什么都没发生。那没起效。我想我必须做得更多?但我不知道是什么?
答案 0 :(得分:2)
以下是您可以遵循的流程 -
在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>
Observer代码是:
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;
}
完整模块:
Step1:在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>
Step2:在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:创建模块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上添加简报字段
<input type="checkbox" name="is_subscribed" title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1" checked="checked" class="checkbox" />