示例#1
0
 private function getCartPageResponse()
 {
     $this->addBreadCrumb($this->translate('_my_session'), $this->router->createUrlFromRoute($this->request->get('return'), true));
     $this->order->setUser($this->user);
     $this->order->loadItemData();
     $response = new ActionResponse();
     if ($result = $this->order->updateToStock()) {
         $response->set('changes', $result);
     }
     $options = $this->getItemOptions();
     $currency = Currency::getValidInstanceByID($this->request->get('currency', $this->application->getDefaultCurrencyCode()), Currency::LOAD_DATA);
     $form = $this->buildCartForm($this->order, $options);
     if ($this->isTosInCartPage()) {
         $form->set('tos', $this->session->get('tos'));
     }
     if ($this->config->get('ENABLE_SHIPPING_ESTIMATE')) {
         $this->loadLanguageFile('User');
         if ($this->estimateShippingCost()) {
             //$this->order->getTotal(true);
             $response->set('isShippingEstimated', true);
         }
         $address = $this->order->shippingAddress->get();
         foreach (array('countryID' => 'country', 'stateName' => 'state_text', 'postalCode' => 'postalCode', 'city' => 'city') as $addressKey => $formKey) {
             $form->set('estimate_' . $formKey, $address->{$addressKey}->get());
         }
         if ($address->state->get() && $address->state->get()->getID()) {
             $form->set('estimate_state_select', $address->state->get()->getID());
         }
         $response->set('countries', $this->getCountryList($form));
         $response->set('states', $this->getStateList($form->get('estimate_country')));
         $hideConf = (array) $this->config->get('SHIP_ESTIMATE_HIDE_ENTRY');
         $hideForm = !empty($hideConf['UNREGISTERED']) && $this->user->isAnonymous() || !empty($hideConf['ALL_REGISTERED']) && !$this->user->isAnonymous() || !empty($hideConf['REGISTERED_WITH_ADDRESS']) && !$this->user->isAnonymous() && !$this->user->defaultBillingAddress->get() || !$this->order->isShippingRequired() || $this->order->isMultiAddress->get();
         $response->set('hideShippingEstimationForm', $hideForm);
     }
     $orderArray = $this->order->toArray();
     $itemsById = array();
     foreach (array('cartItems', 'wishListItems') as $type) {
         if (!empty($orderArray[$type])) {
             foreach ($orderArray[$type] as &$item) {
                 $itemsById[$item['ID']] =& $item;
             }
         }
     }
     $response->set('cart', $orderArray);
     $response->set('itemsById', $itemsById);
     $response->set('form', $form);
     $response->set('return', $this->request->get('return'));
     $response->set('currency', $currency->getID());
     $response->set('options', $options['visible']);
     $response->set('moreOptions', $options['more']);
     $response->set('orderTotal', $currency->getFormattedPrice($this->order->getTotal()));
     $response->set('expressMethods', $this->application->getExpressPaymentHandlerList(true));
     $response->set('isCouponCodes', DiscountCondition::isCouponCodes());
     $response->set('isOnePageCheckout', $this->config->get('CHECKOUT_METHOD') == 'CHECKOUT_ONEPAGE' && !$this->order->isMultiAddress->get() && !$this->session->get('noJS'));
     $this->order->getSpecification()->setFormResponse($response, $form);
     SessionOrder::getOrder()->getShoppingCartItems();
     return $response;
 }
示例#2
0
 private function addProductToCart($id, $prefix = '')
 {
     if ($prefix && !$this->request->get($prefix . 'count')) {
         return '"';
     }
     $product = Product::getInstanceByID($id, true, array('Category'));
     $productRedirect = new ActionRedirectResponse('product', 'index', array('id' => $product->getID(), 'query' => 'return=' . $this->request->get('return')));
     if (!$product->isAvailable()) {
         $productController = new ProductController($this->application);
         $productController->setErrorMessage($this->translate('_product_unavailable'));
         return $productRedirect;
     }
     $variations = !$product->parent->get() ? $product->getVariationData($this->application) : array();
     ClassLoader::import('application.controller.ProductController');
     // add first variations by default?
     $autoVariation = false;
     if ($variations && $this->config->get('DEF_FIRST_VARIATION')) {
         $first = reset($variations['variations']);
         if (!$this->request->get($prefix . 'variation_' . $first['ID'])) {
             $autoVariation = true;
         }
     }
     $validator = ProductController::buildAddToCartValidator($product->getOptions(true)->toArray(), $autoVariation ? array() : $variations, $prefix);
     if (!$validator->isValid()) {
         return $productRedirect;
     }
     // check if a variation needs to be added to cart instead of a parent product
     if ($variations) {
         if ($autoVariation) {
             $foundVariation = false;
             foreach ($variations['products'] as $prod) {
                 if (Product::isOrderable($prod)) {
                     $product = Product::getInstanceByID($prod['ID'], true);
                     $foundVariation = true;
                     break;
                 }
             }
             if (!$foundVariation) {
                 return $productRedirect;
             }
         } else {
             $product = $this->getVariationFromRequest($variations);
         }
     }
     $count = $this->request->get($prefix . 'count', 1);
     if ($count < $product->getMinimumQuantity()) {
         $count = $product->getMinimumQuantity();
     }
     $item = $this->order->addProduct($product, $count);
     if ($item instanceof OrderedItem) {
         $item->name->set($product->name->get());
         foreach ($product->getOptions(true) as $option) {
             $this->modifyItemOption($item, $option, $this->request, $prefix . 'option_' . $option->getID());
         }
         if ($this->order->isMultiAddress->get()) {
             $item->save();
         }
         if ($product->type->get() == Product::TYPE_RECURRING) {
             if ($item->isExistingRecord() == false) {
                 $item->save();
                 // or save in SessionOrder::save()
             }
             $recurringID = $this->getRequest()->get('recurringID');
             $recurringProductPeriod = $product->getRecurringProductPeriodById($recurringID);
             if ($recurringProductPeriod == null) {
                 $recurringProductPeriod = $product->getDefaultRecurringProductPeriod();
             }
             if ($recurringProductPeriod) {
                 $instance = RecurringItem::getNewInstance($recurringProductPeriod, $item);
                 $instance->save();
                 $item->updateBasePriceToCalculatedPrice();
             }
             // what if product with type recurring but no plan? just ignore?
         }
     }
     $this->order->updateToStock(false);
     return $item;
 }