2017-10-06 74 views
0

我嘗試實現一鍵結帳Magento 1.x,但如果我使用我的結帳,他們有買後的問題,沒有選擇運輸方式。Magento一次點擊結帳

這是我的Checkout控制器的代碼。

<?php 


    class MCP_Debitapi_CheckoutController extends Mage_Core_Controller_Front_Action 
    { 
     public function oneClickAction() 
     { 

      /** 
      * @var Mage_Catalog_Model_Product $product 
      */ 
      $options = []; 

      $product = Mage::getModel('catalog/product')->load((int)$_POST['product']); 
      /** 
      * create a cart to work with 
      * @var Mage_Checkout_Model_Cart $cart 
      */ 
      $cart = Mage::getModel('checkout/cart'); 
      $cart->addProduct($product,$options); 
      $cart->save(); 
      /** 
      * @var Mage_Checkout_Model_Type_Onepage $checkout 
      */ 
      $checkout = Mage::getSingleton('checkout/type_onepage'); 
      $checkout->getQuote()->clearInstance(); 
      $checkout->initCheckout(); 
      /** 
      * @var Mage_Customer_Helper_Data $customerHelper 
      */ 
      $customerHelper = Mage::helper('customer'); 
      $billingAddress = $customerHelper->getCustomer()->getDefaultBillingAddress(); 
      $shippingAddress = $customerHelper->getCustomer()->getDefaultShippingAddress(); 
      if(!$billingAddress) { 
       Mage::throwException('cannot create one-click order without default billing address'); 
       return false; 
      } 

      $checkout->getQuote()->getBillingAddress()->importCustomerAddress($billingAddress)->setSaveInAddressBook(false); 
      $checkout->getQuote()->getShippingAddress()->importCustomerAddress($shippingAddress)->setSaveInAddressBook(false); 
      $checkout->getQuote()->getShippingAddress()->setShippingDescription('Fixed')->save(); 
      $checkout->getQuote()->getShippingAddress()->setCollectShippingRates(true); 
      $checkout->saveShippingMethod('flatrate_flatrate'); 
      $checkout->savePayment(array('method'=>'mcpdebitapi')); 
      $checkout->getQuote()->collectTotals(); 
      $checkout->saveOrder(); 
      $cart->truncate(); 
      $cart->save(); 
      $cart->getItems()->clear()->save(); 
      $checkout->getQuote()->setIsActive(false)->save(); 
      $this->_redirect('checkout/onepage/success',array('_secure'=>true)); 
     } 

    } 

如何找出並設置正確的運輸方式?

回答

0

它看起來像你試圖直接添加送貨方式到結帳對象而不是地址。

試用:代替

$checkout->getQuote()->getShippingAddress()->setShippingMethod('flatrate_flatrate'); 
$checkout->saveShippingMethod('flatrate_flatrate'); 

$checkout->saveShippingMethod('flatrate_flatrate'); 

希望它可以幫助你:)

+0

嗨,我tryed它,但得到了同樣的結果。爲了更好的理解,我添加了Exception: –

0
Please specify a shipping method. 

    Trace: 
    #0 /srv/www/shops/magento-1.9.3.4/app/code/core/Mage/Sales/Model/Service/Quote.php(303): Mage::throwException('Please specify ...') 
    #1 /srv/www/shops/magento-1.9.3.4/app/code/core/Mage/Sales/Model/Service/Quote.php(222): Mage_Sales_Model_Service_Quote->_validate() 
    #2 /srv/www/shops/magento-1.9.3.4/app/code/core/Mage/Sales/Model/Service/Quote.php(238): Mage_Sales_Model_Service_Quote->submitNominalItems() 
    #3 /srv/www/shops/magento-1.9.3.4/app/code/core/Mage/Checkout/Model/Type/Onepage.php(815): Mage_Sales_Model_Service_Quote->submitAll() 
    #4 /srv/www/shops/magento-1.9.3.4/app/code/local/MCP/Debitapi/controllers/CheckoutController.php(47): Mage_Checkout_Model_Type_Onepage->saveOrder() 
    #5 /srv/www/shops/magento-1.9.3.4/app/code/core/Mage/Core/Controller/Varien/Action.php(418): MCP_Debitapi_CheckoutController->oneClickAction() 
    #6 /srv/www/shops/magento-1.9.3.4/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('oneClick') 
    #7 /srv/www/shops/magento-1.9.3.4/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http)) 
    #8 /srv/www/shops/magento-1.9.3.4/app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front->dispatch() 
    #9 /srv/www/shops/magento-1.9.3.4/app/Mage.php(683): Mage_Core_Model_App->run(Array) 
    #10 /srv/www/shops/magento-1.9.3.4/index.php(83): Mage::run('', 'store') 
    #11 {main}