Magento 一个订单可使用多个优惠券代码,无需延期

2024-04-11

我已经工作了 3 天,试图在一个订单中应用多个优惠券代码。

我知道有可用的模块。 但我不想投资购买模块。我想要一些自定义代码,以便我可以在单个订单中应用多个优惠券代码。


要添加 2 个优惠券代码, 我所做的是这样的:

1)在 sales_flat_quote 表中添加列 coupon_code2

ALTER TABLE sales_flat_quote

ADD ( coupon_code2varchar(255) )”;

2) 要添加新的优惠券代码 2,只需将其保存在报价中:

Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode2($coupon_code)->save();

3)最后为了计算优惠券代码,我在观察者文件中添加了该函数: /magento/app/code/core/Mage/Sales/Model/Observer.php

 public function setDiscountCouponCode2(Varien_Event_Observer $observer)
{
    $couponCode2 = $observer->getEvent()->getQuote()->getCouponCode2();

    $oCoupon = Mage::getModel('salesrule/coupon')->load($couponCode2, 'code');
    $oRule = Mage::getModel('salesrule/rule')->load($oCoupon->getRuleId());
    $ruleData = $oRule->getData();
    $flag =0;
    if(isset($ruleData['product_ids'])){
        $product_ids = $ruleData['product_ids'];
        foreach(Mage::getSingleton('checkout/session')->getQuote()->getAllItems() as $_item){
                if($product_ids == $_item->getProductId()){
                        $flag = 1;
                }
        }
    }


if($flag){
        $ruleSimpleAction = $ruleData['simple_action'];
        $ruleDiscountAmount = $ruleData['discount_amount'];


        $quote=$observer->getEvent()->getQuote();
        $quoteid=$quote->getId();

        $discountAmount=$ruleDiscountAmount;
        if($quoteid) {
            if($discountAmount>0) {
                $total=$quote->getBaseSubtotal();
                $quote->setSubtotal(0);
                $quote->setBaseSubtotal(0);

                $quote->setSubtotalWithDiscount(0);
                $quote->setBaseSubtotalWithDiscount(0);

                $quote->setGrandTotal(0);
                $quote->setBaseGrandTotal(0);


                $canAddItems = $quote->isVirtual()? ('billing') : ('shipping'); 
                foreach ($quote->getAllAddresses() as $address) {

                    $address->setSubtotal(0);
                    $address->setBaseSubtotal(0);

                    $address->setGrandTotal(0);
                    $address->setBaseGrandTotal(0);

                    $address->collectTotals();

                    $quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
                    $quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());

                    $quote->setSubtotalWithDiscount(
                        (float) $quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
                    );
                    $quote->setBaseSubtotalWithDiscount(
                        (float) $quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()
                    );

                    $quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
                    $quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());

                    $quote ->save(); 

                    $quote->setGrandTotal($quote->getBaseSubtotal()-$discountAmount)
                        ->setBaseGrandTotal($quote->getBaseSubtotal()-$discountAmount)
                        ->setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
                        ->setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
                        ->save(); 

                    if($address->getAddressType()==$canAddItems) {
                        $address->setSubtotalWithDiscount((float) $address->getSubtotalWithDiscount()-$discountAmount);
                        $address->setGrandTotal((float) $address->getGrandTotal()-$discountAmount);
                        $address->setBaseSubtotalWithDiscount((float) $address->getBaseSubtotalWithDiscount()-$discountAmount);
                        $address->setBaseGrandTotal((float) $address->getBaseGrandTotal()-$discountAmount);
                        if($address->getDiscountDescription()){
                            $address->setDiscountAmount(-($address->getDiscountAmount()-$discountAmount));
                            $address->setDiscountDescription($address->getDiscountDescription().', Instant Exchange -'.$couponCode2);
                            $address->setBaseDiscountAmount(-($address->getBaseDiscountAmount()-$discountAmount));
                        }else {
                            $address->setDiscountAmount(-($discountAmount));
                            $address->setDiscountDescription('Instant Exchange -'.$couponCode2);
                            $address->setBaseDiscountAmount(-($discountAmount));
                        }
                        $address->save();
                    }
                }

                foreach($quote->getAllItems() as $item){
                    $rat=$item->getPriceInclTax()/$total;
                    $ratdisc=$discountAmount*$rat;
                    $item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
                    $item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();
                }
            }
        }
}else{
        Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode2('')->save();
    }
}

4) 最后通过更新同一文件中的 setQuoteCanApplyMsrp 函数来调用上述函数:

 public function setQuoteCanApplyMsrp(Varien_Event_Observer $observer)
{
    /** @var $quote Mage_Sales_Model_Quote */
    $quote = $observer->getEvent()->getQuote();

    $canApplyMsrp = false;
    if (Mage::helper('catalog')->isMsrpEnabled()) {
        foreach ($quote->getAllAddresses() as $adddress) {
            if ($adddress->getCanApplyMsrp()) {
                $canApplyMsrp = true;
                break;
            }
        }
    }
    $this->setDiscountCouponCode2($observer);
    $quote->setCanApplyMsrp($canApplyMsrp);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Magento 一个订单可使用多个优惠券代码,无需延期 的相关文章

  • 我如何覆盖Mage_Core_Controller_Request_Http

    我对 Mage Core Controller Request Http 做了一些更改 但在使用 magento 分发的文件中 我知道这不是最好的方法 但我无法弄清楚如何覆盖控制器目录中的文件 我可以找到如何覆盖控制器目录中的文件 谁能告诉
  • 如何在Magento的view.phtml中显示标签产品

    我创建了新主题并使用它们来创建产品 在查看页面中 我没有看到产品标签 我默认查看 view pthml 并知道中的标签 echo this gt getChildHtml product additional data 我的 view ph
  • 扩展 Magento 购物车

    我需要扩展 Magento 购物车以包含商店定位器的额外步骤 我知道我需要覆盖核心 OnePage 控制器 Mage Checkout OnepageController 和块 Mage Checkout Block Onepage 但是在
  • Magento 多个 Authorize.net 网关

    我有一个用于处理信用卡付款的 CAD 和 USD 网关帐户 这两个帐户均来自支持 Authorize net API 的支付提供商 我已经使用 Magento 的内置 Authorize net 支持成功配置了一个 但是如何配置第二个 Au
  • Magento:设置集合限制

    我试图找出的问题是如何对集合设置限制 我在 Google 上找到的答案仅适用于具有 setPage pageNum pageSize 的目录 这对任何其他集合都不起作用 请参阅下面的答案 做这件事有很多种方法 collection Mage
  • 使用 getCollection 和 addLevelFilter 列出 Magento 类别,但排除默认根类别

    我使用以下代码来获取集合并使用 addLevelFilter 2 在级别上进行过滤 这会完美地输出第 2 级的所有类别 除了它还会提取列表中的默认根类别之外 我想从视图中排除它 但在查看了所有可用的方法后 我没有看到任何可以帮助我删除 排除
  • 在不破坏寻呼机的情况下获取第一个集合项

    我之前发布了一个关于此问题的问题 但我现在有了更多信息 我认为最好发布一个新问题而不是修改 抱歉 如果这不是正确的协议 你可以找到我原来的问题here https stackoverflow com questions 6311646 ma
  • 如何将 CORS(跨源策略)添加到 NGINX 中的所有域?

    我创建了一个文件夹 用于提供静态文件 CSS 图像 字体和 JS 等 我最终会将文件夹 CNAME 到子域中 以便在 CDN 上使用 以便与我的 Magento 2 设置一起使用 我想允许所有域通过 CORS 跨源策略进行所有访问 并且我也
  • 如何在magento中将订单状态设置为“完成”

    如何手动将订单状态设置为 完成 我正在使用以下代码 但它给出错误消息 订单状态 完成 不得手动设置 order Mage getModel sales order gt loadByIncrementId order id order gt
  • 在 Magento 中获取过去 24 小时内的订单商品

    我正在尝试获取过去 24 小时内的所有订单商品 我已锁定查询 因此它返回了我需要的内容 order id 和created on 值 order items Mage getResourceModel sales order item co
  • IE 抛出 JavaScript TypeError 但在 chrome 上不抛出

    描述在我们的 Magento 购物车上 当用户单击添加到购物篮在任何 Internet Explorer 浏览器的 产品详细信息 页面上单击按钮 浏览器中都会弹出一个包含以下错误消息的窗口 异常 类型错误 无法获取未定义或空引用的属性 ta
  • 您常见的 Magento 配置错误有哪些? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 为什么在网关取消的订单状态没有转换为“ payment_pending ”?

    我正在使用 Magento 社区 ver1 6 1 0 我在 Magento wiki 上找到了这个状态图http www magentocommerce com wiki 2 magento concepts and architectu
  • Magento 如何通过静态块/页面中的 id 链接到类别

    我正在寻找使用以下方法从静态块链接到一个类别category id 有什么想法吗 我已经进行了通常的搜索 但没有结果 目前我可以做类似的事情 a href 但这并不稳健 使用类别链接小部件内联链接代码 widget type catalog
  • Magento 将所见即所得添加到自定义前端表单

    我正在编写一个模块 并且正在搜索如何将内置所见即所得编辑器添加到前端的文本区域 有可能的 有谁知道 如何实施 因此 我将其保留为最终解决方案 以防万一有人需要它 1 将此代码放入您希望编辑器直接显示的 phtml文件中 2 在代码第6行可以
  • 根据产品属性在 Magento 中创建购物车规则

    我在一个类别中有产品 针 有些以 100 支为一包出售 有些以 500 支为一包出售 盒子中的针数被设置为产品属性 我想根据购物车中的针总数应用购物车规则 F x 如果您购买 1000 2000 根针头 无论 500 100 包的组合如何
  • Magento:设置刚刚创建的网站的配置值?

    我正在以编程方式创建网站 用户等 问题是 创建网站时 我无法立即设置配置值 Code
  • 如何在 magento 的左侧栏中显示最新、评价最高和最好的产品

    我正在使用 Magento 版本 1 5 0 1 在主页中 我使用了 2 列和左栏 我想逐一展示最新的 评价最高的和最好的产品 请帮助我如何做到这一点 我是magento新手请帮助我 在您的 app design frontend your
  • Magento --“SQLSTATE[23000]:违反完整性约束..”客户更新

    迁移服务器后 每次尝试更新客户信息时都会出现错误 我正在使用一个客户激活插件 http www magentocommerce com magento connect vinai extension 489 customer activat
  • 如何从 Magento One Page Checkout 获取发布数据?

    为了在 Magento Checkout 中添加客户评论字段 我在相应的模板文件中添加了一个文本字段 并使用如下观察器将评论添加到订单中 comment strip tags Mage app gt getRequest gt getPar

随机推荐