簡體   English   中英

在 Magento 1.9.x 中以編程方式創建訂單后,如何在成功頁面上獲取訂單 ID 和打印詳細信息

[英]How to get order id and print details on success page after order creating programmatically in Magento 1.9.x

我正在使用 Magento 1.9.x,我想通過報價創建訂單,我可以這樣做:

    private function createOrder()
{
    $orderId = $session->getTransactionId();
    $quote = $this->_getCheckout()->getQuote();
    $quote->setReservedOrderId($orderId);
    $quote->collectTotals()->getPayment()->setMethod($this->_getPaymentMethod());
    $quoteId = $quote->getId();
    $service = Mage::getModel('sales/service_quote', $quote);
    $service->submitAll();
    $order = $service->getOrder();
    $order->setStatus('complete');
    $order->save();
    $checkoutSession = Mage::getSingleton('checkout/type_onepage')->getCheckout();
    $checkoutSession->setLastSuccessQuoteId($quoteId);
    $checkoutSession->setLastQuoteId($quoteId);
    $checkoutSession->setLastOrderId($order->getIncrementId());
}

protected function _getCheckout()
{
    return Mage::getSingleton('checkout/session');
}

protected function _getPaymentMethod()
{
    return $this->_getCheckout()->getQuote()->getPayment()->getMethod();
}

但我的問題是成功頁面。 當我重定向到成功頁面時,就像這樣

沒有出現訂單ID或打印類似的細節

我該如何解決?

謝謝編輯:

我想創建自定義付款方式。 如果客戶選擇我的方法,我會刪除“下訂單”按鈕,並在訂單審核時創建 iframe。

app\\design\\frontend\\base\\default\\layout\\mymodule.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <checkout_onepage_review>
        <reference name="checkout.onepage.review.button">
            <action method="setTemplate">
                <template helper="mymodule/data/getReviewButtonTemplate">
                    <name>mymodule/onepage/iframe.phtml</name>
                    <block>checkout.onepage.review.button</block>
                </template>
            </action>
        </reference>
    </checkout_onepage_review>
</layout>

app\\code\\local\\Mycompany\\Mymodule\\Helper\\data.php

 <?php

class Mycompany_Mymodules_Helper_Data extends Mage_Core_Helper_Abstract
{


      public function getHostedUrl()
        {
            //get iframe url from soap
        }

    public function GetHostedPaymentProcessStatusResult()
    {
        //check from soap response is correct
    }

    public function getReviewButtonTemplate($name, $block)
    {
        $paymentcode = $this->_getPaymentMethod();

        if ($paymentcode == 'mymodule')
            return $name;
        if ($blockObject = Mage::getSingleton('core/layout')->getBlock($block))
            return $blockObject->getTemplate();
        return '';
    }
  public function getConfigData($configName)
{
    return Mage::getStoreConfig('payment/mymodule/' . $configName, Mage::app()->getStore());
}
    protected function _getCheckout()
    {
        return Mage::getSingleton('checkout/session');
    }

    protected function _getPaymentMethod()
    {
        return $this->_getCheckout()->getQuote()->getPayment()->getMethod();
    }
}

付款在 iframe 中完成,並使用一些參數調用我的模塊響應頁面。

app\\code\\local\\Mycompany\\Mymodule\\controllers\\PaymentController.php

    <?php

class Mycompany_Mymodule_PaymentController extends Mage_Core_Controller_Front_Action
{
    public function responseAction()
    {
        $session = Mage::getSingleton('core/session');
        $transactionId = $_GET['TransactionId'];
        $orderId = $session->getTransactionId();
        $mymoduleTransId = $_GET['MymoduleTransId'];
        $helper = Mage::helper('mymodule');
        if ($_GET['Result'] == 'PaymentOk' && $transactionId == $orderId) {
            $quote = Mage::getSingleton('checkout/session')->getQuote();
            $result = $helper->getHostedPaymentProcessStatus($transactionId, $mymoduleTransId);
            if ($result) {
                $result = $result->GetHostedPaymentProcessStatusResult;
                $resultCode = $result->ResultCode;
                if ($resultCode === "Success") {
                    $quote->setReservedOrderId($orderId);
                    $quoteId = $quote->getId();

                    $quote->collectTotals()->getPayment()->setMethod($this->_getPaymentMethod());

                    $service = Mage::getModel('sales/service_quote', $quote);
                    $service->submitAll();
                    $order = $service->getOrder();
                    $order->setStatus(Mage::helper('mymodule')->getConfigData('after_pay_status'));
                    $order->save();

                    $checkoutSession = Mage::getSingleton('checkout/type_onepage')->getCheckout();

                    $checkoutSession->setLastSuccessQuoteId($quoteId);
                    $checkoutSession->setLastQuoteId($quoteId);
                    $checkoutSession->setLastOrderId($order->getIncrementId());

                    $this->_clearQuote($quoteId);
                    $returnUrl = Mage::getUrl('checkout/onepage/success', array('_secure' => true));
                } else
                    $returnUrl = Mage::getUrl('checkout/onepage/failure', array('_secure' => true));
            } else {
                $returnUrl = Mage::getUrl('checkout/onepage/failure', array('_secure' => true));
            }
            echo '<script> window.top.location.href = "' . $returnUrl . '";</script>';
        }
    }

    protected function _getCheckout()
    {
        return Mage::getSingleton('checkout/session');
    }

    protected function _getPaymentMethod()
    {
        return $this->_getCheckout()->getQuote()->getPayment()->getMethod();
    }

    protected function _clearQuote($quoteID)
    {
        $quote = Mage::getModel("sales/quote")->load($quoteID);
        $quote->setIsActive(0)->save();
        $quote->delete();
        $quote->save();

        $cart = Mage::getSingleton('checkout/cart');
        $quoteItems = Mage::getSingleton('checkout/session')
            ->getQuote()
            ->getItemsCollection();
        foreach ($quoteItems as $item) {
            $cart->removeItem($item->getId());
        }
        $cart->save();
    }
}

最好的方法(實際上我相信這是 magento 使用的方式)是將訂單 ID 傳遞到會話中。 為此,請使用Mage::getSingleton('core/session')->setIncrementId($order->getIncrementId()); . 然后在成功頁面上使用Mage::getSingleton('core/session')->getIncrementId($order->getIncrementId()); 檢索訂單的增量 ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM