Beispiel #1
0
 public function getDeliveryOptionList(JeproshopCountryModelCountry $default_country = null, $flush = false)
 {
     static $cache = null;
     if ($cache !== null && !$flush) {
         return $cache;
     }
     $delivery_option_list = array();
     $carriers_price = array();
     $carrier_collection = array();
     $package_list = $this->getPackageList();
     // Foreach addresses
     foreach ($package_list as $address_id => $packages) {
         // Initialize vars
         $delivery_option_list[$address_id] = array();
         $carriers_price[$address_id] = array();
         $common_carriers = null;
         $best_price_carriers = array();
         $best_grade_carriers = array();
         $carriers_instance = array();
         // Get country
         if ($address_id) {
             $address = new JeproshopAddressModelAddress($address_id);
             $country = new JeproshopCountryModelCountry($address->country_id);
         } else {
             $country = $default_country;
         }
         // Foreach packages, get the carriers with best price, best position and best grade
         foreach ($packages as $package_id => $package) {
             // No carriers available
             if (count($package['carrier_list']) == 1 && current($package['carrier_list']) == 0) {
                 $cache = array();
                 return $cache;
             }
             $carriers_price[$address_id][$package_id] = array();
             // Get all common carriers for each packages to the same address
             if (is_null($common_carriers)) {
                 $common_carriers = $package['carrier_list'];
             } else {
                 $common_carriers = array_intersect($common_carriers, $package['carrier_list']);
             }
             $best_price = null;
             $best_price_carrier = null;
             $best_grade = null;
             $best_grade_carrier = null;
             // Foreach carriers of the package, calculate his price, check if it the best price, position and grade
             foreach ($package['carrier_list'] as $carrier_id) {
                 if (!isset($carriers_instance[$carrier_id])) {
                     $carriers_instance[$carrier_id] = new JeproshopCarrierModelCarrier($carrier_id);
                 }
                 $price_with_tax = $this->getPackageShippingCost($carrier_id, true, $country, $package['product_list']);
                 $price_without_tax = $this->getPackageShippingCost($carrier_id, false, $country, $package['product_list']);
                 if (is_null($best_price) || $price_with_tax < $best_price) {
                     $best_price = $price_with_tax;
                     $best_price_carrier = $carrier_id;
                 }
                 $carriers_price[$address_id][$package_id][$carrier_id] = array('without_tax' => $price_without_tax, 'with_tax' => $price_with_tax);
                 $grade = $carriers_instance[$carrier_id]->grade;
                 if (is_null($best_grade) || $grade > $best_grade) {
                     $best_grade = $grade;
                     $best_grade_carrier = $carrier_id;
                 }
             }
             $best_price_carriers[$package_id] = $best_price_carrier;
             $best_grade_carriers[$package_id] = $best_grade_carrier;
         }
         // Reset $best_price_carrier, it's now an array
         $best_price_carrier = array();
         $key = '';
         // Get the delivery option with the lower price
         foreach ($best_price_carriers as $package_id => $carrier_id) {
             $key .= $carrier_id . ',';
             if (!isset($best_price_carrier[$carrier_id])) {
                 $best_price_carrier[$carrier_id] = array('price_with_tax' => 0, 'price_without_tax' => 0, 'package_list' => array(), 'product_list' => array());
             }
             $best_price_carrier[$carrier_id]['price_with_tax'] += $carriers_price[$address_id][$package_id][$carrier_id]['with_tax'];
             $best_price_carrier[$carrier_id]['price_without_tax'] += $carriers_price[$address_id][$package_id][$carrier_id]['without_tax'];
             $best_price_carrier[$carrier_id]['package_list'][] = $package_id;
             $best_price_carrier[$carrier_id]['product_list'] = array_merge($best_price_carrier[$carrier_id]['product_list'], $packages[$package_id]['product_list']);
             $best_price_carrier[$carrier_id]['instance'] = $carriers_instance[$carrier_id];
         }
         // Add the delivery option with best price as best price
         $delivery_option_list[$address_id][$key] = array('carrier_list' => $best_price_carrier, 'is_best_price' => true, 'is_best_grade' => false, 'unique_carrier' => count($best_price_carrier) <= 1);
         // Reset $best_grade_carrier, it's now an array
         $best_grade_carrier = array();
         $key = '';
         // Get the delivery option with the best grade
         foreach ($best_grade_carriers as $package_id => $carrier_id) {
             $key .= $carrier_id . ',';
             if (!isset($best_grade_carrier[$carrier_id])) {
                 $best_grade_carrier[$carrier_id] = array('price_with_tax' => 0, 'price_without_tax' => 0, 'package_list' => array(), 'product_list' => array());
             }
             $best_grade_carrier[$carrier_id]['price_with_tax'] += $carriers_price[$address_id][$package_id][$carrier_id]['with_tax'];
             $best_grade_carrier[$carrier_id]['price_without_tax'] += $carriers_price[$address_id][$package_id][$carrier_id]['without_tax'];
             $best_grade_carrier[$carrier_id]['package_list'][] = $package_id;
             $best_grade_carrier[$carrier_id]['product_list'] = array_merge($best_grade_carrier[$carrier_id]['product_list'], $packages[$package_id]['product_list']);
             $best_grade_carrier[$carrier_id]['instance'] = $carriers_instance[$carrier_id];
         }
         // Add the delivery option with best grade as best grade
         if (!isset($delivery_option_list[$address_id][$key])) {
             $delivery_option_list[$address_id][$key] = array('carrier_list' => $best_grade_carrier, 'is_best_price' => false, 'unique_carrier' => count($best_grade_carrier) <= 1);
         }
         $delivery_option_list[$address_id][$key]['is_best_grade'] = true;
         // Get all delivery options with a unique carrier
         foreach ($common_carriers as $carrier_id) {
             $key = '';
             $package_list = array();
             $product_list = array();
             $price_with_tax = 0;
             $price_without_tax = 0;
             foreach ($packages as $package_id => $package) {
                 $key .= $carrier_id . ',';
                 $price_with_tax += $carriers_price[$address_id][$package_id][$carrier_id]['with_tax'];
                 $price_without_tax += $carriers_price[$address_id][$package_id][$carrier_id]['without_tax'];
                 $package_list[] = $package_id;
                 $product_list = array_merge($product_list, $package['product_list']);
             }
             if (!isset($delivery_option_list[$address_id][$key])) {
                 $delivery_option_list[$address_id][$key] = array('is_best_price' => false, 'is_best_grade' => false, 'unique_carrier' => true, 'carrier_list' => array($carrier_id => array('price_with_tax' => $price_with_tax, 'price_without_tax' => $price_without_tax, 'instance' => $carriers_instance[$carrier_id], 'package_list' => $package_list, 'product_list' => $product_list)));
             } else {
                 $delivery_option_list[$address_id][$key]['unique_carrier'] = count($delivery_option_list[$address_id][$key]['carrier_list']) <= 1;
             }
         }
     }
     $cart_rules = JeproshopCartRuleModelCartRule::getCustomerCartRules(JeproshopContext::getContext()->cookie->lang_id, JeproshopContext::getContext()->cookie->customer_id, true, true, false, $this);
     $free_carriers_rules = array();
     foreach ($cart_rules as $cart_rule) {
         if ($cart_rule->free_shipping && $cart_rule->carrier_restriction) {
             $cartRule = new JeproshopCartRuleModelCartRule((int) $cart_rule->cart_rule_id);
             if (JeproshopValidator::isLoadedObject($cartRule, 'cart_rule_id')) {
                 $carriers = $cart_rule->getAssociatedRestrictions('carrier', true, false);
                 if (is_array($carriers) && count($carriers) && isset($carriers['selected'])) {
                     foreach ($carriers['selected'] as $carrier) {
                         if (isset($carrier->carrier_id) && $carrier->carrier_id) {
                             $free_carriers_rules[] = (int) $carrier->carrier_id;
                         }
                     }
                 }
             }
         }
     }
     // For each delivery options :
     //    - Set the carrier list
     //    - Calculate the price
     //    - Calculate the average position
     foreach ($delivery_option_list as $address_id => $delivery_option) {
         foreach ($delivery_option as $key => $value) {
             $total_price_with_tax = 0;
             $total_price_without_tax = 0;
             $position = 0;
             foreach ($value['carrier_list'] as $carrier_id => $data) {
                 $total_price_with_tax += $data['price_with_tax'];
                 $total_price_without_tax += $data['price_without_tax'];
                 $total_price_without_tax_with_rules = in_array($carrier_id, $free_carriers_rules) ? 0 : $total_price_without_tax;
                 if (!isset($carrier_collection[$carrier_id])) {
                     $carrier_collection[$carrier_id] = new Carrier($carrier_id);
                 }
                 $delivery_option_list[$address_id][$key]['carrier_list'][$carrier_id]['instance'] = $carrier_collection[$carrier_id];
                 if (file_exists(_PS_SHIP_IMG_DIR_ . $carrier_id . '.jpg')) {
                     $delivery_option_list[$address_id][$key]['carrier_list'][$carrier_id]['logo'] = _THEME_SHIP_DIR_ . $carrier_id . '.jpg';
                 } else {
                     $delivery_option_list[$address_id][$key]['carrier_list'][$carrier_id]['logo'] = false;
                 }
                 $position += $carrier_collection[$carrier_id]->position;
             }
             $delivery_option_list[$address_id][$key]['total_price_with_tax'] = $total_price_with_tax;
             $delivery_option_list[$address_id][$key]['total_price_without_tax'] = $total_price_without_tax;
             $delivery_option_list[$address_id][$key]['is_free'] = !$total_price_without_tax_with_rules ? true : false;
             $delivery_option_list[$address_id][$key]['position'] = $position / count($value['carrier_list']);
         }
     }
     // Sort delivery option list
     foreach ($delivery_option_list as &$array) {
         uasort($array, array('Cart', 'sortDeliveryOptionList'));
     }
     $cache = $delivery_option_list;
     return $delivery_option_list;
 }
Beispiel #2
0
 public static function autoRemoveFromCart($context = null)
 {
     if (!$context) {
         $context = JeproshopContext::getContext();
     }
     if (!JeproshopCartRuleModelCartRule::isFeaturePublished() || !JeproshopValidator::isLoadedObject($context->cart, 'cart_id')) {
         return array();
     }
     static $errors = array();
     foreach ($context->cart->getCartRules() as $cart_rule) {
         if ($error = $cart_rule['obj']->checkValidity($context, true)) {
             $context->cart->removeCartRule($cart_rule['obj']->cart_rule_id);
             $context->cart->update();
             $errors[] = $error;
         }
     }
     return $errors;
 }
Beispiel #3
0
 public static function sanitize($string, $id_lang, $indexation = false, $iso_code = false)
 {
     $string = trim($string);
     if (empty($string)) {
         return '';
     }
     $string = strtolower(strip_tags($string));
     $string = html_entity_decode($string, ENT_NOQUOTES, 'utf-8');
     $string = preg_replace('/([' . PREG_CLASS_NUMBERS . ']+)[' . PREG_CLASS_PUNCTUATION . ']+(?=[' . PREG_CLASS_NUMBERS . '])/u', '\\1', $string);
     $string = preg_replace('/[' . PREG_CLASS_SEARCH_EXCLUDE . ']+/u', ' ', $string);
     if ($indexation) {
         $string = preg_replace('/[._-]+/', ' ', $string);
     } else {
         $string = preg_replace('/[._]+/', '', $string);
         $string = ltrim(preg_replace('/([^ ])-/', '$1 ', ' ' . $string));
         $string = preg_replace('/[._]+/', '', $string);
         $string = preg_replace('/[^\\s]-+/', '', $string);
     }
     $blacklist = strtolower(JeproshopSettingModelSetting::getValue('search_blacklist'));
     if (!empty($blacklist)) {
         $string = preg_replace('/(?<=\\s)(' . $blacklist . ')(?=\\s)/Su', '', $string);
         $string = preg_replace('/^(' . $blacklist . ')(?=\\s)/Su', '', $string);
         $string = preg_replace('/(?<=\\s)(' . $blacklist . ')$/Su', '', $string);
         $string = preg_replace('/^(' . $blacklist . ')$/Su', '', $string);
     }
     if (!$indexation) {
         $words = explode(' ', $string);
         $processed_words = array();
         // search for aliases for each word of the query
         foreach ($words as $word) {
             $alias = new Alias(null, $word);
             if (JeproshopValidator::isLoadedObject($alias, '')) {
                 $processed_words[] = $alias->search;
             } else {
                 $processed_words[] = $word;
             }
         }
         $string = implode(' ', $processed_words);
     }
     // If the language is constituted with symbol and there is no "words", then split every chars
     if (in_array($iso_code, array('zh', 'tw', 'ja')) && function_exists('mb_strlen')) {
         // Cut symbols from letters
         $symbols = '';
         $letters = '';
         foreach (explode(' ', $string) as $mb_word) {
             if (strlen(Tools::replaceAccentedChars($mb_word)) == mb_strlen(Tools::replaceAccentedChars($mb_word))) {
                 $letters .= $mb_word . ' ';
             } else {
                 $symbols .= $mb_word . ' ';
             }
         }
         if (preg_match_all('/./u', $symbols, $matches)) {
             $symbols = implode(' ', $matches[0]);
         }
         $string = $letters . $symbols;
     } elseif ($indexation) {
         $minWordLen = (int) JeproshopSettingModelSetting::getValue('search_min_word_length');
         if ($minWordLen > 1) {
             $minWordLen -= 1;
             $string = preg_replace('/(?<=\\s)[^\\s]{1,' . $minWordLen . '}(?=\\s)/Su', ' ', $string);
             $string = preg_replace('/^[^\\s]{1,' . $minWordLen . '}(?=\\s)/Su', '', $string);
             $string = preg_replace('/(?<=\\s)[^\\s]{1,' . $minWordLen . '}$/Su', '', $string);
             $string = preg_replace('/^[^\\s]{1,' . $minWordLen . '}$/Su', '', $string);
         }
     }
     $string = trim(preg_replace('/\\s+/', ' ', $string));
     return $string;
 }
Beispiel #4
0
 public static function applyRuleToProduct($rule_id, $product_id, $product_attribute_id = null)
 {
     $rule = new JeproshopSpecificPriceRuleModelSpecificPriceRule((int) $rule_id);
     if (!JeproshopValidator::isLoadedObject($rule, 'specific-price_rule_id') || !$product_id) {
         return false;
     }
     $specific_price = new JeproshopSpecificPriceModelSpecificPrice();
     $specific_price->specific_price_rule_id = (int) $rule->specific_price_rule_id;
     $specific_price->product_id = (int) $product_id;
     $specific_price->product_attribute_id = (int) $product_attribute_id;
     $specific_price->customer_id = 0;
     $specific_price->shop_id = (int) $rule->shop_id;
     $specific_price->country_id = (int) $rule->country_id;
     $specific_price->currency_id = (int) $rule->currency_id;
     $specific_price->group_id = (int) $rule->group_id;
     $specific_price->from_quantity = (int) $rule->from_quantity;
     $specific_price->price = (double) $rule->price;
     $specific_price->reduction_type = $rule->reduction_type;
     $specific_price->reduction = $rule->reduction_type == 'percentage' ? $rule->reduction / 100 : (double) $rule->reduction;
     $specific_price->from = $rule->from;
     $specific_price->to = $rule->to;
     return $specific_price->add();
 }