Magento 1.使用'sales / service_quote'创建订单programmaticaly返回空订单

时间:2016-11-29 01:47:08

标签: php magento magento-1.9

我尝试使用以下方式创建报价单:

$service = Mage::getModel('sales/service_quote', $quote);

但订单不在数据库中创建(报价创建成功)。

$service->getOrder();

返回nullexception.logsystem.log为空, 我的代码:

    $quote = Mage::getModel('sales/quote')->setStoteId($store->getId());
    $quote->setCustomerEmail($customer->getEmail());
    $quote->setSendConfirmation(0);
    $quote->addProduct($product, new Varien_Object(['qty' => 1]));

    $addressData = array(
            'firstname' => $customer->getFirstname(),
            'lastname' => 'Not set',
            'street' => 'Not set',
            'city' => 'Not set',
            'postcode' => 'Not set',
            'telephone' => '-',
            'country_id' => 'IN',
            'region' => 'Not set',
    );

    $billingAddress = $quote->getBillingAddress()->addData($addressData);
    $shippingAddress = $quote->getShippingAddress()->addData($addressData);

    $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                    ->setShippingMethod('flatrate_flatrate')
                    ->setPaymentMethod('checkmo');

    $quote->getPayment()->importData(['method' => 'checkmo']);

    $quote->collectTotals()->save();

    try {
        $service = Mage::getModel('sales/service_quote', $quote);
        $service->submitAll();
        $order = $service->getOrder();
    } catch (Exception $ex) {
        echo $ex->getMessage();
    } catch (Mage_Core_Exception $e) {
        echo $e->getMessage();
    }

    $order->setStatus('complete');
    $order->save();

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码以编程方式创建订单,并且您必须通过checkmo作为付款方式,您还必须在此代码中更改送货方式。这是我项目中的有效解决方案。

public function createOrderAction($paymentMethod) 
    {
        try {
        $checkout_session = Mage::getSingleton('checkout/session');
        $cq = $checkout_session->getQuote();
        $cq->assignCustomer(Mage::getSingleton('customer/session')->getCustomer());
        $quoteObj = Mage::getModel('sales/quote')->load($checkout_session->getQuoteId()); // Mage_Sales_Model_Quote
        //Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()

        $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');

        $quoteObj->setShippingAddress($cq->getShippingAddress());
        $quoteObj->setBillingAddress($cq->getBillingAddress());
        $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()));

        // convert quote items
        foreach ($items as $item) {
            // @var $item Mage_Sales_Model_Quote_Item
            $orderItem = $convertQuoteObj->itemToOrderItem($item);
            $orderItem->setWholesaler($item->getWholesaler());
            $orderItem->setComment($item->getComment());
            $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);
            $orderObj->place();
            $quoteObj->setIsActive(0)->save();
            $orderObj->save(); 
            $orderObj->sendNewOrderEmail(); 
            return $orderObj->getId();
        } catch (Exception $e){     
            Mage::log($e->getMessage());
            Mage::log($e->getTraceAsString());
        }
    }

答案 1 :(得分:0)

请尝试:

$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll(); 

那么您应该可以通过以下方式获得创建的订单:

$service->getOrder();