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
It looks like you are trying to add shipping method directly to checkout object instead of address.
Try out:
instead of:
Hope it helps you:)