我想从外部脚本应用优惠券代码,将代码和折扣金额设置为报价,最终结果将保存报价与新总计。我做了以下但没有保存报价。
$quoteid = "53701";
$couponCode = "Discount99";
$oCoupon = Mage::getModel('salesrule/coupon')->load(trim($couponCode), 'code');
$oRule = Mage::getModel('salesrule/rule')->load($oCoupon->getRuleId());
if($oRule->getRuleId() && $oRule->getRuleId() > 0){
try{
$quoteObj = Mage::getModel('sales/quote')->setCouponCode($oCoupon)->load($quoteid);
$quoteObj->setTotalsCollectedFlag(true)
->collectTotals()
->save();
$returndata["success"] = "1";
$returndata["message"] = "Coupon Applied Successfully";
$returndata["data"]["discount_amount"] = $oRule->getDiscountAmount();
}catch (Exception $e){
$returndata["success"] = "0";
$returndata["message"] = "Cart Coupon update Failed";
$returndata["data"] = null;
}
}else{
$returndata["success"] = "0";
$returndata["message"] = "Invalid Coupon";
$returndata["data"] = null;
}
echo json_encode($returndata);
exit;
答案 0 :(得分:2)
$quoteid = '53703';
$couponCode = "cvpcode2013";
$oCoupon = Mage::getModel('salesrule/coupon')->load(trim($couponCode), 'code');
$oRule = Mage::getModel('salesrule/rule')->load($oCoupon->getRuleId());
if($oRule->getRuleId() && $oRule->getRuleId() > 0){
try{
$quote = $this->_getQuote($quoteid,Mage::app()->getStore()->getStoreId());
$quote->setCouponCode($couponCode);
$quote->setTotalsCollectedFlag(false)->collectTotals();
$quote->collectTotals();
$quote->save();
}catch (Exception $e){
echo $e->getMessage();
}
}else{
// invalid coupon
}
// create new function
protected function _getQuote($quoteId, $store = null)
{
$quote = Mage::getModel("sales/quote");
if (!(is_string($store) || is_integer($store))) {
$quote->loadByIdWithoutStore($quoteId);
} else {
$storeId = $store;
$quote->setStoreId($storeId)
->load($quoteId);
}
if (is_null($quote->getId())) {
$this->_fault('quote_not_exists');
}
return $quote;
}