/** * 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); }
/** * This is the method used by the advanced cart and checkout to retrieve * shipping options and their rates for the shipping estimator. * @return string JSON encoded array of shipping options ordered by price. */ public function actionGetShippingRates() { $checkoutForm = MultiCheckoutForm::loadFromSessionOrNew(); // If no CheckoutForm is posted, then the checkoutForm stored in the // session is used. This is completely valid when we want updated // rates, but haven't modified (or perhaps don't know) the shipping // address. if (isset($_POST['CheckoutForm'])) { $checkoutForm->attributes = $_POST['CheckoutForm']; } // Transform the postcode as required by the shipping modules. // TODO Can we move this? $checkoutForm->shippingPostal = strtoupper(str_replace(' ', '', $checkoutForm->shippingPostal)); // Minimal requirements for shipping: just address details. $checkoutForm->scenario = 'MinimalShipping'; if ($checkoutForm->validate() === false) { return $this->renderJSON(array('result' => 'error', 'errors' => $checkoutForm->getErrors())); } try { $arrCartScenario = Shipping::getCartScenarios($checkoutForm); } catch (Exception $e) { return $this->renderJSON(array('result' => 'error', 'errormsg' => $e->getMessage())); } // Get the shipping estimator JavaScript options. $wsShippingEstimatorOptions = WsShippingEstimator::getShippingEstimatorOptions($arrCartScenario, $checkoutForm->shippingProvider, $checkoutForm->shippingPriority, $checkoutForm->shippingCity, $checkoutForm->shippingStateCode, $checkoutForm->shippingCountryCode); $shippingEstimatorMessage = findWhere($wsShippingEstimatorOptions['messages'], array('code' => 'WARN')); if ($shippingEstimatorMessage !== null) { $message = Yii::t('checkout', 'Your previous shipping selection is no longer available. Please choose an available shipping option.'); Yii::app()->user->setFlash('error', $message); } // Save to session. Shipping::saveCartScenariosToSession($arrCartScenario); MultiCheckoutForm::saveToSession($checkoutForm); // Save to the database. $objShipping = CartShipping::getOrCreateCartShipping(); $objShipping->updateShipping(); return $this->renderJSON(array('result' => 'success', 'wsShippingEstimatorOptions' => $wsShippingEstimatorOptions, 'taxModeChangedMessage' => Yii::app()->user->getFlash('taxModeChange', null))); }