Magento核心折扣

ueum 发布于 2019-01-31 coupon 最后更新 2019-01-31 18:44 44 浏览

我一直在努力这一段时间,应该是非常直接的,但... 我试图使用magento cart.coupon.add api来申请优惠券。 该产品是虚拟的 这里是我的代码(在我来到这里之前,我已经尝试了所有可以在google上找到的东西):

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,并且用户从他的信用卡中全额支付。 任何人....?帮帮我...?
已邀请:

ksequi

赞同来自:

终于找到了答案 在添加要引用的任何项目之前,我需要调用setCouponCode()

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