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

吃可爱长大的小学妹 提交于 2019-12-05 01:45:24

问题


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!


回答1:


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/




回答2:


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>



回答3:


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

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