Guest User

Untitled

a guest
Oct 11th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class MCP_Debitapi_CheckoutController extends Mage_Core_Controller_Front_Action
  5. {
  6.     public function oneClickAction()
  7.     {
  8.         /**
  9.          * @var Mage_Catalog_Model_Product $product
  10.          */
  11.         $options = [];
  12.  
  13.         $product = Mage::getModel('catalog/product')->load((int)$_POST['product']);
  14.         /**
  15.          * create a cart to work with
  16.          * @var Mage_Checkout_Model_Cart $cart
  17.          */
  18.         $cart = Mage::getSingleton('checkout/cart');
  19.         $cart->addProduct($product,$options);
  20.         $cart->save();
  21.         $cart->getQuote()->collectTotals();
  22.         /**
  23.          * @var Mage_Checkout_Model_Type_Onepage $checkout
  24.          */
  25.         $checkout = Mage::getSingleton('checkout/type_onepage');
  26.         $checkout->getQuote()->clearInstance();
  27.         $checkout->initCheckout();
  28.         /**
  29.          * @var Mage_Customer_Helper_Data $customerHelper
  30.          */
  31.         $customerHelper = Mage::helper('customer');
  32.         $billingAddress = $customerHelper->getCustomer()->getDefaultBillingAddress();
  33.         $shippingAddress = $customerHelper->getCustomer()->getDefaultShippingAddress();
  34.         if(!$billingAddress) {
  35.             Mage::throwException('cannot create one-click order without default billing address');
  36.             return false;
  37.         }
  38.  
  39.         $lastOrder = $this->getLastOrderFromCustomer($customerHelper->getCustomer());
  40.  
  41.         $checkout->getQuote()->getBillingAddress()->importCustomerAddress($billingAddress)->setSaveInAddressBook(false)->save();
  42.         $checkout->getQuote()->getShippingAddress()->importCustomerAddress($shippingAddress)->setSaveInAddressBook(false)->save();
  43.         $checkout->saveShippingMethod($lastOrder->getShippingMethod());
  44.         $checkout->getQuote()->getShippingAddress()->setShippingDescription('Fixed')->save();
  45.         $checkout->getQuote()->getShippingAddress()->requestShippingRates();
  46.         $checkout->getQuote()->collectTotals();
  47.         $checkout->savePayment(array('method'=>'mcpdebitapi'));
  48.  
  49.         $checkout->saveOrder();
  50.         $cart->truncate();
  51.         $cart->save();
  52.         $cart->getItems()->clear()->save();
  53.         $checkout->getQuote()->setIsActive(false)->save();
  54.         $this->_redirect('checkout/onepage/success',array('_secure'=>true));
  55.     }
  56.  
  57.  
  58.     public function indexAction()
  59.     {
  60.         $this->loadLayout();
  61.         $this->renderLayout();
  62.     }
  63.  
  64.     public function getLastOrderFromCustomer(Mage_Customer_Model_Customer $customer)
  65.     {
  66.         /**
  67.          * @var Mage_Sales_Model_Resource_Order_Collection $orders
  68.          */
  69.         $orders = Mage::getModel('sales/order')->getCollection();
  70.         $orders->setOrder('increment_id','DESC')
  71.         ->setPageSize(1);
  72.         $orders->addFilter('customer_id',$customer->getId());
  73.         return $orders->getFirstItem();
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment