1

I want to show Order review page first then I want to show payment page in Checkout page in Magento there is any way if then help me...

1 Answer 1

1

This can be an involved change to make. However, I am hopeful that my code and explanation will make it clear to you.

The approach that is best to take is to rewrite as little as possible. As such, my method involves only rewriting one class (Mage_Checkout_Block_Onepage), to specify the order of the steps. You can do all of this with rewriting, if you wish.

I am assuming that you know how to create a new module. You will also need to change some styling and wording on the frontend.

Config.xml changes:

In your config.xml > config/global/blocks add:

<checkout>
    <rewrite>
        <onepage>SwiftOtter_Test_Block_Override_Checkout_Onepage</onepage>
    </rewrite>
</checkout>

This is where we denote our one override to adjust the order of the checkout steps.

Create the node, config/frontend/events and add:

<!-- Redirecting our shipping method -->
<controller_action_postdispatch_checkout_onepage_saveShippingMethod>
    <observers>
        <SwiftOtter_Test>
            <class>SwiftOtter_Test/Observer</class>
            <method>controllerActionPostDispatchCheckoutOnepageSaveShippingMethod</method>
        </SwiftOtter_Test>
    </observers>
</controller_action_postdispatch_checkout_onepage_saveShippingMethod>
<!-- The final method of our checkout -->
<controller_action_postdispatch_checkout_onepage_savePayment>
    <observers>
        <SwiftOtter_Test>
            <class>SwiftOtter_Test/Observer</class>
            <method>controllerActionPostdispatchCheckoutOnepageSavePayment</method>
        </SwiftOtter_Test>
    </observers>
</controller_action_postdispatch_checkout_onepage_savePayment>
<!-- Intercepting order saving to prevent it from occurring -->
<controller_action_predispatch_checkout_onepage_saveOrder>
    <observers>
        <SwiftOtter_Test>
            <class>SwiftOtter_Test/Observer</class>
            <method>controllerActionPredispatchCheckoutOnepageSaveOrder</method>
        </SwiftOtter_Test>
    </observers>
</controller_action_predispatch_checkout_onepage_saveOrder>
<!-- Sending out payment method HTML, instead -->
<controller_action_postdispatch_checkout_onepage_saveOrder>
    <observers>
        <SwiftOtter_Test>
            <class>SwiftOtter_Test/Observer</class>
            <method>controllerActionPostdispatchCheckoutOnepageSaveOrder</method>
        </SwiftOtter_Test>
    </observers>
</controller_action_postdispatch_checkout_onepage_saveOrder>

Block/Override/Checkout/Onepage.php

class SwiftOtter_Test_Block_Override_Checkout_Onepage extends Mage_Checkout_Block_Onepage
{
    /**
     * Get checkout steps codes
     *
     * @return array
     */
    protected function _getStepCodes()
    {
        return array('login', 'billing', 'shipping', 'shipping_method', 'review', 'payment');
    }
}

Model/Observer.php

This is where all of the work happens. See my function comments in the body below.

class SwiftOtter_Test_Model_Observer
{
    const STEP_PAYMENT = 'payment';
    const PAYMENT_REVIEW_HANDLE = 'checkout_onepage_paymentmethod';

    const STEP_REVIEW = 'review';
    const REVIEW_HANDLE = 'swiftotter_test_save_shipping_method';

    /**
     * We are intercepting the response from save shipping to send the information review for the order.
     */
    public function controllerActionPostDispatchCheckoutOnepageSaveShippingMethod()
    {
        $result = json_decode(Mage::app()->getResponse()->getBody(), true);

        if (is_array($result) && isset($result['goto_section']) && $result['goto_section'] == self::STEP_PAYMENT) {
            $this->_setupReview();

            $result['goto_section'] = self::STEP_REVIEW;
            $result['update_section'] = array(
                'name' => 'review',
                'html' => $this->_loadHtml(self::REVIEW_HANDLE)
            );

            $output = json_encode($result);
            Mage::app()->getResponse()->setBody($output);
        }
    }
    
    /**
     * This is a dirty hack to bypass the order saving process. We are making the action
     * think that it needs to redirect back to the cart.
     * 
     * @param $observer
     */
    public function controllerActionPredispatchCheckoutOnepageSaveOrder($observer)
    {
        /** @var Mage_Core_Controller_Varien_Action $action */
        $action = $observer->getControllerAction();

        $quote = Mage::getSingleton('checkout/cart')->getQuote();
        $quote->setHasError(true);
        $quote->setHasFakeError(true);
    }

    /**
     * Now that we can get back to our logic, we reset the headers/status code from
     * our "trick" of the system, and now we send back payment information.
     * 
     * @param $observer
     * @throws Zend_Controller_Response_Exception
     */
    public function controllerActionPostdispatchCheckoutOnepageSaveOrder($observer)
    {
        $response = Mage::app()->getResponse();
        $result = json_decode($response->getBody(), true);
        $response->clearAllHeaders();
        $response->setHttpResponseCode(200);
        $response->setHeader('content-type', 'application/json');

        $quote = Mage::getSingleton('checkout/cart')->getQuote();

        if ($quote->getHasError() && $quote->getHasFakeError()) {
            $quote->setHasError(false);
        }

        $result['goto_section'] = self::STEP_PAYMENT;
        $result['update_section'] = array(
            'name' => 'payment-method',
            'html' => $this->_loadHtml(self::PAYMENT_REVIEW_HANDLE)
        );

        $output = json_encode($result);
        $response->setBody($output);
    }
    
    
    /**
     * This is the final action of our adjustments. Here, we take the incoming
     * controller, and execute "saveOrderAction" on it. The order is created.
     * 
     * It would be wise to add some error checking as it just saves the order
     * at this point.
     * 
     * After the order is saved, we add a redirect url (usually dealt with by
     * JS, but that won't work here as we are mistreating our onepage.js file).
     * 
     * @param $observer
     */
    public function controllerActionPostdispatchCheckoutOnepageSavePayment($observer)
    {
        /** @var Mage_Core_Controller_Varien_Action $action */
        $action = $observer->getControllerAction();

        $action->saveOrderAction();

        $response = Mage::app()->getResponse();
        $result = json_decode($response->getBody(), true);

        $result['redirect'] = Mage::getUrl('checkout/onepage/success');

        $response->setBody(json_encode($result));
    }

    protected function _setupReview()
    {
        $quote = Mage::getSingleton('checkout/cart')->getQuote();
        $methodCode = 'checkmo'; // logic for determining default payment code

        if (!$quote->getPayment()->getMethod()) {
            $quote->getPayment()->setMethod($methodCode);
            if ($quote->isVirtual()) {
                $quote->getBillingAddress()->setPaymentMethod($methodCode);
            } else {
                $quote->getShippingAddress()->setPaymentMethod($methodCode);
            }
        }
    }

    protected function _loadHtml($handle)
    {
        // very dirty way to reset singleton
        Mage::unregister('_singleton/core/layout');

        $layout = Mage::getSingleton('core/layout');
        $update = $layout->getUpdate();
        $update->load($handle);
        $layout->generateXml();
        $layout->generateBlocks();

        $output = $layout->getOutput();
        return $output;
    }
}
1
  • can you send me the Module for the above
    – prashanth
    May 22, 2017 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.