Beispiel #1
0
 public function product()
 {
     // Update the cart ONLY if $this->cookies are available, in order to avoid ghost carts created by bots
     if (!$this->context) {
         $this->context = JeproshopContext::getContext();
     }
     if ($this->context->cookie->exists() && !$this->has_errors && !($this->context->customer->isLogged() && !$this->isTokenValid())) {
         // Send noindex to avoid ghost carts by bots
         header("X-Robots-Tag: noindex, nofollow", true);
         if (!$this->isInitialized()) {
             $this->initialize();
         }
         $app = JFactory::getApplication();
         $product_id = $app->input->get('product_id', null);
         $customization_id = (int) $app->input->get('customization_id', null);
         $address_delivery_id = (int) $app->input->get('address_delivery_id', null);
         $product_attribute_id = $app->input->get('product_attribute_id', null);
         $mode = $app->input->get('task') == 'update' && $product_id ? 'update' : 'add';
         if ($app->input->get('quantity') == 0) {
             $this->has_errors = true;
             echo JText::_('COM_JEPROSHOP_NULL_QUANTITY_MESSAGE') . ' ' . !$app->input->get('use_ajax');
         } elseif (!$product_id) {
             $this->has_errors = true;
             echo JText::_('COM_JEPROSHOP_PRODUCT_ID_NOT_FOUND_MESSAGE') . ' ' . !$app->input->get('use_ajax');
         }
         $product = new JeproshopProductModelProduct($product_id, true, $this->context->language->lang_id);
         if (!$product->product_id || !$product->published) {
             $this->has_errors = true;
             echo JText::_('COM_JEPROSHOP_THIS_PRODUCT_IS_NO_LONGER_AVAILABLE_MESSAGE.') . ' ' . !$app->input->get('use_ajax');
             exit;
         }
         $quantity = abs($app->input->get('quantity', 1));
         $qty_to_check = $quantity;
         $cart_products = $this->context->cart->getProducts();
         if (is_array($cart_products)) {
             foreach ($cart_products as $cart_product) {
                 if ((!isset($this->product_attribute_id) || $cart_product->product_attribute_id == $product_attribute_id) && (isset($product_id) && $cart_product->product_id == $product_id)) {
                     $qty_to_check = $cart_product->cart_quantity;
                     if ($app->input->get('op', 'up') == 'down') {
                         $qty_to_check -= $quantity;
                     } else {
                         $qty_to_check += $quantity;
                     }
                     break;
                 }
             }
         }
         // Check product quantity availability
         if ($product_attribute_id) {
             if (!JeproshopProductModelProduct::isAvailableWhenOutOfStock($product->out_of_stock) && !JeproshopAttributeModelAttribute::checkAttributeQty($product_attribute_id, $qty_to_check)) {
                 $this->has_errors = true;
                 echo JText::_('COM_JEPROSHOP_THERE_IS_NOT_ENOUGH_PRODUCT_IN_STOCK_MESSAGE') . ' ' . __LINE__ . !$app->input->get('use_ajax');
             }
         } elseif ($product->hasAttributes()) {
             $minimumQuantity = $product->out_of_stock == 2 ? !JeproshopSettingModelSetting::getValue('order_out_of_stock') : !$product->out_of_stock;
             $product_attribute_id = JeproshopProductModelProduct::getDefaultAttribute($product->product_id, $minimumQuantity);
             // @todo do something better than a redirect admin !!
             if (!$product_attribute_id) {
                 $app->redirect($this->getProductLink($product));
             } elseif (!JeproshopProductModelProduct::isAvailableWhenOutOfStock($product->out_of_stock) && !JeproshopAttributeModelAttribute::checkAttributeQty($product_attribute_id, $qty_to_check)) {
                 $this->has_errors = true;
                 echo JText::_('COM_JEPROSHOP_THERE_IS_NOT_ENOUGH_PRODUCT_IN_STOCK_MESSAGE') . ' ' . __LINE__ . !$app->input->get('use_ajax');
             }
         } elseif (!$product->checkQuantity($qty_to_check)) {
             $this->has_errors = true;
             echo JText::_('COM_JEPROSHOP_THERE_IS_NOT_ENOUGH_PRODUCT_IN_STOCK_MESSAGE') . ' ' . __LINE__ . !$app->input->get('use_ajax');
         }
         // If no errors, process product addition
         if ($mode == 'add') {
             // Add cart if no cart found
             if (!$this->context->cart->cart_id) {
                 if (JeproshopContext::getContext()->cookie->guest_id) {
                     $guest = new JeproshopGuestModelGuest(JeproshopContext::getContext()->cookie->guest_id);
                     $this->context->cart->mobile_theme = $guest->mobile_theme;
                 }
                 $this->context->cart->add();
                 if ($this->context->cart->cart_id) {
                     $this->context->cookie->cart_id = (int) $this->context->cart->cart_id;
                 }
             }
             // Check customizable fields
             if (!$product->hasAllRequiredCustomizableFields() && !$customization_id) {
                 // $this->errors[] = Tools::displayError('Please fill in all of the required fields, and then save your customizations.', !Tools::getValue('ajax'));
             }
             if (!$this->has_errors) {
                 $cart_rules = $this->context->cart->getCartRules();
                 $update_quantity = $this->context->cart->updateQuantity($quantity, $product_id, $product_attribute_id, $customization_id, $app->input->get('op', 'up'), $address_delivery_id);
                 if ($update_quantity < 0) {
                     // If product has attribute, minimal quantity is set with minimal quantity of attribute
                     $minimal_quantity = $product_attribute_id ? JeproshopAttributeModelAttribute::getAttributeMinimalQty($product_attribute_id) : $product->minimal_quantity;
                     $this->has_errors = true;
                     sprintf(Tools::displayError('You must add %d minimum quantity', !$app->input->get('use_ajax')), $minimal_quantity);
                 } elseif (!$update_quantity) {
                     $this->has_errors = true;
                     echo JText::_('COM_JEPROSHOP_YOU_ALREADY_HAVE_THE_MAXIMUM_AVAILABLE_FOR_THIS_PRODUCT_MESSAGE') . ' ' . !$app->input->get('use_ajax');
                 } elseif ((int) $app->input->get('allow_refresh')) {
                     // If the cart rules has changed, we need to refresh the whole cart
                     $cart_rules2 = $this->context->cart->getCartRules();
                     if (count($cart_rules2) != count($cart_rules)) {
                         $this->ajax_refresh = true;
                     } else {
                         $rule_list = array();
                         foreach ($cart_rules2 as $rule) {
                             $rule_list[] = $rule->cart_rule_id;
                         }
                         foreach ($cart_rules as $rule) {
                             if (!in_array($rule->cart_rule_id, $rule_list)) {
                                 $this->ajax_refresh = true;
                                 break;
                             }
                         }
                     }
                 }
             }
         }
         $removed = JeproshopCartRuleModelCartRule::autoRemoveFromCart();
         JeproshopCartRuleModelCartRule::autoAddToCart();
         if (count($removed) && (int) $app->input->get('allow_refresh')) {
             $this->ajax_refresh = true;
         }
         echo 'bonjour';
         exit;
     } elseif (!$this->context->cookie->exists()) {
         echo 'bonjour cookkie';
         exit;
     } elseif ($this->has_errors) {
     } elseif ($this->context->customer->isLogged() && !$this->isTokenValid()) {
     }
 }
Beispiel #2
0
 public function initialize()
 {
     if (self::$initialized) {
         return;
     }
     self::$initialized = true;
     $app = JFactory::getApplication();
     $context = JeproshopContext::getContext();
     $view = $app->input->get('view', 'default');
     $viewClass = $this->getView($view, JFactory::getDocument()->getType());
     if (JeproshopTools::usingSecureMode()) {
         $this->ssl_enabled = true;
     }
     if (isset($context->cookie->account_created)) {
         $accountCreated = true;
         $viewClass->assignRef('account_created', $accountCreated);
         $context->cookie->account_created = 0;
     }
     JeproshopTools::setCookieLanguage($context->cookie);
     $cart_id = (int) $this->recoverCart();
     if ($cart_id) {
         $context->cookie->cart_id = $cart_id;
     }
     if ($this->authenticated && !$context->customer->isLogged($this->guest_allowed)) {
         $app->redirect('index.php?option=com_jeproshop&view=authentication');
         // todo add retun option
     }
     if (JeproshopSettingModelSetting::getValue('enable_geolocation')) {
         $defaultCountry = $this->geolocationManagement($context->country);
         if ($defaultCountry && JeproshopTools::isLoadedObject($defaultCountry, 'country_id')) {
             $context->country = $defaultCountry;
         }
     }
     $currency = JeproshopTools::setCurrency($context->cookie);
     $logout = $app->input->get('logout');
     $myLogout = $app->input->get('mylogout');
     if (isset($logout) || $context->customer->logged && JeproshopCustomerModelCustomer::isBanned($context->cutomer->customer_id)) {
         $context->customer->logout();
         //$app->input->get('')
     } elseif (isset($myLogout)) {
         $context->customer->mylogout();
     }
     if ((int) $context->cookie->cart_id) {
         $cart = new JeproshopCartModelCart($context->cookie->cart_id);
         if ($cart->orderExists()) {
             $context->cookie->cart_id = null;
             $context->cookie->check_selling_condition = false;
         } elseif ((int) JeproshopSettingModelSetting::getValue('enable_geolocation') && !in_array(strtoupper($context->cookie->iso_code_country), explode(';', JeproshopSettingModelSetting::getValue('allowed_countries'))) && $cart->numberOfProducts() && (int) JeproshopSettingModelSetting::getValue('geolocation_behavior') != -1 && !self::isInWhiteListForGeolocation() && !in_array($_SERVER['SERVER_NAME'], array('localhost', '127.0.0.1'))) {
             $context->cookie->cart_id = null;
             $cart = null;
         } elseif ($context->cookie->customer_id != $cart->customer_id || $context->cookie->lang_id != $cart->lang_id || $currency->currency_id != $cart->currency_id) {
             if ($context->cookie->customer_id) {
                 $cart->customer_id = (int) $context->cookie->customer_id;
             }
             $cart->lang_id = (int) $context->cookie->lang_id;
             $cart->currency_id = (int) $currency->currency_id;
             $cart->update();
         }
         if (isset($cart) && (!isset($cart->address_delivery_id) || $cart->address_delivery_id == 0 || !isset($cart->address_invoice_id) || $cart->address_invoice_id) && $context->cookie->customer_id) {
             $toUpdate = false;
             if (!isset($cart->address_delivery_id) || $cart->address_delivery_id == 0) {
                 $toUpdate = true;
                 $cart->address_delivery_id = (int) JeproshopAddressModelAddress::getCustomerFirstAddressId($cart->customer_id);
             }
             if (!isset($cart->address_invoice_id) || $cart->address_invoice_id == 0) {
                 $toUpdate = true;
                 $cart->address_invoice_id = (int) JeproshopAddressModelAddress::getCustomerFirstAddressId($cart->customer_id);
             }
             if ($toUpdate) {
                 $cart->update();
             }
         }
     }
     if (!isset($cart) || $cart->cart_id) {
         $cart = new JeproshopCartModelCart();
         $cart->lang_id = (int) $context->cookie->lang_id;
         $cart->currency_id = (int) $context->cookie->currency_id;
         $cart->guest_id = (int) $context->cookie->guest_id;
         $cart->shop_group_id = (int) $context->shop->shop_group_id;
         $cart->shop_id = $context->shop->shop_id;
         if ($context->cookie->customer_id) {
             $cart->customer_id = (int) $context->cookie->id_customer;
             $cart->address_delivery_id = (int) JeproshopAddressModelAddress::getCustomerFirstAddressId($cart->customer_id);
             $cart->address_invoice_id = $cart->address_delivery_id;
         } else {
             $cart->address_delivery_id = 0;
             $cart->address_invoice_id = 0;
         }
         // Needed if the merchant want to give a free product to every visitors
         $context->cart = $cart;
         JeproshopCartRuleModelCartRule::autoAddToCart($context);
     } else {
         $context->cart = $cart;
     }
     JeproshopProductModelProduct::initPricesComputation();
     $display_tax_label = $context->country->display_tax_label;
     if (isset($cart->{JeproshopSettingModelSetting::getValue('tax_address_type')}) && $cart->{JeproshopSettingModelSetting::getValue('tax_address_type')}) {
         $info = JeproshopAddressModelAddress::getCountryAndState($cart->{JeproshopSettingModelSetting::getValue('tax_address_type')});
         $country = new JeproshopCountryModelCountry((int) $info->country_id);
         $context->country = $country;
         if (JeproshopTools::isLoadedObject($country, 'country_id')) {
             $display_tax_label = $country->display_tax_label;
         }
     }
     $languages = JeproshopLanguageModelLanguage::getLanguages(true);
     $meta_language = array();
     foreach ($languages as $lang) {
         $meta_language[] = $lang->iso_code;
     }
     $compared_products = array();
     $comparatorMaxItem = JeproshopSettingModelSetting::getValue('comparator_max_item');
     if ($comparatorMaxItem && isset($context->cookie->compare_id)) {
         $compared_products = JeproshopProductComparedModelProductCompared::getComparedProducts($context->cookie->compare_id);
     }
     $mobileDevice = $context->getMobileDevice();
     $viewClass->assignRef('mobile_device', $mobileDevice);
     $viewClass->assignRef('cart', $cart);
     $viewClass->assignRef('currency', $currency);
     $viewClass->assignRef('display_tax_label', $display_tax_label);
     $isLogged = (bool) $context->customer->isLogged();
     $viewClass->assignRef('is_logged', $isLogged);
     $isGuest = (bool) $context->customer->isGuest();
     $viewClass->assignRef('is_guest', $isGuest);
     $priceRoundMode = JeproshopSettingModelSetting::getValue('price_round_mode');
     $viewClass->assignRef('price_round_mode', $priceRoundMode);
     $useTax = JeproshopSettingModelSetting::getValue('use_tax');
     $viewClass->assignRef('use_taxes', $useTax);
     $showTax = (int) JeproshopSettingModelSetting::getValue('display_tax') == 1 && JeproshopSettingModelSetting::getValue('use_tax');
     $viewClass->assignRef('show_tax', $showTax);
     $catalogMode = (bool) JeproshopSettingModelSetting::getValue('catalog_mode') || !JeproshopGroupModelGroup::getCurrent()->show_prices;
     $viewClass->assignRef('catalog_mode', $catalogMode);
     $enableB2bMode = (bool) JeproshopSettingModelSetting::getValue('enable_b2b_mode');
     $viewClass->assignRef('enable_b2b_mode', $enableB2bMode);
     $stockManagement = JeproshopSettingModelSetting::getValue('stock_management');
     $viewClass->assignRef('stock_management', $stockManagement);
     $metaLanguages = implode(',', $meta_language);
     $viewClass->assignRef('meta_languages', $metaLanguages);
     $viewClass->assignRef('languages', $languages);
     $numberOfProducts = $cart->numberOfProducts();
     $viewClass->assignRef('cart_quantities', $numberOfProducts);
     $currencies = JeproshopCurrencyModelCurrency::getCurrencies();
     $viewClass->assignRef('currencies', $currencies);
     $comparatorMaxItem = JeproshopSettingModelSetting::getValue('comparator_max_item');
     $viewClass->assignRef('comparator_max_item', $comparatorMaxItem);
     $quickView = (bool) JeproshopSettingModelSetting::getValue('quick_view');
     $viewClass->assignRef('quick_view', $quickView);
     $restrictedCountryMode = false;
     $viewClass->assignRef('restricted_country_mode', $restrictedCountryMode);
     $displayPrice = JeproshopProductModelProduct::getTaxCalculationMethod((int) $context->cookie->customer_id);
     $viewClass->assignRef('display_price', $displayPrice);
     /*$viewClass->assignRef('');
       $viewClass->assignRef('');
       $viewClass->assignRef('');*/
     $viewClass->assignRef('compared_products', $compared_products);
     /*$viewClass->assignRef('comparator_max_item', $comparatorMaxItem); */
 }