示例#1
0
 /**
  *  4. Select shipping methods
  *	@role login
  */
 public function shipping()
 {
     $shipments = $this->order->getShipments();
     $this->restoreShippingMethodSelection();
     if ($redirect = $this->validateOrder($this->order, self::STEP_SHIPPING)) {
         return $redirect;
     }
     if (!$this->order->isShippingRequired()) {
         return new ActionRedirectResponse('checkout', 'pay');
     }
     foreach ($shipments as $shipment) {
         if (count($shipment->getItems()) == 0) {
             $shipment->delete();
             $shipments->removeRecord($shipment);
         }
     }
     $form = $this->buildShippingForm($shipments);
     $needSelecting = null;
     foreach ($shipments as $key => $shipment) {
         if (!$shipment->isShippable()) {
             $download = $shipment;
             $downloadIndex = $key;
             $needSelecting = false;
             continue;
         }
         $shipmentRates = $shipment->getAvailableRates();
         if ($shipmentRates->size() > 1) {
             $needSelecting = true;
         } else {
             if (!$shipmentRates->size()) {
                 $validator = $this->buildAddressSelectorValidator($this->order, 'shipping');
                 $validator->triggerError('selectedAddress', $this->translate('_err_no_rates_for_address'));
                 $validator->saveState();
                 return new ActionRedirectResponse('checkout', 'selectAddress');
             } else {
                 $shipment->setRateId($shipmentRates->get(0)->getServiceId());
                 if ($this->order->isMultiAddress->get()) {
                     $shipment->save();
                 }
             }
         }
         $rates[$key] = $shipmentRates;
         if ($shipment->getSelectedRate()) {
             $form->set('shipping_' . $key, $shipment->getSelectedRate()->getServiceID());
         }
     }
     SessionOrder::save($this->order);
     // only one shipping method for each shipment, so we pre-select it automatically
     if (is_null($needSelecting) && $this->config->get('SKIP_SHIPPING') && $this->config->get('CHECKOUT_CUSTOM_FIELDS') != 'SHIPPING_METHOD_STEP') {
         $this->order->serializeShipments();
         SessionOrder::save($this->order);
         return new ActionRedirectResponse('checkout', 'pay');
     }
     $rateArray = array();
     foreach ((array) $rates as $key => $rate) {
         $rateArray[$key] = $rate->toArray();
     }
     $response = new ActionResponse();
     $shipmentArray = $shipments->toArray();
     if (isset($download)) {
         $response->set('download', $download->toArray());
         unset($shipmentArray[$downloadIndex]);
     }
     $locale = self::getApplication()->getLocale();
     foreach ($rateArray as &$rates) {
         foreach ($rates as $k => &$item) {
             if (!empty($item['ShippingService']['deliveryTimeMinDays'])) {
                 $item['ShippingService']['formatted_deliveryTimeMinDays'] = $locale->getFormattedTime(strtotime('+' . $item['ShippingService']['deliveryTimeMinDays'] . ' days'));
             }
             if (!empty($item['ShippingService']['deliveryTimeMaxDays'])) {
                 $item['ShippingService']['formatted_deliveryTimeMaxDays'] = $locale->getFormattedTime(strtotime('+' . $item['ShippingService']['deliveryTimeMaxDays'] . ' days'));
             }
         }
     }
     unset($item);
     $recurringIDs = array();
     $recurringPlans = array();
     foreach ($shipmentArray as $item) {
         foreach ($item['Order']['cartItems'] as $orderedItem) {
             if (isset($orderedItem['recurringID'])) {
                 $recurringIDs[] = $orderedItem['recurringID'];
             }
         }
     }
     if (count($recurringIDs)) {
         $this->loadLanguageFile('Product');
         // contains translations for recurring product pricing.
         ClassLoader::import('application.model.product.RecurringProductPeriod');
         $recurringPlans = RecurringProductPeriod::getRecordSetArrayByIDs($recurringIDs);
         $response->set('periodTypesPlural', RecurringProductPeriod::getAllPeriodTypes(RecurringProductPeriod::PERIOD_TYPE_NAME_PLURAL));
         $response->set('periodTypesSingle', RecurringProductPeriod::getAllPeriodTypes(RecurringProductPeriod::PERIOD_TYPE_NAME_SINGLE));
     }
     $response->set('shipments', $shipmentArray);
     $response->set('rates', $rateArray);
     $response->set('recurringPlans', $recurringPlans);
     $response->set('currency', $this->getRequestCurrency());
     $response->set('form', $form);
     $response->set('order', $this->order->toArray());
     $this->order->getSpecification()->setFormResponse($response, $form);
     $this->order->setCheckoutStep(CustomerOrder::CHECKOUT_ADDRESS);
     return $response;
 }