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

14,834

Solution 1

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

Solution 2

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>
Share:
14,834

Related videos on Youtube

RattanOutdoorFurnitureDev
Author by

RattanOutdoorFurnitureDev

I am the lead developer for Rattan Outdoor Furniture, an industry leader in the american market for retail and wholesale synthetic wicker and rattan outdoor patio furniture. The website is undergoing a major re-development, based on the Magento Framework. Used mainly as a framework, and less as a CMS. Many custom elements have been added to this framework: modules, observers, handles, controllers, etc. I look to the community at stack overflow with assistance with many questions in the programming sphere, on many projects, as I believe learning through example and the expertise of others is, by far, the best method. I am a strong PHP developer with a deep rooted history with front-end technologies, mainly javascript. My real niche is developing RESTful APIs in a LAMP stack server environment, and developing a front-end client, preferably with javascript, to retrieve information from the aforementioned API in an exciting and easy-to-use experience for the end user!

Updated on October 06, 2022

Comments

  • RattanOutdoorFurnitureDev
    RattanOutdoorFurnitureDev over 1 year

    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.

  • RattanOutdoorFurnitureDev
    RattanOutdoorFurnitureDev almost 10 years
    Thanks. This is a good solution. I would like to have it run in a custom controller, which is what i think you're suggesting now, but I just added some similar logic to the default addAction. I just passed an additional parameter if they click my checkout button, and it does the redirect to 'checkout/onepage' if the additional parameter is set. But I just added this to the core, which i don't like doing, but i was not able to get this into a module. Is a having a separate controller a better method than an additional parameter? I suppose if it were in the local code pool, that would make sense
  • RattanOutdoorFurnitureDev
    RattanOutdoorFurnitureDev almost 10 years
    My products have additional custom properties (color option). So I'm not sure how this would work form a server-side defined url. But I was hoping I could just pass the redirect_url as the checkout page. but that didn't work. Editing in an additional parameter into the core cartController, worked. Although i would like to abstract that from the core.
  • Sunry
    Sunry over 8 years
    Like this jQuery one, it doesn't need to modify core file.
  • Gem
    Gem over 6 years
    @aziz : I did paste script , where i can paste button, and how can i run?