/** * Updates the cart scenarios stored in the session. * * @return void * @see Shipping::getCartScenarios. */ public static function updateCartScenariosInSession() { // If we already have shipping details in the session we can try to // update the cart scenarios. $checkoutForm = MultiCheckoutForm::loadFromSession(); if ($checkoutForm === null) { $arrCartScenario = null; } else { // Save shipping options and rates to session. try { $arrCartScenario = Shipping::getCartScenarios($checkoutForm); } catch (Exception $e) { // TODO: We should probably execute this block if $arrCartScenario is an empty array as well. Yii::log('Unable to get cart scenarios: ' . $e->getMessage(), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); // If there are no valid cart scenarios we can deselect whatever // was previously selected. // TODO: We should possibly do this if the newly update cart // scenarios don't include the previously selected one. $checkoutForm->shippingProvider = null; $checkoutForm->shippingPriority = null; MultiCheckoutForm::saveToSession($checkoutForm); // Remove any previously stored cart scenarios. $arrCartScenario = null; } } // Save the updated rates back to the session. Shipping::saveCartScenariosToSession($arrCartScenario); }
/** * 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'); }