【问题标题】:Adding discount by coupon code programmatically以编程方式通过优惠券代码添加折扣
【发布时间】:2016-07-13 12:05:47
【问题描述】:
 public function coupon($data) {
        $couponCode = $data['couponcode'];
        if (!Zend_Validate::is(trim($couponCode), 'NotEmpty')) {
            throw new Exception($this->__('coupon code cannot be empty.'));
        }
        $oCoupon = Mage::getModel('salesrule/coupon')->load($couponCode, 'code');
        $data = $oCoupon->getData();
        if (empty($data)) {
            throw new Exception($this->__('coupon code did not match.'));
        }     
        $quote = Mage::getModel('checkout/session')->getQuote();
        $quote->setCouponCode($couponCode);
        $quote->save();
        $quoteData = Mage::getModel('checkout/cart')->getQuote();
        $subTotal = $quoteData['subtotal'];
        $subtotal_with_discount = $quoteData['subtotal_with_discount'];
        $grandTotal = $quoteData['grand_total'];
        $discountTotal = ($subTotal - $subtotal_with_discount);
        $discount = number_format($discountTotal, 4, null, '');
        return $discount;
    }

优惠券代码已应用并显示,但当我打印 quoteData->GetData() 时折扣不会到来,当我更新刷新购物车页面时折扣即将到来

【问题讨论】:

    标签: magento-1.9 checkout cart quote discount


    【解决方案1】:

    我找到了解决方案

    public function coupon($data) {
            $couponCode = $data['couponcode'];
            if (!Zend_Validate::is(trim($couponCode), 'NotEmpty')) {
                throw new Exception($this->__('coupon code cannot be empty.'));
            }
            $oCoupon = Mage::getModel('salesrule/coupon')->load($couponCode, 'code');
            $data = $oCoupon->getData();
            if (empty($data)) {
                throw new Exception($this->__('coupon code did not match.'));
            }
            Mage::getSingleton('checkout/cart')->getQuote()->getShippingAddress()
                    ->setCollectShippingRates(true);
            Mage::getSingleton('checkout/cart')->getQuote()
                    ->setCouponCode($couponCode)->collectTotals()->save();
            $quoteData = Mage::getModel('checkout/cart')->getQuote();
            $subTotal = $quoteData['subtotal'];
            $subtotal_with_discount = $quoteData['subtotal_with_discount'];
            $grandTotal = $quoteData['grand_total'];
            $discountTotal = ($subTotal - $subtotal_with_discount);
            $discount = number_format($discountTotal, 4, null, '');
    
            return $discount;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 2012-12-27
      • 2011-06-21
      • 2014-01-09
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      相关资源
      最近更新 更多