magento:用于添加产品并将折扣优惠券应用于购物车的URL查询字符串 [英] magento: URL querystring for adding product and applying discount coupon to cart

查看:56
本文介绍了magento:用于添加产品并将折扣优惠券应用于购物车的URL查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,经过广泛的研究和探索,我能够弄清楚以下几点:

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

/checkout/cart/add?product=76&qty=1

应用我可以使用的折扣代码

to apply discount code I can use

/checkout/cart/couponPost?coupon_code = WQ9D-XXXX

/checkout/cart/couponPost?coupon_code=WQ9D-XXXX

它的代码位于文件中:

/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

/checkout/cart/couponPost?product=76&qty=1&coupon_code=WQ9D-XXXX

OR

/checkout/cart/add?product = 76 & qty = 1 & coupon_code = WQ9D-XXXX

/checkout/cart/add?product=76&qty=1&coupon_code=WQ9D-XXXX

或 还有其他方法吗?

是否有办法使它正常工作?想法是将此链接嵌入到新闻通讯中,这样用户只需单击一下就可以将产品添加到购物车并获得折扣.

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.

我尝试从function addAction()调用$this->couponPostAction();,反之亦然,但是它不起作用!

I have tried calling $this->couponPostAction(); from function addAction() and vice-versa but it does not work!

推荐答案

rukpat的答案在Magento 1.8中不起作用.扩展CartController的addAction方法后,您需要像这样设置URL和查询字符串的格式:

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

您还可以省略最后一个return_url参数.无需包含;; (分号).

You can also omit the last return_url parameter. There's no need to include the ; (semicolons).

您还可以通过以下操作将具有多种数量的多种产品添加到URL:

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

因此,只需添加带有对产品ID的多个引用的& related_product = 28,28,28,就可以添加多个数量的该商品.不太优雅,但是可以.

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.

当然,最好扩展CartController的addAction方法.

Of course, it would be better to extend the addAction method of the CartController.

要使此解决方案在Magento 1.8及更高版本中有效,您需要像下面这样修改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

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

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

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>

这篇关于magento:用于添加产品并将折扣优惠券应用于购物车的URL查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆