Skip to content

Instantly share code, notes, and snippets.

@RabeaWahab
Created January 5, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RabeaWahab/42fa57f7199bfc1ce31f to your computer and use it in GitHub Desktop.
Save RabeaWahab/42fa57f7199bfc1ce31f to your computer and use it in GitHub Desktop.
public function couponPostAction()
{
/**
* No reason continue with empty shopping cart
*/
if ($this->_expireAjax()) {
return;
}
$couponCode = (string) $this->getRequest()->getParam('coupon_code');
if ($remov=$this->getRequest()->getParam('remove') == 1) {
$couponCode = '';
}
//$result['enabled']=false;
$oldCouponCode = $this->_getQuote()->getCouponCode();
if (!strlen($couponCode) && !strlen($oldCouponCode)) {
$result['enabled']=false;
return;
}
/**
* Gift Certificate Change
*/
$cert = Mage::getModel('ugiftcert/cert')->load($couponCode, 'cert_number');
try {
if ($cert->getId()){
if($cert->getStatus()=='A' && $cert->getBalance()>0) {
$quote = $this->_getQuote();
$cert->addToQuote($quote);
$quote->collectTotals()->save();
// success return
$result['success'] = $this->__("Gift certificate '%s' was applied to your order.", Mage::helper('core')->htmlEscape($cert->getCertNumber()));
$result['reload']=true;
} else {
// fail return
$result['error'] = $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->htmlEscape($cert->getCertNumber()));
$result['reload']=true;
}
} else {
$this->_getQuote()->getShippingAddress()->setCollectShippingRates(true);
$this->_getQuote()->setCouponCode(strlen($couponCode) ? $couponCode : '')
->collectTotals()
->save();
}
if ($couponCode) {
if(!$cert->getId()){
if ($couponCode == $this->_getQuote()->getCouponCode()) {
$result['success'] = $this->__('Coupon code "%s" was applied successfully.', Mage::helper('core')->htmlEscape($couponCode));
$result['reload']=true;
}
else {
$result['error'] =$this->__('Coupon code "%s" is not valid.', Mage::helper('core')->htmlEscape($couponCode));
$result['reload']=false;
}
}
} else {
$result['error'] = $this->__('Coupon code was canceled successfully.');
$result['reload']=true;
}
}
catch (Mage_Core_Exception $e) {
$result['error'] = $e->getMessage();
}
catch (Exception $e) {
$result['error'] = $this->__('Can not apply coupon code.');
}
$this->getResponse()->setBody(Zend_Json::encode($result));
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment