以编程方式创建的订单不发送订单收据电子邮

时间:2014-01-21 22:01:29

标签: magento

当我通过Magento商店正常下订单时,我会立即收到电子邮件收据。当我通过自定义代码创建订单时,不会发送电子邮件。

这是我的代码:

private function _submitOrder($customer, $billing, $shipping, $products, $payment, $coupon, $finalize){
    if($coupon && $this->_checkCouponValidity($coupon)){
        $quote = Mage::getModel('sales/quote')->setCouponCode($coupon)->load();
    }else{
        $quote = Mage::getModel('sales/quote');
    }

    foreach($products as $item) {
        if($item['qty']>0){
            $product = Mage::getModel('catalog/product');
            $productId = Mage::getModel('catalog/product')->getIdBySku($item['sku']);
            $product->load($productId);
            $quoteItem = Mage::getModel('sales/quote_item')->setProduct($product);
            $quoteItem->setQuote($quote);
            $quoteItem->setQty($item['qty']);
            $quote->addItem($quoteItem);
        }
    }

    $quote->getBillingAddress()
        ->addData($billing);

    $quote->getShippingAddress()
        ->addData($billing)
        ->setShippingMethod('tablerate_bestway')
        ->setPaymentMethod('authorizenet')
        ->setCollectShippingRates(true);

    $quote->setCheckoutMethod('guest')
        ->setCustomerId(null)
        ->setCustomerEmail($quote->getBillingAddress()->getEmail())
        ->setCustomerIsGuest(true)
        ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);




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


    $convertQuote = Mage::getSingleton('sales/convert_quote');

    $quotePayment = $quote->getPayment(); // Mage_Sales_Model_Quote_Payment
    $quotePayment->setMethod('authorizenet');

    $order = $convertQuote->addressToOrder($quote->getShippingAddress());

    if($finalize){
        $orderPayment = $convertQuote->paymentToOrderPayment($quotePayment);

        $order->setBillingAddress($convertQuote->addressToOrderAddress($quote->getBillingAddress()));
        $order->setShippingAddress($convertQuote->addressToOrderAddress($quote->getShippingAddress()));

        $order->setPayment($convertQuote->paymentToOrderPayment($quote->getPayment()));

        $order->getPayment()->setCcNumber($payment['ccNumber']);
        $order->getPayment()->setCcType($payment['ccType']);
        $order->getPayment()->setCcExpMonth($payment['ccExpMonth']);
        $order->getPayment()->setCcExpYear($payment['ccExpYear']);
        $order->getPayment()->setCcLast4($payment['ccLast4']);
    }
    Mage::log("loop quote items");
    foreach ($quote->getAllItems() as $item) {
        $orderItem = $convertQuote->itemToOrderItem($item);
        if ($item->getParentItem()) {
            $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
        }
        $order->addItem($orderItem);
    }

    try {
        if($finalize){
            $order->place();
            $order->save();
            if ($order->getCanSendNewEmailFlag()) {
                try {
                    $order->sendNewOrderEmail();
                } catch (Exception $e){
                    Mage::log($e->getMessage());
                }
            }
            return $order;
        }
        else{
            return $order;
        }
    } catch (Exception $e){
        return $e->getMessage();
    }


}

1 个答案:

答案 0 :(得分:0)

我发现了问题。

这一行:

$quote->getBillingAddress()->getEmail()

什么都没回来。创建$ billing时我没有设置发送电子邮件。糟糕。