3
votes

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.

2

2 Answers

4
votes

I think you can use jQuery for this.

Checkout Button in list.phtml

<button type="button" title="<?php echo $this->__('Check out') ?>" class="button btn-cart" onclick="setcheckoutLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>

And add this script to same list.phtml file

<script>
function setcheckoutLocation(location)
{
jQuery.ajax({
                    type:"GET",
                    url:location,
                    success:function(data){
                         window.location.href = "http://your-checkout-page-url";
                    }
                 });

}
</script>
3
votes

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