我们如何在心愿单页面上的自定义表单上验证优惠券代码?有没有办法用ajax功能检查优惠券代码的有效性?
我使用了以下代码,但它始终返回有效消息:
$codeLength = strlen($couponCode); // $couponCode is the coupon code entered by the user.
$isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH;
if (!Mage::getSingleton('checkout/cart')->getQuote()->getItemsCount()) {
exit('No items in cart');
}
Mage::getModel('checkout/cart')->getQuote()->getShippingAddress()->setCollectShippingRates(true);
Mage::getModel('checkout/cart')->getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')
->collectTotals()
->save();
if ($codeLength) {
if ($isCodeLengthValid && $couponCode == Mage::getModel('checkout/cart')->getQuote()->getCouponCode()) {
$msg = 'Coupon code applied';
} else {
$msg = 'Coupon code is not valid';
}
} else {
$msg = 'Coupon code was canceled.';
}
简单地说,我已尝试过ajax页面上的购物车商品。但它没有用。我是magento的新手。任何帮助表示赞赏..