Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Init magento base class
- require_once 'app/Mage.php';
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- // Dummy data source
- $settings = array
- (
- 'WebsiteId' => 2,
- 'StoreId' => 4,
- 'ShippingMethod' => 'icw_shipping',
- 'PaymentMethod' => 'icw_payment'
- );
- $jsonOrderData = '{"DiscountAmount":0, "DiscountReason":"", "STPointsUsed": 0,
- "STPointsEarned": 0,"CustomerId":17557,"SendConfirmation":true,"BasketData":
- [{"ProductId":18844,"Price":8.88,"Qty":1}],"ShippingMethod":"Flat Rate",
- "DeliveryDate":"21/01/2016","SalesRep":"latheesan","PaymentMethod":"PayPal Here",
- "PPHPaymentType":"CreditCard","PPHInvoiceId":"INV2-AHWG-SQHP-QMLT-1234",
- "PPHTxId":"111-22-3333","ShippingAmount":15}';
- //
- // Test
- //
- echo '<pre>';
- try
- {
- // Init
- $orderCreator = new Ordercreator(
- $settings,
- $jsonOrderData
- );
- // Create order
- print_r($orderCreator->create());
- // Clean-up
- $orderCreator = null;
- }
- catch (Exception $ex)
- {
- echo 'Failed to create order: '. $ex->getMessage()
- .' @ '.
- basename($ex->getFile()) .':'. $ex->getLine();
- }
- echo '</pre>';
- //
- // Test
- //
- /**
- * Helper class
- */
- class Ordercreator
- {
- // Class properties
- private $_settings = array();
- private $_orderData = array();
- private $_customer = null;
- private $_quote = null;
- private $_salesOrder = null;
- // Class constructor
- function __construct(array $settings, string $jsonOrderData)
- {
- Mage::register('isSecureArea', true);
- $this->_settings = $settings;
- $this->_orderData = $jsonOrderData;
- }
- // Class Destructor
- function __destruct()
- {
- // Clean-up
- $this->_settings = null;
- $this->_orderData = null;
- $this->_customer = null;
- $this->_quote = null;
- $this->_salesOrder = null;
- }
- // Main method to initialise the order creation process
- function create()
- {
- // Parepare new sales order quote
- $this->parseSalesOrderData(
- $this->_orderData
- );
- $this->loadSalesOrderCustomer();
- $this->initSalesOrderQuote();
- $this->addOrderLinesToQuote();
- $this->saveSalesOrderQuote();
- // Convert quote to new sales order
- $this->convertQuoteToOrder();
- $this->updateSalesOrderAttributes();
- // Finished
- return $this->_salesOrder->getIncrementId();
- }
- // Method to parse sales order data
- private function parseSalesOrderData($jsonOrderData)
- {
- // Parse & validate order data object
- $this->_orderData = @json_decode(
- $jsonOrderData
- );
- if (!$this->_orderData)
- throw new Exception('Invalid order data');
- }
- // Method to load order customer
- private function loadSalesOrderCustomer()
- {
- $this->_customer = Mage::getModel('customer/customer');
- $this->_customer->setWebsiteId(
- $this->_settings['WebsiteId']
- );
- $this->_customer->load(
- $this->_orderData->CustomerId
- );
- if (!$this->_customer->getId())
- throw new Exception(
- 'Invalid customer id '. $this->_orderData->CustomerId
- );
- }
- // Method to initialise a new sales order quote
- private function initSalesOrderQuote()
- {
- // Validate customer's default address data
- if (!$this->_customer->getPrimaryBillingAddress())
- throw new Exception('Default customer billing address missing');
- if (!$this->_customer->getPrimaryShippingAddress())
- throw new Exception('Default customer shipping address missing');
- // Create a new quote
- $this->_quote = Mage::getModel('sales/quote');
- $this->_quote->assignCustomer(
- $this->_customer
- );
- $this->_quote->setStore(
- $this->_quote->getStore()->load(
- $this->_settings['StoreId']
- )
- );
- }
- // Method to add order lines to sales order quote
- private function addOrderLinesToQuote()
- {
- // Validate basket data
- if (!sizeof($this->_orderData->BasketData))
- throw new Exception('Basket data is empty');
- // Add basket data to sales order quote
- $product = Mage::getModel('catalog/product');
- foreach ($this->_orderData->BasketData as $orderLine)
- {
- // Load product
- $quoteProduct = $product->load(
- $orderLine->ProductId
- );
- if (!$quoteProduct || !$quoteProduct->getId())
- throw new Exception(
- 'Product #'. $orderLine->ProductId .' does not exists'
- );
- // Add sales order quote item
- $quoteItem = Mage::getModel('sales/quote_item')->setProduct(
- $quoteProduct
- );
- $quoteItem->setQuote(
- $this->_quote
- );
- $quoteItem->setQty(
- $orderLine->Qty
- );
- $quoteItem->setPrice(
- $orderLine->Price
- );
- $this->_quote->addItem(
- $quoteItem
- );
- // Clean-up
- $quoteProduct = null;
- $quoteItem = null;
- }
- // Clean-up
- $product = null;
- }
- // Method to save sales order quote
- private function saveSalesOrderQuote()
- {
- Mage::register($this->_settings['ShippingMethod'], array(
- 'shippingCarrier' => 'Custom',
- 'shippingTitle' => $this->_orderData->ShippingMethod,
- 'shippingPrice' => $this->_orderData->ShippingAmount
- ));
- $this->_quote->getShippingAddress()->setShippingMethod(
- $this->_settings['ShippingMethod']
- );
- $this->_quote->getShippingAddress()->setCollectShippingRates(
- true
- );
- $this->_quote->getShippingAddress()->collectShippingRates();
- $this->_quote->collectTotals();
- $this->_quote->reserveOrderId();
- $this->_quote->save();
- }
- // Method to convert quote into real order & return order data
- private function convertQuoteToOrder()
- {
- // Set payment method
- $quotePaymentObj = $this->_quote->getPayment();
- $quotePaymentObj->setMethod(
- $this->_settings['PaymentMethod']
- );
- $quotePaymentObj->setAdditionalData(
- 'Custom - '. $this->_orderData->PaymentMethod
- );
- $this->_quote->setPayment(
- $quotePaymentObj
- );
- // Convert quote to order
- $convertQuoteObj = Mage::getSingleton('sales/convert_quote');
- $this->_salesOrder = $convertQuoteObj->addressToOrder(
- $this->_quote->getShippingAddress()
- );
- $orderPaymentObj = $convertQuoteObj->paymentToOrderPayment(
- $quotePaymentObj
- );
- $this->_salesOrder->setBillingAddress(
- $convertQuoteObj->addressToOrderAddress(
- $this->_quote->getBillingAddress()
- )
- );
- // Set shipping info
- $this->_salesOrder
- ->setShippingAddress(
- $convertQuoteObj->addressToOrderAddress(
- $this->_quote->getShippingAddress()
- )
- );
- // Set payment info
- $this->_salesOrder->setPayment(
- $convertQuoteObj->paymentToOrderPayment(
- $this->_quote->getPayment()
- )
- );
- // Add items from quote or order
- foreach ($this->_quote->getAllItems() as $item) {
- $orderItem = $convertQuoteObj->itemToOrderItem(
- $item
- );
- if ($item->getParentItem()) {
- $orderItem->setParentItem(
- $this->_salesOrder->getItemByQuoteItemId(
- $item->getParentItem()->getId()
- )
- );
- }
- $this->_salesOrder->addItem(
- $orderItem
- );
- $orderItem = null;
- }
- $this->_salesOrder->setCanShipPartiallyItem(
- true
- );
- // Save order
- $this->_salesOrder->place();
- $this->_salesOrder->save();
- // Clean-up
- $quotePaymentObj = null;
- $convertQuoteObj = null;
- }
- // Method to update sales order attributes
- private function updateSalesOrderAttributes()
- {
- $this->_salesOrder
- ->setSalesRep($this->_orderData->SalesRep)
- ->setDeliveryDate($this->_orderData->DeliveryDate)
- ->setPphPaymentType($this->_orderData->PPHPaymentType)
- ->setPphInvoiceId($this->_orderData->PPHInvoiceId)
- ->setPphTxId($this->_orderData->PPHTxId)
- ->save();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement