Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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::getSingleton('checkout/cart');
- $cart->addProduct($product,$options);
- $cart->save();
- $cart->getQuote()->collectTotals();
- /**
- * @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;
- }
- $lastOrder = $this->getLastOrderFromCustomer($customerHelper->getCustomer());
- $checkout->getQuote()->getBillingAddress()->importCustomerAddress($billingAddress)->setSaveInAddressBook(false)->save();
- $checkout->getQuote()->getShippingAddress()->importCustomerAddress($shippingAddress)->setSaveInAddressBook(false)->save();
- $checkout->saveShippingMethod($lastOrder->getShippingMethod());
- $checkout->getQuote()->getShippingAddress()->setShippingDescription('Fixed')->save();
- $checkout->getQuote()->getShippingAddress()->requestShippingRates();
- $checkout->getQuote()->collectTotals();
- $checkout->savePayment(array('method'=>'mcpdebitapi'));
- $checkout->saveOrder();
- $cart->truncate();
- $cart->save();
- $cart->getItems()->clear()->save();
- $checkout->getQuote()->setIsActive(false)->save();
- $this->_redirect('checkout/onepage/success',array('_secure'=>true));
- }
- public function indexAction()
- {
- $this->loadLayout();
- $this->renderLayout();
- }
- public function getLastOrderFromCustomer(Mage_Customer_Model_Customer $customer)
- {
- /**
- * @var Mage_Sales_Model_Resource_Order_Collection $orders
- */
- $orders = Mage::getModel('sales/order')->getCollection();
- $orders->setOrder('increment_id','DESC')
- ->setPageSize(1);
- $orders->addFilter('customer_id',$customer->getId());
- return $orders->getFirstItem();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment