[英]How can i add a driver tip in magento onepage checkout process?
目前我需要在magento onepage checkout过程中添加自定义Driver tip步骤,紧接在送货方法(步骤3)之后,我希望用户从某些给定选项中选择提示(我将制作单选按钮),将包含一定金额,假设用户选择150美元,那么这笔金额将被添加到总付款中? 我尝试了谷歌上的所有其他教程,没有一个对我有用,任何帮助表示赞赏,
我最近研究类似的要求。 所以按照我的指示: -
我请求你不要专注于答案的长度只关注结果
第1步 : - 如果您想在发货和发货方式之间放置驱动程序提示,请先打开
\\ app \\ code \\ core \\ Mage \\ Checkout \\ Block \\ Onepage.php有一个数组$ stepCodes(行号: - 44)。 用它替换它
$stepCodes = array('billing', 'shipping', 'excellence2','shipping_method', 'payment', 'review');
我正在使用Excellence2你也可以使用这个名字。
第2步 : - 现在我们需要在app \\ code \\ core \\ Mage \\ Checkout \\ Block \\ Onepage \\上创建Excellence2类,所以创建一个新的php文件并将该代码放入其中并保存为Excellence2.php
class Mage_Checkout_Block_Onepage_Excellence2 extends Mage_Checkout_Block_Onepage_Abstract
{
protected function _construct()
{
$this->getCheckout()->setStepData('excellence2', array(
'label' => Mage::helper('checkout')->__('Tip Ammount'),
'is_show' => $this->isShow()
));
parent::_construct();
}
}
注意 : - 现在您可以在_construct()函数的标签上添加任何名称,以便将'Tip Ammount'更改为Driver tip
第3步 : - 现在打开OnepageController.php ,它位于app \\ code \\ core \\ Mage \\ Checkout \\ controllers \\中,找到saveBillingAction()函数(行号:-296)并用此替换该代码
public function saveBillingAction()
{
if ($this->_expireAjax())
{
return;
}
if ($this->getRequest()->isPost()) {
// $postData = $this->getRequest()->getPost('billing', array());
// $data = $this->_filterPostData($postData);
$data = $this->getRequest()->getPost('billing', array());
$customerAddressId = $this->getRequest()->getPost('billing_address_id', false);
if (isset($data['email'])) {
$data['email'] = trim($data['email']);
}
$result = $this->getOnepage()->saveBilling($data, $customerAddressId);
if (!isset($result['error'])) {
/* check quote for virtual */
if ($this->getOnepage()->getQuote()->isVirtual()) {
$result['goto_section'] = 'payment';
$result['update_section'] = array(
'name' => 'payment-method',
'html' => $this->_getPaymentMethodsHtml()
);
} elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
$result['goto_section'] = 'excellence2'; //Goes to our step
$result['allow_sections'] = array('shipping');
$result['duplicateBillingInfo'] = 'true';
} else {
$result['goto_section'] = 'shipping';
}
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
第4步 : - 再次在同一个文件OnepageController.php中有一个saveShippingAction()函数(第331行)将此更改为
public function saveShippingAction()
{
if ($this->_expireAjax()) {
return;
}
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost('shipping', array());
$customerAddressId = $this->getRequest()->getPost('shipping_address_id', false);
$result = $this->getOnepage()->saveShipping($data, $customerAddressId);
if (!isset($result['error'])) {
$result['goto_section'] = 'excellence2'; //Go to our step
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
此代码告诉用户通过发货步骤后,他将转到我们的驱动程序提示步骤。
步骤5 : - 在同一个文件中( OnepageController.php )我们需要告诉用户在通过驱动程序提示步骤之后将重定向的位置。在saveShippingAction()函数之后创建一个saveExcellence2Action()
public function saveExcellence2Action()
{
if ($this->_expireAjax()) {
return;
}
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost('excellence2', array());
$result = $this->getOnepage()->saveExcellence2($data);
if (!isset($result['error'])) {
$result['goto_section'] = 'shipping_method';
$result['update_section'] = array(
'name' => 'shipping-method',
'html' => $this->_getShippingMethodsHtml()
);
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
第6步 : - 现在我们更改位于的checkout.xml
\\ app \\ design \\ frontend \\ default \\ your template \\ layout打开并查找
<checkout_onepage_index translate="label">
在那个特定的节点有块(第326行)
<block type="checkout/onepage_shipping" name="checkout.onepage.shipping" as="shipping" template="checkout/onepage/shipping.phtml"/>
只需在上面显示的这一行之后添加一个新块
<block type="checkout/onepage_excellence2" name="checkout.onepage.excellence2" as="excellence2" template="checkout/onepage/excellence2.phtml"/>
第7步 : - 现在我们需要在\\ app \\ design \\ frontend \\ default \\ your template \\ template \\ checkout \\ onepage \\创建一个excellent2.phtml文件
此文件显示您要向用户显示的内容
<form id="co-excellence2-form" action="">
<div class="wide"> <label for="excellence2:like" class="required"><em style="color:#F00;">*</em> Select the amount of tip ,You wish to give to the driver.</label>
</div>
<div style="margin-top:20px;">
<ul>
<li>
<input type="radio" name="excellence2[like]" id="excellence2:like" value="0" checked="checked" class="radio" onclick="savevalue(this.value);"/> No Tip/Pay driver at the door
</li>
<li>
<input type="radio" name="excellence2[like]" id="excellence2:like" value="150" class="radio" onclick="savevalue(this.value);" /> 150$
</li>
<li>
<input type="radio" name="excellence2[like]" id="excellence2:like" value="250" class="radio" onclick="savevalue(this.value);"/> 250$
</li>
<li>
<input type="radio" name="excellence2[like]" id="excellence2:like" value="400" class="radio" onclick="savevalue(this.value);" /> 400$
</li>
<li>
<input type="radio" name="excellence2[like]" id="excellence2:like" value="500" class="radio" onclick="savevalue(this.value);"/> 500$
</li>
<li>
<input type="radio" name="excellence2[like]" id="excellence2:like" value="15% of total amount" class="radio" onclick="savevalue(this.value);" /> 15% of Total Amount
</li>
</ul>
</div>
<fieldset>
<div class="buttons-set" id="excellence2-buttons-container">
<p class="required"><?php echo $this->__('* Required Fields') ?></p>
<button type="button" title="<?php echo $this->__('Continue') ?>" class="button" onclick="excellence2.save()"><span><span><?php echo $this->__('Continue') ?></span></span>
</button>
<span class="please-wait" id="excellence2-please-wait" style="display:none;">
<img src="<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif') ?>" alt="<?php echo $this->__('Loading next step...') ?>" title="<?php echo $this->__('Loading next step...') ?>" class="v-middle" /> <?php echo $this->__('Loading next step...') ?>
</span>
</div>
</fieldset>
</form>
<script type="text/javascript">
//<![CDATA[
var excellence2 = new ExcellenceMethod2('co-excellence2-form','<?php echo $this->getUrl('checkout/onepage/saveExcellence2') ?>');
var excellenceForm2 = new VarienForm('co-excellence2-form');
//]]>
</script>
第8步 : - 现在我们需要在\\ skin \\ frontend \\ default \\你的模板\\ js创建一个excellencecheckout.js文件
var ExcellenceMethod2 = Class.create();
ExcellenceMethod2.prototype = {
initialize: function(form, saveUrl){
this.form = form;
if ($(this.form)) {
$(this.form).observe('submit', function(event){this.save();Event.stop(event);}.bind(this));
}
this.saveUrl = saveUrl;
this.validator = new Validation(this.form);
this.onSave = this.nextStep.bindAsEventListener(this);
this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
},
validate: function() {
return true;
},
save: function(){
//alert('hi');
if (checkout.loadWaiting!=false) return;
if (this.validate()) {
checkout.setLoadWaiting('excellence2');
var request = new Ajax.Request(
this.saveUrl,
{
method:'post',
onComplete: this.onComplete,
onSuccess: this.onSave,
onFailure: checkout.ajaxFailure.bind(checkout),
parameters: Form.serialize(this.form)
}
);
}
},
resetLoadWaiting: function(transport){
checkout.setLoadWaiting(false);
},
nextStep: function(transport){
if (transport && transport.responseText){
try{
response = eval('(' + transport.responseText + ')');
}
catch (e) {
response = {};
}
}
if (response.error) {
alert(response.message);
return false;
}
if (response.update_section) {
$('checkout-'+response.update_section.name+'-load').update(response.update_section.html);
}
if (response.goto_section) {
//alert(response);
checkout.gotoSection(response.goto_section);
checkout.reloadProgressBlock();
return;
}
checkout.setPayment();
}
}
步骤9 : - 现在我们需要创建一个新功能来保存选择用户的数据。 所以我们正在创建一个名为saveExcellence2()的新函数,它位于Onepage.php中的saveShipping($ data,$ customerAddressId)后面,位于\\ app \\ code \\ core \\ Mage \\ Checkout \\ Model \\ Type \\
public function saveExcellence2($data)
{
if (empty($data))
{
return array('error' => -1, 'message' => $this->_helper->__('Invalid data.'));
}
$this->getQuote()->setExcellenceLike2($data['like']);
$this->getQuote()->collectTotals();
$this->getQuote()->save();
$this->getCheckout()
->setStepData('excellence2', 'complete', true)
->setStepData('shipping_method', 'allow', true);
return array();
}
默认情况下,magento会提供一些结帐步骤。 但有时您需要从客户那里添加额外信息以供将来参考。 常见的请求自定义是在默认结帐过程中添加自定义表单。 这不是触摸核心文件的好习惯。 您可以通过覆盖模块来完成此操作。 在此示例中,Comapnyname是Ipragmatech ,模块名称是Checkoutstep 。
步骤1:在结帐过程中添加自定义步骤
打开Ipragmatech> Checkoutstep> Block> Onepage> Checkoutstep.php文件并编写以下代码
class Ipragmatech_Checkoutstep_Block_Onepage_Checkoutstep extends Mage_Checkout_Block_Onepage_Abstract
{
protected function _construct()
{
$this->getCheckout()->setStepData('checkoutstep', array(
'label' => Mage::helper('checkout')->__('Invitation to participation'),
'is_show' => true
));
parent::_construct();
}
}
第2步:在结帐过程中添加您想要的步骤
打开Ipragmatech> Checkoutstep> Block> Onepage> Checkoutstep.php文件并编写以下代码
class Ipragmatech_Checkoutstep_Block_Onepage extends Mage_Checkout_Block_Onepage
{
public function getSteps()
{
$steps = array();
if (!$this->isCustomerLoggedIn()) {
$steps['login'] = $this->getCheckout()->getStepData('login');
}
$stepCodes = array('billing', 'shipping', 'shipping_method', 'payment', 'checkoutstep', 'review');
foreach ($stepCodes as $step) {
$steps[$step] = $this->getCheckout()->getStepData($step);
}
return $steps;
}
}
第3步:获取自定义表单的提交值并设置自定义表单的值
打开ipragmatech> Checkoutstep> controllers> OnepageController.php并编写以下功能
public function saveCheckoutstepAction()
{
$this->_expireAjax();
if ($this->getRequest()->isPost()) {
//Grab the submited value
$_entrant_name = $this->getRequest()->getPost('entrant_name',"");
$_entrant_phone = $this->getRequest()->getPost('entrant_phone',"");
$_entrant_email = $this->getRequest()->getPost('entrant_email',"");
$_permanent_address = $this->getRequest() ->getPost('permanent_address',"");
$_address = $this->getRequest()->getPost('local_address',"");
Mage::getSingleton('core/session') ->setIpragmatechCheckoutstep(serialize(array(
'entrant_name' =>$_entrant_name,
'entrant_phone' =>$_entrant_phone,
'entrant_email' =>$_entrant_email,
'permanent_address' =>$_permanent_address,
'address' =>$_address
)));
$result = array();
$redirectUrl = $this->getOnePage()->getQuote()->getPayment() ->getCheckoutRedirectUrl();
if (!$redirectUrl) {
$this->loadLayout('checkout_onepage_review');
$result['goto_section'] = 'review';
$result['update_section'] = array(
'name' => 'review',
'html' => $this->_getReviewHtml()
);
}
if ($redirectUrl) {
$result['redirect'] = $redirectUrl;
}
$this->getResponse()->setBody(Zend_Json::encode($result));
}
}
第4步:保存自定义表单信息
当调用checkout_onepage_controller_success_action事件挂钩时。 打开Ipragmatech> Checkoutstep> Model> Observer.php并编写以下内容
class Ipragmatech_Checkoutstep_Model_Observer {
const ORDER_ATTRIBUTE_FHC_ID = 'checkoutstep';
public function hookToOrderSaveEvent() {
if (Mage::helper('checkoutstep')->isEnabled()) {
$order = new Mage_Sales_Model_Order ();
$incrementId = Mage::getSingleton ( 'checkout/session' )->getLastRealOrderId ();
$order->loadByIncrementId ( $incrementId );
// Fetch the data
$_checkoutstep_data = null;
$_checkoutstep_data = Mage::getSingleton ( 'core/session' )->getIpragmatechCheckoutstep ();
$model = Mage::getModel ( 'checkoutstep/customerdata' )->setData ( unserialize ( $_checkoutstep_data ) );
$model->setData ( "order_id",$order["entity_id"] );
try {
$insertId = $model->save ()->getId ();
Mage::log ( "Data successfully inserted. Insert ID: " . $insertId, null, 'mylog.log');
} catch ( Exception $e ) {
Mage::log ( "EXCEPTION " . $e->getMessage (), null, 'mylog.log' );
}
}
}
}
Magento - 在Checkout Extension中添加自定义表单是一个完整的解决方案,可以为您的电子商务网站添加Checkout流程中的额外步骤。 它允许管理员以CSV格式从自定义表中导出数据。 访问该链接获取此免费扩展程序http://www.magentocommerce.com/magento-connect/custom-form-in-checkout.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.