Bypass cart and go to checkout

Sometimes there is a need to bypass the Magento shopping cart and go straight to checkout. Maybe your products (or services) are such that most customers only buy one product so why not speed up checkout by bypassing the cart.

Although Magento has an option in admin of whether to stay on product page or go to the shopping cart when you add a product to the cart, it does not handle bypass the shopping cart completely.

For this solution we will be creating a custom extension called Cart in the MCorner namespace. We will use Magento’s Event-Observer methodology to trigger a redirect to the checkout page.┬áSo let’s get started.

Create the file app/etc/modules/MCorner_Cart.xml to enable the module.

<?xml version="1.0"?>
<config>
    <modules>
        <MCorner_Cart>
            <active>true</active>
            <codePool>community</codePool>
        </MCorner_Cart>
    </modules>
</config>

Now create the following directory path app/code/community/MCorner/Cart/etc.
Create the file config.xml with the following in it. This listens to the checkout_cart_add_product_complete event and calls our observer.

<?xml version="1.0"?>
<config>
    <modules>
        <MCorner_Cart>
            <version>0.1.0</version>
        </MCorner_Cart>
    </modules>
    <frontend>
        <events>
            <checkout_cart_add_product_complete>
                <observers>
                    <mcorner_cartbypass_observer>
                        <type>singleton</type>
                        <class>MCorner_Cart_Model_Observer</class>
                        <method>afterAddToCart</method>
                    </mcorner_cartbypass_observer>
                </observers>
            </checkout_cart_add_product_complete>
        </events>
    </frontend>
</config>

Create the following directory path: app/code/community/MCorner/Cart/Model.
Now create a file Observer.php in the Model directory with the following:

<?php

class MCorner_Cart_Model_Observer extends Varien_Object
{
    public function afterAddToCart(Varien_Event_Observer $observer) {
        $response = $observer->getResponse();

        $response->setRedirect(Mage::getUrl('checkout/onepage'));
        Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
    }
}

The above code is our observer that triggers the redirect.
Now clear the cache and test adding a product to the cart.

Let us know your thoughts below.