Advertisement
Guest User

Latheesan - magento order creator

a guest
Feb 10th, 2016
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.98 KB | None | 0 0
  1. <?php
  2.  
  3. // Init magento base class
  4. require_once 'app/Mage.php';
  5. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  6.  
  7. // Dummy data source
  8. $settings   = array
  9. (
  10.     'WebsiteId' => 2,
  11.     'StoreId' => 4,
  12.     'ShippingMethod' => 'icw_shipping',
  13.     'PaymentMethod' => 'icw_payment'
  14. );
  15. $jsonOrderData = '{"DiscountAmount":0, "DiscountReason":"", "STPointsUsed": 0,
  16. "STPointsEarned": 0,"CustomerId":17557,"SendConfirmation":true,"BasketData":
  17. [{"ProductId":18844,"Price":8.88,"Qty":1}],"ShippingMethod":"Flat Rate",
  18. "DeliveryDate":"21/01/2016","SalesRep":"latheesan","PaymentMethod":"PayPal Here",
  19. "PPHPaymentType":"CreditCard","PPHInvoiceId":"INV2-AHWG-SQHP-QMLT-1234",
  20. "PPHTxId":"111-22-3333","ShippingAmount":15}';
  21.  
  22. //
  23. // Test
  24. //
  25. echo '<pre>';
  26. try
  27. {
  28.     // Init
  29.     $orderCreator = new Ordercreator(
  30.         $settings,
  31.         $jsonOrderData
  32.     );
  33.  
  34.     // Create order
  35.     print_r($orderCreator->create());
  36.  
  37.     // Clean-up
  38.     $orderCreator = null;
  39. }
  40. catch (Exception $ex)
  41. {
  42.     echo 'Failed to create order: '. $ex->getMessage()
  43.         .' @ '.
  44.         basename($ex->getFile()) .':'. $ex->getLine();
  45. }
  46. echo '</pre>';
  47. //
  48. // Test
  49. //
  50.  
  51. /**
  52.  * Helper class
  53.  */
  54. class Ordercreator
  55. {
  56.     // Class properties
  57.     private $_settings        = array();
  58.     private $_orderData       = array();
  59.     private $_customer        = null;
  60.     private $_quote           = null;
  61.     private $_salesOrder      = null;
  62.  
  63.     // Class constructor
  64.     function __construct(array $settings, string $jsonOrderData)
  65.     {
  66.         Mage::register('isSecureArea', true);
  67.         $this->_settings = $settings;
  68.         $this->_orderData = $jsonOrderData;
  69.     }
  70.  
  71.     // Class Destructor
  72.     function __destruct()
  73.     {
  74.         // Clean-up
  75.         $this->_settings   = null;
  76.         $this->_orderData  = null;
  77.         $this->_customer   = null;
  78.         $this->_quote      = null;
  79.         $this->_salesOrder = null;
  80.     }
  81.  
  82.     // Main method to initialise the order creation process
  83.     function create()
  84.     {
  85.         // Parepare new sales order quote
  86.         $this->parseSalesOrderData(
  87.             $this->_orderData
  88.         );
  89.         $this->loadSalesOrderCustomer();
  90.         $this->initSalesOrderQuote();
  91.         $this->addOrderLinesToQuote();
  92.         $this->saveSalesOrderQuote();
  93.  
  94.         // Convert quote to new sales order
  95.         $this->convertQuoteToOrder();
  96.         $this->updateSalesOrderAttributes();
  97.  
  98.         // Finished
  99.         return $this->_salesOrder->getIncrementId();
  100.     }
  101.  
  102.     // Method to parse sales order data
  103.     private function parseSalesOrderData($jsonOrderData)
  104.     {
  105.         // Parse & validate order data object
  106.         $this->_orderData = @json_decode(
  107.             $jsonOrderData
  108.         );
  109.         if (!$this->_orderData)
  110.             throw new Exception('Invalid order data');
  111.     }
  112.  
  113.     // Method to load order customer
  114.     private function loadSalesOrderCustomer()
  115.     {
  116.         $this->_customer = Mage::getModel('customer/customer');
  117.         $this->_customer->setWebsiteId(
  118.             $this->_settings['WebsiteId']
  119.         );
  120.         $this->_customer->load(
  121.             $this->_orderData->CustomerId
  122.         );
  123.         if (!$this->_customer->getId())
  124.             throw new Exception(
  125.                 'Invalid customer id '. $this->_orderData->CustomerId
  126.             );
  127.     }
  128.  
  129.     // Method to initialise a new sales order quote
  130.     private function initSalesOrderQuote()
  131.     {
  132.         // Validate customer's default address data
  133.         if (!$this->_customer->getPrimaryBillingAddress())
  134.             throw new Exception('Default customer billing address missing');
  135.         if (!$this->_customer->getPrimaryShippingAddress())
  136.             throw new Exception('Default customer shipping address missing');
  137.  
  138.         // Create a new quote
  139.         $this->_quote = Mage::getModel('sales/quote');
  140.         $this->_quote->assignCustomer(
  141.             $this->_customer
  142.         );
  143.         $this->_quote->setStore(
  144.             $this->_quote->getStore()->load(
  145.                 $this->_settings['StoreId']
  146.             )
  147.         );
  148.     }
  149.  
  150.     // Method to add order lines to sales order quote
  151.     private function addOrderLinesToQuote()
  152.     {
  153.         // Validate basket data
  154.         if (!sizeof($this->_orderData->BasketData))
  155.             throw new Exception('Basket data is empty');
  156.  
  157.         // Add basket data to sales order quote
  158.         $product = Mage::getModel('catalog/product');
  159.         foreach ($this->_orderData->BasketData as $orderLine)
  160.         {
  161.             // Load product
  162.             $quoteProduct = $product->load(
  163.                 $orderLine->ProductId
  164.             );
  165.             if (!$quoteProduct || !$quoteProduct->getId())
  166.                 throw new Exception(
  167.                     'Product #'. $orderLine->ProductId .' does not exists'
  168.                 );
  169.  
  170.             // Add sales order quote item
  171.             $quoteItem = Mage::getModel('sales/quote_item')->setProduct(
  172.                 $quoteProduct
  173.             );
  174.             $quoteItem->setQuote(
  175.                 $this->_quote
  176.             );
  177.             $quoteItem->setQty(
  178.                 $orderLine->Qty
  179.             );
  180.             $quoteItem->setPrice(
  181.                 $orderLine->Price
  182.             );
  183.             $this->_quote->addItem(
  184.                 $quoteItem
  185.             );
  186.  
  187.             // Clean-up
  188.             $quoteProduct = null;
  189.             $quoteItem    = null;
  190.         }
  191.  
  192.         // Clean-up
  193.         $product = null;
  194.     }
  195.  
  196.     // Method to save sales order quote
  197.     private function saveSalesOrderQuote()
  198.     {
  199.         Mage::register($this->_settings['ShippingMethod'], array(
  200.             'shippingCarrier' => 'Custom',
  201.             'shippingTitle'   => $this->_orderData->ShippingMethod,
  202.             'shippingPrice'   => $this->_orderData->ShippingAmount
  203.         ));
  204.         $this->_quote->getShippingAddress()->setShippingMethod(
  205.             $this->_settings['ShippingMethod']
  206.         );
  207.         $this->_quote->getShippingAddress()->setCollectShippingRates(
  208.             true
  209.         );
  210.         $this->_quote->getShippingAddress()->collectShippingRates();
  211.         $this->_quote->collectTotals();
  212.         $this->_quote->reserveOrderId();
  213.         $this->_quote->save();
  214.     }
  215.  
  216.     // Method to convert quote into real order & return order data
  217.     private function convertQuoteToOrder()
  218.     {
  219.         // Set payment method
  220.         $quotePaymentObj = $this->_quote->getPayment();
  221.         $quotePaymentObj->setMethod(
  222.             $this->_settings['PaymentMethod']
  223.         );
  224.         $quotePaymentObj->setAdditionalData(
  225.             'Custom - '. $this->_orderData->PaymentMethod
  226.         );
  227.         $this->_quote->setPayment(
  228.             $quotePaymentObj
  229.         );
  230.  
  231.         // Convert quote to order
  232.         $convertQuoteObj = Mage::getSingleton('sales/convert_quote');
  233.         $this->_salesOrder = $convertQuoteObj->addressToOrder(
  234.             $this->_quote->getShippingAddress()
  235.         );
  236.         $orderPaymentObj = $convertQuoteObj->paymentToOrderPayment(
  237.             $quotePaymentObj
  238.         );
  239.         $this->_salesOrder->setBillingAddress(
  240.             $convertQuoteObj->addressToOrderAddress(
  241.                 $this->_quote->getBillingAddress()
  242.             )
  243.         );
  244.  
  245.         // Set shipping info
  246.         $this->_salesOrder
  247.             ->setShippingAddress(
  248.                 $convertQuoteObj->addressToOrderAddress(
  249.                     $this->_quote->getShippingAddress()
  250.                 )
  251.             );
  252.  
  253.         // Set payment info
  254.         $this->_salesOrder->setPayment(
  255.             $convertQuoteObj->paymentToOrderPayment(
  256.                 $this->_quote->getPayment()
  257.             )
  258.         );
  259.  
  260.         // Add items from quote or order
  261.         foreach ($this->_quote->getAllItems() as $item) {
  262.             $orderItem = $convertQuoteObj->itemToOrderItem(
  263.                 $item
  264.             );
  265.             if ($item->getParentItem()) {
  266.                 $orderItem->setParentItem(
  267.                     $this->_salesOrder->getItemByQuoteItemId(
  268.                         $item->getParentItem()->getId()
  269.                     )
  270.                 );
  271.             }
  272.             $this->_salesOrder->addItem(
  273.                 $orderItem
  274.             );
  275.             $orderItem = null;
  276.         }
  277.         $this->_salesOrder->setCanShipPartiallyItem(
  278.             true
  279.         );
  280.  
  281.         // Save order
  282.         $this->_salesOrder->place();
  283.         $this->_salesOrder->save();
  284.  
  285.         // Clean-up
  286.         $quotePaymentObj = null;
  287.         $convertQuoteObj = null;
  288.     }
  289.  
  290.     // Method to update sales order attributes
  291.     private function updateSalesOrderAttributes()
  292.     {
  293.         $this->_salesOrder
  294.             ->setSalesRep($this->_orderData->SalesRep)
  295.             ->setDeliveryDate($this->_orderData->DeliveryDate)
  296.             ->setPphPaymentType($this->_orderData->PPHPaymentType)
  297.             ->setPphInvoiceId($this->_orderData->PPHInvoiceId)
  298.             ->setPphTxId($this->_orderData->PPHTxId)
  299.             ->save();
  300.     }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement