Magento Add to Cart and Redirect to Checkout (product page: on "Checkout" button, "Add to Cart" redirects to cart)

checkout, magento, product, redirect

Solution

create copy of function `addAction` of `cartController.php`(`app/code/core/checkout/controllers/`) to myaddAction().

add below to end view.phtml`(app/design/frontend/your package/your template/template/catalog/product/view.phtml)`

    <script type="text/javascript">
    //<![CDATA[
 productAddToCartForm.submit = function(button, url){

 replaceURL = url.replace("add/","myadd/");
            if(this.validator) {
                var nv = Validation.methods;
                delete Validation.methods['required-entry'];
                delete Validation.methods['validate-one-required'];
                delete Validation.methods['validate-one-required-by-name'];
                // Remove custom datetime validators
                for (var methodName in Validation.methods) {
                    if (methodName.match(/^validate-datetime-.*/i)) {
                        delete Validation.methods[methodName];
                    }
                }

                if (this.validator.validate()) {
                    if (url) {
                        this.form.action = replaceURL;
                    }
                    this.form.submit();
                }
                Object.extend(Validation.methods, nv);
            }
        }.bind(productAddToCartForm);
        //]]>
    </script>

add checkout button to addtocart.phhtml `(app/design/frontend/your package/your template/template/catalog/product/view`)

 <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submitmy(this)"><span><span><?php echo "Checkout"; ?></span></span></button>

As i tell you copy addAction to mycartAction Change

$this->_goBack();

to

$this->_redirect('checkout/onepage');
        return; 

endof

 if (!$cart->getQuote()->getHasError()){
                    $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
                    $this->_getSession()->addSuccess($message);
                }

add

Problem

I have two "Add to Cart" buttons on my product pages. The first, default functionality, is just "Add to Cart", which functions as it should, adding the product to the cart, and redirects to the cart. The second is labeled "Checkout" which I would like to have add the product to the cart, and redirect to checkout instead of the cart. (But only if the Checkout button was clicked). I have looked, and it seems like an Observer might be used? I am not sure how to implement this, or differentiate which button was clicked, or what url to point the Checkout button to.

Original source