magento: URL querystring for adding product and applying discount coupon to cart

二次信任 提交于 2019-12-03 17:36:52

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!