【问题标题】:Magento Core discountMagento 核心折扣
【发布时间】:2023-05-20 08:17:01
【问题描述】:

我已经为此苦苦挣扎了一段时间,应该很直截了当,但是... 我正在尝试使用 magento cart.coupon.add api 将优惠券应用于订单。 该产品是虚拟的 这是我的代码(在我来到这里之前,我已经尝试了所有可以在谷歌上找到的东西):

protected function _applyCoupon($quoteId, $couponCode, $store = null)
{
    $coupon = Mage::getModel('salesrule/coupon');
    $coupon->loadByCode($couponCode);
    Mage::log('_applyCoupon('.$couponCode.')');
    $quote = $this->_getQuote($quoteId, $store);

    if (!$quote->getItemsCount()) {
   //     $this->_fault('quote_is_empty');
    }

    $oldCouponCode = $quote->getCouponCode();
    if (!strlen($couponCode) && !strlen($oldCouponCode)) {
        return false;
    }
    try {
        //$quote->getShippingAddress()->setCollectShippingRates(true);
        $quote->setCouponCode($couponCode);
        $quote->setTotalsCollectedFlag(false)->collectTotals();
        $quote->collectTotals();
        $quote->save();
        Mage::getModel("checkout/session")->setData("coupon_code",$couponCode);
        Mage::getModel('checkout/cart')->getQuote()->setCouponCode($couponCode)->save();
        Mage::getModel('checkout/cart')->getQuote()->collectTotals();
        Mage::getModel('checkout/cart')->getQuote()->save();
        Mage::log("_applyCoupon : Set coupon to quote:".$quote->getCouponCode());

    } catch (Exception $e) {
        $this->_fault("cannot_apply_coupon_code", $e->getMessage());
    }
        Mage::log('3');

    if ($couponCode) {
        Mage::log("Coupon applied");
        if (!$couponCode == $quote->getCouponCode()) {
            Mage::log('3.2');
            $this->_fault('coupon_code_is_not_valid');
        }
    }

    return true;
}

我也尝试过将优惠券应用于地址:

protected function applyDiscountToAddress($address,$quote)
{
    Mage::log('applyDiscountToProduct ...');
    $coupon = Mage::getModel('salesrule/coupon');
    Mage::log("checkoutprocess: checkout/session:".Mage::getModel("checkout/session")->getData("coupon_code"));
    $coupon->loadByCode(Mage::getModel("checkout/session")->getData("coupon_code"));
    $rule = Mage::getModel('salesrule/rule');
    $rule->load($coupon->getRuleId());
    $discountamount = $rule->getDiscountAmount();
    $dbldiscount = 0.00 + $discountamount;
    $grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
    $subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
    Mage::log('applyDiscountToProduct $grandTotal:'.$grandTotal);
    Mage::log('applyDiscountToProduct $subTotal:'.$subTotal);

    $gTotal = $grandTotal - $dbldiscount;
    $address->setDiscountAmount($dbldiscount)
        ->setBaseDiscountAmount($dbldiscount)
        ->setGrandTotal($gTotal)
        ->setBaseGrandTotal($gTotal);

    $grandTotal     = $address->getGrandTotal();
    $baseGrandTotal = $address->getBaseGrandTotal();
    Mage::log('applyDiscountToProduct Address:$grandTotal:'.$grandTotal);
    Mage::log('applyDiscountToProduct Address:$baseGrandTotal:'.$baseGrandTotal);

    $totals     = array_sum($address->getAllTotalAmounts());
    $baseTotals = array_sum($address->getAllBaseTotalAmounts());

    $address->setGrandTotal($grandTotal+$totals);
    $address->setBaseGrandTotal($baseGrandTotal+$baseTotals);
}

优惠券是有效的,但在 Magento 管理员中下订单后,我看到折扣金额 = 0.0,并且用户已从他的信用卡中扣除全额费用。 任何人....?帮助...?

【问题讨论】:

    标签: magento discount coupon


    【解决方案1】:

    终于找到答案了

    在添加任何要引用的项目之前,我需要致电 setCouponCode()

    $quote= Mage::getModel('sales/quote')->setCouponCode($couponCode);
    

    【讨论】:

    • 你们能否详细说明一下这个答案?我目前在 Magento 1.9 中将折扣代码应用于虚拟产品时遇到了类似的问题。谢谢!
    • 我们是否应该在加载付款时已经创建的报价或首次创建购物车/报价时执行此操作,在这种情况下,用户不会添加优惠券而是添加购物车中的物品。