Rewrite you can do this magento two process
Process One:
one is rewrite the class Mage_Checkout_CartController
and in this addAction()
at code if (!$cart->getQuote()->getHasError()) {
add redirection
code $this->_redirect('checkout/onepage');
Process two:
Using event/Observer
Event:
checkout_cart_add_product_complete
This event provide the data of product object and cart request parameters
Mage::dispatchEvent('checkout_cart_add_product_complete',
array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
);
Config.xml code:
<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>
Observer code:
using this event you can set your return url
$observer->getEvent()->getRequest()->setParam('return_url',Mage::getUrl('checkout/onepage'));
this redirect to you at checkout Onepage
function:
public function addToCartComplete(Varien_Event_Observer $observer) {
$product = $observer->getEvent()->getProduct();
$Request=$observer->getEvent()->getRequest();
$Request->setParam('return_url',Mage::getUrl('checkout/onepage'));
}
this redirect to you at Checkout onepage
The add to cart request is a GET request that among other params, it can contain a uenc param with can hold a base64 encoded return URL. The user is redirected to that URL upon return from add to cart.
You can simply create a new button that you add anywhere on the page, that on click will do a GET request to a URL like https://example.com/checkout/cart/add/product/XYZ/uenc/aHR0cHM6Ly9leGFtcGxlLmNvbS9jaGVja291dC9vbmVwYWdlCg==/
where aHR0cHM6Ly9leGFtcGxlLmNvbS9jaGVja291dC9vbmVwYWdlCg== is base64 encoded https://example.com/checkout/onepage
Best Answer
First, change Add to Cart Button to Buy Now.
Then add a hidden input field to the #product_addtocart_form form. Name should be return_url and its value should be
<?php echo Mage::getUrl('checkout/onepage')?>"
:If you want both Add to Cart and Buy now, add the buy now button to
addtoCart.phtml
On that buttons onlick event, a custom
VarienForm
is used,productBuyNowForm.submit(this)
, which will submit this form and redirect to checkout onepage.To define this custom form, add the following JavaScript code at view.phtml