/** * Run the widget. Renders the shipping estimator lines on the page. */ public function run() { // Required assets. $assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets', false, -1, true); Yii::app()->clientScript->registerScriptFile($assets . '/js/WsShippingEstimator.js'); $pathToZippo = Yii::getPathOfAlias('ext') . DIRECTORY_SEPARATOR . 'wsadvcheckout' . DIRECTORY_SEPARATOR . 'assets'; $assets = Yii::app()->getAssetManager()->publish($pathToZippo, false, -1, true); Yii::app()->clientScript->registerScriptFile($assets . '/zippo.js'); $checkoutForm = MultiCheckoutForm::loadFromSessionOrNew(); // We may wish to update the shipping options right away if we know the // cart has changed. $updateOnLoad = false; if ($this->updateShippingOptions) { // This check for shippingCountry being null is a workaround to fix // WS-3180. When shippingCountry is null, we need to update the // shipping estimates *now* because they will not be updated by the // JavaScript (since the JavaScript in WsShippingEstimator requires // country to be set). The reason we need to do this is because // shippingCountry may be null when in-store pickup has been // chosen. // TODO: Fix this in WsShippingEstimator and remove this workaround. if (empty($checkoutForm->shippingCountry) || empty($checkoutForm->shippingPostal)) { Shipping::updateCartScenariosInSession(); } else { $updateOnLoad = true; } } // Use the shipping scenarios and shipping address in the session. $arrCartScenario = Shipping::loadCartScenariosFromSession(); $wsShippingEstimatorOptions = self::getShippingEstimatorOptions($arrCartScenario, $checkoutForm->shippingProvider, $checkoutForm->shippingPriority, $checkoutForm->shippingCity, $checkoutForm->shippingStateCode, $checkoutForm->shippingCountryCode, $updateOnLoad); $selectedCartScenario = Shipping::getSelectedCartScenarioFromSession(); if ($selectedCartScenario !== null) { $formattedShippingPrice = $selectedCartScenario['formattedShippingPrice']; $formattedCartTax = $selectedCartScenario['formattedCartTax']; } else { $formattedShippingPrice = null; $formattedCartTax = null; } $this->render('_shippingestimator', array('countries' => CHtml::listData(Country::getShippingCountries(), 'code', 'country'), 'formattedShippingPrice' => $formattedShippingPrice, 'formattedCartTax' => $formattedCartTax, 'shippingCountryCode' => $wsShippingEstimatorOptions['shippingCountryCode'], 'shippingCountryName' => $wsShippingEstimatorOptions['shippingCountryName'], 'shippingPostal' => $checkoutForm->shippingPostal, 'wsShippingEstimatorOptions' => CJSON::encode($wsShippingEstimatorOptions), 'cssClass' => self::CSS_CLASS)); }
/** * Update the cart shipping (xlsws_cart_shipping) based on selected * shipping scenario from the session. Before calling this make sure that * the shipping scenarios in the session are up to date and the * checkoutForm in the session uses the desired providerId and * priorityLabel. * * If no cart shipping already exists, one will be created. * If an error occurs it will be added to $this->errors in the Yii's model error format. * @See CModel::getErrors(). * * @return bool true if the shipping was updated, false otherwise. */ public function updateShipping() { $selectedCartScenario = Shipping::getSelectedCartScenarioFromSession(); if ($selectedCartScenario === null) { Yii::log('Cannot update shipping, no scenario selected', 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return false; } Yii::log("Shipping Product " . $selectedCartScenario['shippingProduct'], 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); // Populate the shipping object with default data. $this->shipping_method = $selectedCartScenario['shippingProduct']; $this->shipping_module = $selectedCartScenario['module']; $this->shipping_data = $selectedCartScenario['shippingLabel']; $this->shipping_cost = $selectedCartScenario['shippingPrice']; $this->shipping_sell = $selectedCartScenario['shippingPrice']; $this->shipping_sell_taxed = $selectedCartScenario['shippingPriceWithTax']; $this->shipping_taxable = Yii::app()->params['SHIPPING_TAXABLE'] == '1' ? 1 : 0; if ($this->save() === false) { Yii::log("Error saving Cart Shipping:\n" . print_r($this->getErrors()), 'error', 'application.' . __CLASS__ . '.' . __FUNCTION__ . '.' . __LINE__); return false; } Yii::app()->shoppingcart->shipping_id = $this->id; Yii::app()->shoppingcart->recalculateAndSave(); return true; }
/** * Return the options required by ConfirmationShippingEstimator.js * which is registered anytime the confirmation page is rendered * * @return array */ private function _getShippingEstimatorOptions() { $selectedCartScenario = Shipping::getSelectedCartScenarioFromSession(); $wsShippingEstimatorOptions = WsShippingEstimator::getShippingEstimatorOptions(array($selectedCartScenario), $this->checkoutForm->shippingProvider, $this->checkoutForm->shippingPriority, $this->checkoutForm->shippingCity, $this->checkoutForm->shippingStateCode, $this->checkoutForm->shippingCountryCode); $wsShippingEstimatorOptions['redirectToShippingOptionsUrl'] = Yii::app()->getController()->createUrl('shippingoptions'); return $wsShippingEstimatorOptions; }