skip to Main Content

i try to implement a one-click checkout for Magento-1.x, but if i use my Checkout, they have after a buy the problem that the shipping method is not selected.

Here is the Code from my Checkout Controller.

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

    }

How i can find out and set the correct shipping method?

2

Answers


  1. Chosen as BEST ANSWER
        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}
    

  2. It looks like you are trying to add shipping method directly to checkout object instead of address.

    Try out:

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

    instead of:

    $checkout->saveShippingMethod('flatrate_flatrate');
    

    Hope it helps you:)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search