0
votes

I want to add a button "Direct Checkout" on my product details page. On click, it will add product to cart and redirect to checkout/onepage.

By default on addtocart Magento's redirect to cart page.

I have added a hidden field on product page and adding value in it on click of Direct Checkout button using javascript. Then in observer checking that value and redirect to checkout.

I have implement the above logic in observer checkout_cart_add_product_complete but it is not redirecting to checkout page, instead it redirects to cart page.

My observer function is:

public function afterAddToCart(Varien_Event_Observer $observer)
{
    $params = Mage::app()->getFrontController()->getRequest()->getParams();
    if(isset($params['dco']) && $params['dco'] == 1){
        Mage::app()->getResponse()->setRedirect(Mage::getUrl("checkout/onepage"));
    }
}

I suppose it is not working because after my redirect code, magento executes default addtocart code which redirects it to cart again.

Is there any way I can break and simply redirect to checkout page?

3

3 Answers

1
votes

I typically go with

protected function _redirectToUrl($url, $code = 302)  {

    Mage::app()->getResponse()
        ->setRedirect($url, $code)
        ->sendResponse();
    exit;
}

You might want a different observer however: sales_quote_save_after

1
votes

Please check the answer form this post https://stackoverflow.com/a/4053248/3018289

Here is a copy for quick access.

You could create an observer listening for the checkout_cart_add_product_complete event and in there you could do something like the following

  public function addToCartComplete(Varien_Event_Observer $observer) {
    // Send the user to the Item added page
    $response = $observer->getResponse();
    $request = $observer->getRequest();
    $response->setRedirect(Mage::getUrl('checkout/onepage'));
    Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
}

Your config would look something like this

 <frontend>
    <events>
    <checkout_cart_add_product_complete>
      <observers>
        <packagename_modulename_observer>
          <type>singleton</type>
          <class>packagename_modulename/observer</class>
          <method>addToCartComplete</method>
        </packagename_modulename_observer>
      </observers>
      </checkout_cart_add_product_complete>
   </events>
   </frontend>
0
votes

I think that it comes from the fact that you don't send the response. I use :

$response = Mage::app()->getFrontController()->getResponse();
$response->setRedirect(Mage::getUrl("checkout/onepage"));
$response->sendResponse();

It works fine with 1.8 but I imagine it will with 1.9