public function actionNewpromo() { if (isset($_POST['PromoCode'])) { $objPromo = new PromoCode(); $objPromo->attributes = $_POST['PromoCode']; $objPromo->setScenario('create'); if ($objPromo->validate()) { if ($objPromo->save()) { echo CJSON::encode(array('result' => 'success')); } } else { echo CJSON::encode(array('result' => 'failure', 'errors' => $objPromo->getErrors())); } } }
/** * Apply a promo code to the cart and return an array indicating what happened. * @param string $strPromoCode The promocode. * @return array An array indicating what happened. * ['success'] boolean Whether the promo code was applied. * ['action'] string Recommended action: alert|error|triggerCalc|success. * ['message'] string A message to display. */ protected function applyPromoCodeModal($strPromoCode) { if (Yii::app()->shoppingcart->PromoCode !== null) { return array('success' => false, 'action' => 'alert', 'message' => Yii::t('global', 'Only one promo code can be applied')); } $objPromoCode = new PromoCode(); $objPromoCode->code = $strPromoCode; $objPromoCode->setScenario('checkout'); if ($objPromoCode->validate() === false) { $arrErrors = $objPromoCode->getErrors(); return array('success' => false, 'action' => 'error', 'message' => $arrErrors['code'][0]); } $objPromoCode = PromoCode::LoadByCode($strPromoCode); Yii::app()->shoppingcart->applyPromoCode($objPromoCode); // See if this promo code is supposed to turn on free shipping. // This runs AFTER validate() so if we get here, it means that any // criteria have passed. So just apply and refresh the shipping list. if ($objPromoCode->Shipping) { // Update the shipping selection to use the free shipping module. $objFreeShipping = Modules::model()->freeshipping()->find(); if ($objFreeShipping !== null) { $checkoutForm = MultiCheckoutForm::loadFromSession(); if ($checkoutForm !== null) { try { $arrCartScenario = Shipping::getCartScenarios($checkoutForm); } catch (Exception $e) { $arrCartScenario = null; } if ($arrCartScenario !== null) { $freeShippingScenario = findWhere($arrCartScenario, array('providerId' => $objFreeShipping->id)); $checkoutForm = MultiCheckoutForm::loadFromSessionOrNew(); $checkoutForm->shippingProvider = $freeShippingScenario['providerId']; $checkoutForm->shippingPriority = $freeShippingScenario['priorityLabel']; MultiCheckoutForm::saveToSession($checkoutForm); } } } return array('success' => true, 'action' => 'triggerCalc', 'message' => Yii::t('global', 'Congratulations! This order qualifies for Free Shipping!')); } return array('success' => true, 'action' => 'success'); }