I am a newbie, after extensive research and exploration I am able to figure out the following:
To add a product to cart I can use
/checkout/cart/add?product=76&qty=1
to apply discount code I can use
/checkout/cart/couponPost?coupon_code=WQ9D-XXXX
The code for it resides in file:
/public_html/app/code/local/Mage/Checkout/controllers/-
I would like to add product and apply discount code in one link such as:
/checkout/cart/couponPost?product=76&qty=1&coupon_code=WQ9D-XXXX
OR
/checkout/cart/add?product=76&qty=1&coupon_code=WQ9D-XXXX
OR Any other means?
Is there a way to get this working? Idea is to embed this link in a newsletter so using one click the user is able to add the product to cart and get discount.
I have tried calling $this->couponPostAction();
from function addAction()
and vice-versa but it does not work!
Got it working without modifying the code,
'http://www.example.com/checkout/cart/add?Product=76&qty;=1&return;_url=http://www.example.com/index.php/checkout/cart/couponPost?coupon_code=WQ9D-XXXX&return;_url=http://www.example.com/checkout/cart/'
the return_url has to be encoded.
Please see: http://www.magentocommerce.com/boards/viewthread/296763/
rukpat's answer doesn't work in Magento 1.8. You need to format the URL and query string like so, once you have extended the addAction method of the CartController:
http://www.example.com/checkout/cart/add?product=76&qty=1&return_url=http://www.example.com/index.php/checkout/cart/couponPost?coupon_code=WQ9D-XXXX
You can also omit the last return_url parameter. There's no need to include the ; (semicolons).
You can also add multiple products to the URL with multiple quantities by simply doing:
http://www.example.com/checkout/cart/add?product=76&related_product=28,28,28&return_url=http://www.example.com/index.php/checkout/cart/couponPost?coupon_code=WQ9D-XXXX
So simply adding &related_product=28,28,28 with multiple references to the product ID allows you to add multiple quantities of that item. Not very elegant but it works.
Of course, it would be better to extend the addAction method of the CartController.
In order for this solution to work in Magento 1.8 and above, you need to modify the CartController like so:
NOTE: Replace 'Namespace' with your own namespace (company name or your name etc).
etc/modules/Namespace_AddProductFromUrl.xml
<?xml version="1.0"?>
<config>
<modules>
<Namespace_AddProductFromUrl>
<active>true</active>
<codePool>local</codePool>
</Namespace_AddProductFromUrl>
</modules>
</config>
app/code/local/Namespace/AddProductFromUrl/controllers/Checkout/CartController.php
<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Namespace_AddProductFromUrl_Checkout_CartController extends Mage_Checkout_CartController {
# overloaded addAction
public function addAction()
{
// generate form_key if missing or invalid
if ( ! ($formKey = $this->getRequest()->getParam('form_key', null)) or $formKey != Mage::getSingleton('core/session')->getFormKey())
{
$this->getRequest()->setParams(array('form_key' => Mage::getSingleton('core/session')->getFormKey()));
}
// do parent actions
parent::addAction();
}
}
app/code/local/Namespace/AddProductFromUrl/etc/config.xml
<config>
<frontend>
<routers>
<checkout>
<args>
<modules>
<Namespace_AddProductFromUrl before="Mage_Checkout">Namespace_AddProductFromUrl_Checkout</Namespace_AddProductFromUrl>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>
I can verify that Gaz's code with above with Namespace_AddProductFromUrl does work on 1.8
In addition, if by chance your product is a bundled one, you have to use this format url:
http://domain.com/en/checkout/cart/add?product=24&qty=1&bundle_option[2]=4&bundle_option[3]=8
来源:https://stackoverflow.com/questions/13232686/magento-url-querystring-for-adding-product-and-applying-discount-coupon-to-cart