我正在尝试通过我的自定义扩展为订单应用自定义折扣。我正在显示用于添加优惠券代码的自定义文本框,然后我从 API 获取该代码的折扣金额并尝试申请相应的订单。
错误:使用下面的代码我的折扣显示在订单审查页面上,但它没有被最终订单下达。最终订单显示没有折扣。
我把下面的函数放在我的控制器中。
require_once 'Mage/Checkout/controllers/CartController.php';
类 Company_ModuleName_IndexController 扩展 Mage_Checkout_CartController {
public function customcouponPostAction() {
$quote = $this->_getQuote();
$quoteid = $quote->getId();
$discountAmount = 20;
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) {
//echo $address->setDiscountAmount; exit;
$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() . ', Custom Discount');
$address->setBaseDiscountAmount(-($address->getBaseDiscountAmount() - $discountAmount));
} else {
$address->setDiscountAmount(-($discountAmount));
$address->setDiscountDescription('Custom Discount');
$address->setBaseDiscountAmount(-($discountAmount));
}
$address->save();
}//end: if
} //end: foreach
//echo $quote->getGrandTotal();
foreach ($quote->getAllItems() as $item) {
//We apply discount amount based on the ratio between the GrandTotal and the RowTotal
$rat = $item->getPriceInclTax() / $total;
$ratdisc = $discountAmount * $rat;
$item->setDiscountAmount(($item->getDiscountAmount() + $ratdisc) * $item->getQty());
$item->setBaseDiscountAmount(($item->getBaseDiscountAmount() + $ratdisc) * $item->getQty())->save();
}
}
}
$this->loadLayout(false);
$review = $this->getLayout()->getBlock('roots')->toHtml();
$response['review'] = $review;
//$this->_goBack();
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}
}
谢谢,