久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Magento 中以編程方式創建訂單

Create order programmatically in Magento(在 Magento 中以編程方式創建訂單)
本文介紹了在 Magento 中以編程方式創建訂單的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用這兩種方法在 Magento 中以編程方式創建訂單.

I'm using these two methods to create orders programmatically in Magento.

第一個創建引用:

 public function prepareCustomerOrder($customerId, array $shoppingCart, array  $shippingAddress, array $billingAddress, 
        $shippingMethod, $couponCode = null) 
    {
        $customerObj = Mage::getModel('customer/customer')->load($customerId);
        $storeId = $customerObj->getStoreId();
        $quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);
        $storeObj = $quoteObj->getStore()->load($storeId);
        $quoteObj->setStore($storeObj);

        // add products to quote
        foreach($shoppingCart as $part) {
            $productModel = Mage::getModel('catalog/product');
            $productObj = $productModel->setStore($storeId)->setStoreId($storeId)->load($part['PartId']);

            $productObj->setSkipCheckRequiredOption(true);

            try{
                $quoteItem = $quoteObj->addProduct($productObj);
                $quoteItem->setPrice(20);
                $quoteItem->setQty(3);
                $quoteItem->setQuote($quoteObj);                                    
                $quoteObj->addItem($quoteItem);

            } catch (exception $e) {
            return false;
            }

            $productObj->unsSkipCheckRequiredOption();
            $quoteItem->checkData();
        }

        // addresses
        $quoteShippingAddress = new Mage_Sales_Model_Quote_Address();
        $quoteShippingAddress->setData($shippingAddress);
        $quoteBillingAddress = new Mage_Sales_Model_Quote_Address();
        $quoteBillingAddress->setData($billingAddress);
        $quoteObj->setShippingAddress($quoteShippingAddress);
        $quoteObj->setBillingAddress($quoteBillingAddress);

        // coupon code
        if(!empty($couponCode)) $quoteObj->setCouponCode($couponCode); 


        // shipping method an collect
        $quoteObj->getShippingAddress()->setShippingMethod($shippingMethod);
        $quoteObj->getShippingAddress()->setCollectShippingRates(true);
        $quoteObj->getShippingAddress()->collectShippingRates();
        $quoteObj->collectTotals(); // calls $address->collectTotals();
        $quoteObj->setIsActive(0);
        $quoteObj->save();

        return $quoteObj->getId();

    }

第二個使用該報價創建訂單:

And the second one uses that Quote to create Order:

 public function createOrder($quoteId, $paymentMethod, $paymentData) 
    {
        $quoteObj = Mage::getModel('sales/quote')->load($quoteId); // Mage_Sales_Model_Quote
        $items = $quoteObj->getAllItems();                  

        $quoteObj->reserveOrderId();

          // set payment method 
        $quotePaymentObj = $quoteObj->getPayment(); // Mage_Sales_Model_Quote_Payment
        $quotePaymentObj->setMethod($paymentMethod);
        $quoteObj->setPayment($quotePaymentObj);

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

        // convert quote addresses
        $orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress()));
        $orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()));

        // set payment options
        $orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));
        if ($paymentData) {
        $orderObj->getPayment()->setCcNumber($paymentData->ccNumber);
        $orderObj->getPayment()->setCcType($paymentData->ccType);
        $orderObj->getPayment()->setCcExpMonth($paymentData->ccExpMonth);
        $orderObj->getPayment()->setCcExpYear($paymentData->ccExpYear);
        $orderObj->getPayment()->setCcLast4(substr($paymentData->ccNumber,-4));
        }
        // convert quote items
        foreach ($items as $item) {
            // @var $item Mage_Sales_Model_Quote_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);
        }

        $orderObj->setCanShipPartiallyItem(false);

        try {
            $orderObj->place();
        } catch (Exception $e){     
            Mage::log($e->getMessage());
            Mage::log($e->getTraceAsString());
        }

        $orderObj->save(); 
        //$orderObj->sendNewOrderEmail(); 
        return $orderObj->getId();

    }

流程運行正常,沒有錯誤,訂單創建完成.但是總數是0,不管我放什么里面都沒有產品.

The process works fine, no errors, and the order is created. But the total is 0 and there are no products in it no matter what I put.

我已經對其進行了跟蹤,并且可以確認這些行已添加到 sales_flat_quotesales_flat_quote_item 表中,所以沒問題.但是當運行 createOrder 并調用

I've traced it and I can confirm that the rows are added to the sales_flat_quote and sales_flat_quote_item tables, so that is ok. But when running the createOrder and calling

     $items = $quoteObj->getAllItems();

總是返回一個空數組,我不知道為什么.我的店里有可配置且簡單的產品.當我添加簡單時會發生這種情況,當我添加可配置時,錯誤顯示為方法

an empty array is always returned, and I have no idea why. I have configurable and simple products in my shop. This happens when I add simple, when I add configurable the error appears as the method

    $quoteItem = $quoteObj->addProduct($productObj);

返回空值.

推薦答案

在我看來,你沒有加載產品集合,因此,購物車總是返回空的.試試這個鏈接,它會給你更明確的幫助.以編程方式創建訂單

It seems to me, you didn't load product collection, therefore, the cart always return empty. Try this link, it will give you more clear help. Create order programmatically

// this is get only one product, you can refactor the code
$this->_product = Mage::getModel('catalog/product')->getCollection()
  ->addAttributeToFilter('sku', 'Some value here...')
  ->addAttributeToSelect('*')
  ->getFirstItem();

// load product data
$this->_product->load($this->_product->getId());

這篇關于在 Magento 中以編程方式創建訂單的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 久久久久国产精品一区二区 | 国产成人麻豆免费观看 | 一级毛片中国 | 蜜桃臀av一区二区三区 | 91porn在线 | 中文字幕电影在线观看 | 美女视频三区 | 亚洲国产精品一区二区三区 | 国产精品久久久久aaaa | 一级黄色片日本 | 成人在线免费 | 久久久成人动漫 | 精品福利在线 | 中文字幕在线看第二 | 91九色视频在线 | 成人福利片 | 亚洲欧美视频 | 欧美1级 | 久久久久久国产精品免费 | 激情婷婷 | 波多野吉衣久久 | a级在线免费视频 | 欧美日韩综合一区 | 精品久久香蕉国产线看观看亚洲 | 国产天堂| 狠狠操av| 蜜桃传媒一区二区 | 久久精品久久综合 | 在线观看国产三级 | 中文字幕在线观看成人 | 日韩精品一区二区三区在线观看 | av黄色免费 | 乳色吐息在线观看 | 国产日韩欧美中文字幕 | 午夜精品影院 | 久久九九免费 | 久久久激情 | 欧美一级片黄色 | 成人性视频免费网站 | 成人字幕网zmw | 中文字幕在线观看 |