【问题标题】:How can i add a driver tip in magento onepage checkout process?如何在 magento onepage 结帐过程中添加驱动程序提示?
【发布时间】:2013-04-17 19:31:14
【问题描述】:

目前,我需要在 magento 单页结帐过程中添加自定义 司机提示 步骤,就在运输方式(第 3 步)之后,我希望用户从给定的一些中选择提示选项(我将制作单选按钮),它将包含一定的金额,假设用户选择了 150 美元,那么这个金额将被添加到总付款中?我通过谷歌尝试了所有其他教程,它们都不适合我,感谢任何帮助,

【问题讨论】:

    标签: php magento checkout


    【解决方案1】:

    我最近处理类似类型的需求。 所以请按照我的指示:-

    我要求你不要关注答案的长度,只关注结果

    第 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 步:- 现在打开位于 app\code\core\Mage\Checkout\controllers\ 中的 OnepageController.php 并找到 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) 中,我们需要告诉用户在通过 驱动程序提示 后将重定向到哪里> step .so 在 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 步:- 现在我们需要在以下位置创建一个 excellence2.phtml 文件 \app\design\frontend\default\你的模板\template\checkout\onepage\

    此文件显示您要显示给用户的内容

    <form id="co-excellence2-form" action="">
    <div class="wide"> <label for="excellence2:like" class="required"><em  style="color:#F00;">*</em>&nbsp;&nbsp;&nbsp;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);"/>&nbsp;&nbsp;&nbsp;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);" />&nbsp;&nbsp;&nbsp;150$
    </li>
    <li>
    <input type="radio" name="excellence2[like]" id="excellence2:like" value="250" class="radio"  onclick="savevalue(this.value);"/>&nbsp;&nbsp;&nbsp;250$
    </li>
    <li>
    <input type="radio" name="excellence2[like]" id="excellence2:like" value="400" class="radio" onclick="savevalue(this.value);" />&nbsp;&nbsp;&nbsp;400$
    </li>
    <li>
    <input type="radio" name="excellence2[like]" id="excellence2:like" value="500" class="radio"  onclick="savevalue(this.value);"/>&nbsp;&nbsp;&nbsp;500$
    </li>
    <li>
    <input type="radio" name="excellence2[like]" id="excellence2:like" value="15% of total amount" class="radio" onclick="savevalue(this.value);" />&nbsp;&nbsp;&nbsp;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 步:-现在我们需要在以下位置创建一个 excellencecheckout.js 文件 \skin\frontend\default\你的模板\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 步:- 现在我们需要创建一个新函数来保存选定用户的数据。 所以我们在 Onepage.php 中的 saveShipping($data, $customerAddressId) 函数之后创建了一个名为 saveExcellence2() 的新函数,该函数位于 \app\code\core\Mage\Checkout\型号\类型\

    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();
    }
    

    【讨论】:

    • 你知道这是否适用于 1.7.0.2 吗?我遵循了所有步骤,但最终无法通过第一个结帐步骤。
    【解决方案2】:

    默认情况下,magento 会提供一些结帐步骤。但有时您需要添加来自客户的额外信息以供将来参考。一个常见的请求自定义是在默认结帐过程中添加自定义表单。 这不是接触核心文件的好习惯。您可以通过覆盖模块来做到这一点。 在此示例中,公司名称为 Ipragmatech,模块名称为 Checkoutstep

    Step1:在结账过程中添加自定义步骤

    打开 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;
       }
    }
    

    Step3:抓取自定义表单的提交值,设置自定义表单的值

    打开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));
        }
    }
    

    Step4:保存自定义表单信息

    当 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 – 在结帐扩展中添加自定义表单是一个完整的解决方案,可在您的电子商务网站的结帐流程中添加额外步骤。它允许管理员以 CSV 格式从自定义表中导出数据。 访问链接以获取此免费扩展程序http://www.magentocommerce.com/magento-connect/custom-form-in-checkout.html

    【讨论】:

    • 很好的回复!你知道这是否适用于 1.7.0.2 吗?我按照所有说明进行操作,但它不起作用。最后,我无法通过第一个结帐步骤。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多