Magento根据购物车中的所有商品提交订单

时间:2016-06-28 12:09:21

标签: php magento invoice

我已经尝试过我能找到的所有东西,没有任何东西能为我创造订单。我基本上想要将购物车报价转移到订单,有点像自定义结账,继承我的班级:

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
    class MyCompany_Finance_IndexController extends Mage_Core_Controller_Front_Action{
        public function indexAction(){

            $conn = Mage::getSingleton('core/resource')->getConnection('core_read');

            $conn->query("CREATE TABLE IF NOT EXISTS `v12_finance_sales`
            (
                `sales_id` INT(11) NOT NULL AUTO_INCREMENT, 
                `sales_reference` VARCHAR(50) NOT NULL UNIQUE, 
                `customer_id` INT(11) NOT NULL , 
                `products` TEXT NOT NULL , 
                `total_price` VARCHAR(255) NOT NULL , 
                `status` VARCHAR(4) NOT NULL , 
                PRIMARY KEY(`sales_id`)
            )");



            $this->loadLayout();

             $block = $this->getLayout()->createBlock(
                                                'Mage_Core_Block_Template',
                                                'v12content'
                                                )
                                   ->setTemplate('v12_finance/index.phtml');
            $this->getLayout()->getBlock('content')->append($block);

            $this->renderLayout();

        }
        private function _getCustomerSession() {
            Mage::getSingleton('core/session', array('name' => 'frontend'));
            return Mage::getSingleton('customer/session');
        }
        function successAction()
        {

            $reference = $_GET['REF'];
            $sales_reference = $_GET['SR'];
            $status = $_GET['Status'];
            $auth = $_GET['Auth'];

            $conn = Mage::getSingleton('core/resource')->getConnection('core/read');

            $confirmed = 0;

            if($status == 'A' || $status == 'S')
            {
                $confirmed = 1;
            }
            try{

                $conn->query("UPDATE `v12_finance_sales` SET `confirmed` = '$confirmed' , `status` = '$status' WHERE `sales_reference` = '$sales_reference'");

                $this->loadLayout();

                $this->renderLayout();

            }catch(Exception $e)
            {
                $fh = fopen("FINANCE_LOG.log" , "a+");
                fwrite($fh , "ERROR[" . date('H:i:s d-m-Y') . ']:EXCEPTION:' . $e->getMessage());
                fclose($fh);

                $conn->query("CREATE TABLE IF NOT EXISTS `v12_finance_errors`
                (
                    `error_id` int(11) not null AUTO_INCREMENT, 
                    `sales_reference` VARCHAR(200) NOT NULL , 
                    `status` VARCHAR(4) NOT NULL , 
                    PRIMARY KEY(`error_id`)
                )");

                $conn->query("INSERT INTO `v12_finance_errors`
                    (
                        `sales_reference` , 
                        `status`
                    ) VALUES (
                        '" . $sales_reference . "' , 
                        '$status'
                    )");

                die("There was an error processing this request, please contact <a href='mailto:support@jejamescycles.co.uk'>Support</a> with this URL");
            }

        }
        function errorAction()
        {
            $message = $_GET['error'];

            $this->loadLayout();

             $block = $this->getLayout()->createBlock(
                                                    'Mage_Core_Block_Template',
                                                    'v12content'
                                                    )
                                       ->setTemplate('v12_finance/finance_error.phtml');
            $this->getLayout()->getBlock('content')->append($block);

            $this->renderLayout();
        }
        function basic_infoAction()
        {


            $conn = Mage::getSingleton('core/resource')->getConnection('core/read');

            if(!Mage::getSingleton('customer/session')->isLoggedIn()){
               // not logged in

                header("Location: http:" . $_SERVER['SERVER_NAME'] . "/customer/account");

            }else{

                $customer = Mage::getModel('customer/customer')
                ->setWebsiteId(Mage::app()->getWebsite()->getId())
                ->loadByEmail(Mage::getSingleton('customer/session')->getCustomer()->getEmail());

                $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId());
                $quote->assignCustomer($customer);
                $quote->setSendConfirmation(1);

                $items = Mage::getModel('checkout/cart')->getQuote();

                foreach($items as $item)
                {
                    $quote->addProduct(Mage::getModel('catalog/product')->load($item->getId()) , 1);
                }

                // Collect Rates and Set Shipping & Payment Method
                $shippingAddress = $quote->getShippingAddress();
                $shippingAddress->setCollectShippingRates(true)
                        ->collectShippingRates()
                        ->setShippingMethod('flatrate_flatrate');


                // Set Sales Order Payment

                $quote_id = $this->createorder(array(
                    'quoteId' => $quote->getId() , 
                    'paymentMethod' => 'banktransfer' , 
                    'paymentData' => array()
                ) , $quote);

                echo '<pre>';
                var_dump($quote_id);
                var_dump($quote->getId());
                die();
                 //$this->loadLayout();

                 //$block = $this->getLayout()->createBlock(
                                                 //   'Mage_Core_Block_Template',
                                                  //  'v12content'
                                        //            )
                                      // ->setTemplate('v12_finance/finance_setup.phtml');
                //$this->getLayout()->getBlock('content')->append($block);

               // $this->renderLayout();

            }

        }
        public function createorder(array $orderdata , $quoteObj)
         {
           $quoteId = $orderdata['quoteId'];
           $paymentMethod = $orderdata['paymentMethod'];
           $paymentData = $orderdata['paymentData'];       
           $items = $quoteObj->getAllItems();        
           $quoteObj->reserveOrderId();

            $quotePaymentObj = $quoteObj->getPayment(); 
            $quotePaymentObj->setMethod($paymentMethod);
            $quoteObj->setPayment($quotePaymentObj); 

            $convertQuoteObj = Mage::getSingleton('sales/convert_quote');
            $orderObj = $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress());
            $orderPaymentObj = $convertQuoteObj->paymentToOrderPayment($quotePaymentObj);

            $orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress()));
            $orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()));
            $orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));

            foreach ($items as $item) 
            {
                $orderItem = $convertQuoteObj->itemToOrderItem($item);        
                $options = array();
               if ($productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) 
               {
                 $options = $productOptions;
               }
               if ($addOptions = $item->getOptionByCode('additional_options')) 
               {
                $options['additional_options'] = unserialize($addOptions->getValue());
               }
               if ($options) 
               {
                  $orderItem->setProductOptions($options);
               }
               if ($item->getParentItem())
               {
                    $orderItem->setParentItem($orderObj->getItemByQuoteItemId($item->getParentItem()->getId()));
               }
               $orderObj->addItem($orderItem);
            }
             $quoteObj->collectTotals();
             $service = Mage::getModel('sales/service_quote', $quoteObj);
             if(is_string($service->submitAll()))
             {
                echo $service->submitAll();
             }
            try 
            {

                 $last_order_increment_id = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
                 return $last_order_increment_id;            
            } 
            catch (Exception $e)
            {     
                Mage::log($e->getMessage());
                Mage::log($e->getTraceAsString());
                return "Exception:".$e;
            }

        }

}

每次我提交订单时,它都不会提交,只返回最后插入的订单,而不是来自此脚本。有人提供任何帮助,我把头发拉出来,

0 个答案:

没有答案