2013-04-24 31 views
4

目前我有(第3步)將一個自定義驅動尖端步驟在Magento onepage結帳過程,運輸方法之後的需求,其中,我想用戶選擇從一些給定的選項尖端(ⅰ將使無線電按鈕),這將包含一定的金額,假設用戶選擇了$ 150,那麼這筆金額將被添加到總付款中?我嘗試了所有其他教程在谷歌,他們沒有爲我工作,任何幫助表示讚賞,如何添加在Magento onepage結賬過程中司機小費?

回答

3

我最近在相似類型的要求工作。 所以按照我的指示: -

我請求你,不注重答案的長度只注重結果

步驟1: - 如果你想放之間的司機小費運輸和運輸方法然後首先打開

\ app \ code \ core \ Mage \ Checkout \ Block \ Onepage.php 有一個$ stepCodes(行號: - 44)數組。 替換本

$stepCodes = array('billing', 'shipping', 'excellence2','shipping_method', 'payment', 'review'); 

我使用excellence2你也可以使用這個名稱。

第二步: - 現在我們需要創建應用程序\代碼\核心Excellence2類\法師\結帳\塊\ Onepage \ 所以創建一個新的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()函數的標籤,以便改變'提示ammount的'司機小費

步驟3: - 現在打開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),我們需要告訴用戶哪裏會通不過司機小費步驟之後重定向 - 同樣。所以創建一個saveExcellence2Action()剛過saveShippingAction()函數

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: - 現在我們在位於

\程序\設計\前臺\ DEFAULT \ checkout.xml更改模板\佈局打開它,找到

<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文件\程序\設計\前臺\ DEFAULT \你的模板\模板\結賬\ 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在 文件\皮膚\前端\ 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: - 現在我們需要創建一個用於保存用戶被選中的數據的新功能。 因此,我們正在創建一個名爲saveExcellence2()的新功能,位於\ app \ code \ core \ Mage \ Checkout \ Model \ Type \中的Onepage.php中的saveShipping($ data,$ customerAddressId)

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

你知道,如果這個工程在1.7.0.2?我遵循了所有步驟,但最終無法通過第一個結帳步驟。 – 2016-02-27 18:38:16

0

默認情況下,magento提供了一些結帳步驟。但有時您需要添加客戶的額外信息以備將來參考。常見的請求定製是在默認結帳過程中添加自定義表單。 這不是觸摸核心文件的好習慣。你可以通過覆蓋模塊來做到這一點。 在此示例Comapnyname是Ipragmatech和模塊名稱是Checkoutstep

步驟1:添加自定義步驟在結賬過程

打開Ipragmatech> Checkoutstep>塊> 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>控制器> 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)); 
    } 
} 

第四步:保存自定義表單信息

當checkout_onepage_controller_success_action 事件掛接被調用。打開Ipragmatech> Checkoutstep>模式> 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

+0

很棒的回覆!你知道這是否適用於1.7.0.2?我遵循了所有說明,但不起作用。最後,我無法超越第一個結賬步驟。 – 2015-11-17 19:47:05

相關問題