简体   繁体   中英

Magento 1.9 add parameter to add to cart url

I'm working on a Magento 1.9 installation, which has to have a Direct Checkout aswell as a Add To Cart button. The main (and only) difference between the two is that the Direct Checkout link should skip the cart and redirect to checkout when clicked. I used this code for the functionality, and as you can see there is a comment that applies here.

"All I need to do is to put a parameter /checkout/ to the url". It's driving me crazy.

So this is the check, in an Observer file

if (Mage::app()->getRequest()->getParam('checkout')) {

Seems easy enough, but I've been breaking my head and scouring the web for an answer how to "addParam" in such a way that this check returns true.

This is the form(action) on which I want this functionality:

<form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product) ?>checkout/" method="post" id="product_addtocart_form" <?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>

As you can see I added the /checkout/ manually to the url, which works when you look at the url the browser points to, ie:

<form action="example.com/checkout/cart/add/uenc/aHR0cDovL2JrZm9vdHdlYXIuaHlwZXJub2RlLmlvL21hbi9yb2NvLmh0bWw,/product/5651/form_key/Zs9uAOj8oTuQFAbj/checkout/" method="post" id="product_addtocart_form" <input="" type="hidden" name="sid" value="checkout">

But it doesn't work, it looks like the check function doesn't "see" it. I think this has a most simple answer, but as I said, it's killing me for days now. Any ideas?

Maybe change the if-statement to something checking if "checkout" is in the called url?

Update 2016 : In this same situation I found myself needing to change this solution for tracking-reasons. What I did now (relates to this question ) was to have the default Add To Cart button in place, and added another (default) button with a jQuery onClick function that set the url to the correct add-to-cart-URL with the selected choices of the variable product. Using AJAX I check if the URL is called correctly, and if so, redirect to checkout. A much simpeler solution.

Your problem is that you are checking the parsed params for the key 'checkout' but Magento has actually stored the param as 0 => 'checkout' . I would advise using this code instead:

Template

$this->getAddToCartUrl($_product, array('checkout' => true))

Observer

$observer->getRequest()->getParam('checkout')

Be sure your cache is cleared/disabled while testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM