reset(); } function restore_contents() { global $customer_id; if (!tep_session_is_registered('customer_id')) return false; // insert current cart contents in database if (is_array($this->contents)) { reset($this->contents); // -> OSC-79 : La quantit?minimum de vente n'est pas ajust? $pf = new PriceFormatter; while (list($products_id, ) = each($this->contents)) { $pf->loadProduct($products_id); $qty = $pf->adjustQty($this->contents[$products_id]['qty']); // <- OSC-79 $product_query = tep_db_query("select products_id from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); if (!tep_db_num_rows($product_query)) { tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . $qty . "', '" . date('Ymd') . "')"); if (isset($this->contents[$products_id]['attributes'])) { reset($this->contents[$products_id]['attributes']); while (list($option, $value) = each($this->contents[$products_id]['attributes'])) { tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "')"); } } } else { tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $qty . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); } } } // reset per-session cart contents, but not the database contents $this->reset(false); // -> QPBPP135 $products_query = tep_db_query("select cb.products_id, ptdc.discount_categories_id, customers_basket_quantity from " . TABLE_CUSTOMERS_BASKET . " cb left join " . TABLE_PRODUCTS_TO_DISCOUNT_CATEGORIES . " ptdc using(products_id) where customers_id = '" . (int)$customer_id . "'"); while ($products = tep_db_fetch_array($products_query)) { $this->contents[$products['products_id']] = array('qty' => $products['customers_basket_quantity'], 'discount_categories_id' => $products['discount_categories_id']); // <- QPBPP135 // attributes $attributes_query = tep_db_query("select products_options_id, products_options_value_id from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products['products_id']) . "'"); while ($attributes = tep_db_fetch_array($attributes_query)) { $this->contents[$products['products_id']]['attributes'][$attributes['products_options_id']] = $attributes['products_options_value_id']; } } $this->cleanup(); } function reset($reset_database = false) { global $customer_id; $this->contents = array(); $this->total = 0; $this->weight = 0; $this->content_type = false; if (tep_session_is_registered('customer_id') && ($reset_database == true)) { tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'"); tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "'"); } unset($this->cartID); if (tep_session_is_registered('cartID')) tep_session_unregister('cartID'); } function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) { global $new_products_id_in_cart, $customer_id; // -> QPBPP135 $pf = new PriceFormatter; $pf->loadProduct($products_id); $qty = $pf->adjustQty($qty); $discount_category = $pf->get_discount_category(); // <- QPBPP135 $products_id_string = tep_get_uprid($products_id, $attributes); $products_id = tep_get_prid($products_id_string); $attributes_pass_check = true; if (is_array($attributes)) { reset($attributes); while (list($option, $value) = each($attributes)) { if (!is_numeric($option) || !is_numeric($value)) { $attributes_pass_check = false; break; } } } if (is_numeric($products_id) && is_numeric($qty) && ($attributes_pass_check == true)) { $check_product_query = tep_db_query("select products_status from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'"); $check_product = tep_db_fetch_array($check_product_query); if (($check_product !== false) && ($check_product['products_status'] == '1')) { if ($notify == true) { $new_products_id_in_cart = $products_id; tep_session_register('new_products_id_in_cart'); } // -> QPBPP135 if ($this->in_cart($products_id_string)) { $this->update_quantity($products_id_string, $qty, $attributes, $discount_category); } else { $this->contents[$products_id_string] = array('qty' => (int)$qty, 'discount_categories_id' => $discount_category); // <- QPBPP135 // insert into database if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$qty . "', '" . date('Ymd') . "')"); if (is_array($attributes)) { reset($attributes); while (list($option, $value) = each($attributes)) { $this->contents[$products_id_string]['attributes'][$option] = $value; // insert into database if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$option . "', '" . (int)$value . "')"); } } } $this->cleanup(); // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure $this->cartID = $this->generate_cart_id(); } } } // -> QPBPP135 function update_quantity($products_id, $quantity = '', $attributes = '', $discount_categories_id = NULL) { // <- QPBPP135 global $customer_id; $products_id_string = tep_get_uprid($products_id, $attributes); $products_id = tep_get_prid($products_id_string); // -> OSC-79 : La quantit?minimum de vente n'est pas ajust? $pf = new PriceFormatter; $pf->loadProduct($products_id); $quantity = $pf->adjustQty($quantity); // <- OSC-79 $attributes_pass_check = true; if (is_array($attributes)) { reset($attributes); while (list($option, $value) = each($attributes)) { if (!is_numeric($option) || !is_numeric($value)) { $attributes_pass_check = false; break; } } } if (is_numeric($products_id) && isset($this->contents[$products_id_string]) && is_numeric($quantity) && ($attributes_pass_check == true)) { // -> QPBPP135 $this->contents[$products_id_string] = array('qty' => (int)$quantity, 'discount_categories_id' => $discount_categories_id); // <- QPBPP135 // update database if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . (int)$quantity . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "'"); if (is_array($attributes)) { reset($attributes); while (list($option, $value) = each($attributes)) { $this->contents[$products_id_string]['attributes'][$option] = $value; // update database if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "' and products_options_id = '" . (int)$option . "'"); } } } } function cleanup() { global $customer_id; reset($this->contents); while (list($key,) = each($this->contents)) { if ($this->contents[$key]['qty'] < 1) { unset($this->contents[$key]); // remove from database if (tep_session_is_registered('customer_id')) { tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'"); tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'"); } } } } function count_contents() { // get total number of items in cart $total_items = 0; if (is_array($this->contents)) { reset($this->contents); while (list($products_id, ) = each($this->contents)) { $total_items += $this->get_quantity($products_id); } } return $total_items; } function get_quantity($products_id) { if (isset($this->contents[$products_id])) { return $this->contents[$products_id]['qty']; } else { return 0; } } function in_cart($products_id) { if (isset($this->contents[$products_id])) { return true; } else { return false; } } function remove($products_id) { global $customer_id; unset($this->contents[$products_id]); // remove from database if (tep_session_is_registered('customer_id')) { tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); } // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure $this->cartID = $this->generate_cart_id(); } function remove_all() { $this->reset(); } function get_product_id_list() { $product_id_list = ''; if (is_array($this->contents)) { reset($this->contents); while (list($products_id, ) = each($this->contents)) { $product_id_list .= ', ' . $products_id; } } return substr($product_id_list, 2); } // -> OSC-70 : Gestion HT/TTC : affichage prix HT et TTC /* * Ajoute un mode $type_de_prix ?la m?hode 'calculate()' de la classe 'shoppingCart'. * * Si l'on ne sp?ifie pas de valeur ?$type_de_prix lorsque l'on appelle cette m?hode, on aura par d?aut le calcul du PANIER en HT. * Si l'on sp?ifie 'HT' : le mode de calcul du PANIER sera en calcul?en HT (via la fonction tep_add_tax() situ? dans includes/functions/general.php) * Si l'on sp?ifie 'TTC' : le mode de calcul du PANIER sera en calcul?en TTC (via la fonction prix_avec_taxes() situ? dans includes/functions/general.php) * */ function calculate( $type_de_prix = 'HT' ) { // -> OSC-70 : Gestion HT/TTC : affichage prix HT et TTC // -> QPBPP135 global $currencies, $languages_id, $pfs, $customer_id; // for qpbpp added: $languages_id, $pfs $account_query = tep_db_query("select is_partner,coeff_partner from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'"); $account = tep_db_fetch_array($account_query); $coeff_partner = $account['coeff_partner']; $is_partner = $account['is_partner']; // <- QPBPP135 $this->total = 0; $this->weight = 0; if (!is_array($this->contents)) return 0; // -> QPBPP135 $discount_category_quantity = array(); // calculates no of items per discount category in shopping basket foreach ($this->contents as $products_id => $contents_array) { if(tep_not_null($contents_array['discount_categories_id'])) { if (!isset($discount_category_quantity[$contents_array['discount_categories_id']])) { $discount_category_quantity[$contents_array['discount_categories_id']] = $contents_array['qty']; } else { $discount_category_quantity[$contents_array['discount_categories_id']] += $contents_array['qty']; } } } // end foreach $pf = new PriceFormatter; // <- QPBPP135 reset($this->contents); while (list($products_id, ) = each($this->contents)) { $qty = $this->contents[$products_id]['qty']; // -> QPBPP135 if (tep_not_null($this->contents[$products_id]['discount_categories_id'])) { $nof_items_in_cart_same_cat = $discount_category_quantity[$this->contents[$products_id]['discount_categories_id']]; $nof_other_items_in_cart_same_cat = $nof_items_in_cart_same_cat - $qty; } else { $nof_other_items_in_cart_same_cat = 0; } // <- QPBPP135 // products price // -> QPBPP135 $pf->loadProduct($products_id, $languages_id); if ($product = $pfs->getPriceFormatterData($products_id)) { $prid = $product['products_id']; $products_tax = tep_get_tax_rate($product['products_tax_class_id']); $products_price = $pf->computePrice($qty, $nof_other_items_in_cart_same_cat); // -> MARP256 $products_cost = $product['products_cost']; $products_lowPrice = $pf->lowPrice; $products_partner_price = $products_cost*$coeff_partner; if ($is_partner==true) { if ($products_price > $products_partner_price) { $products_price = $products_partner_price; if ($products_price >$products_lowPrice ) { $products_price = $products_lowPrice; } } } // <- MARP256 // <- QPBPP135 $products_weight = $product['products_weight']; // -> QPBPP135 /* $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'"); if (tep_db_num_rows ($specials_query)) { $specials = tep_db_fetch_array($specials_query); $products_price = $specials['specials_new_products_price']; } */ // <- QPBPP135 // -> OSC-70 : Gestion HT/TTC : affichage prix HT et TTC /* * pour les produits complet: * V?ifie si on calcule en HT (via (tep_add_tax()) ou en TTC (via prix_avec_taxes()) * */ switch ($type_de_prix) { case 'HT': // sans le calcul des taxes : $this->total += tep_add_tax($products_price, $products_tax) * $qty; break; case 'TTC': // avec le calcul des taxes : $this->total += prix_avec_taxes($products_price, $products_tax) * $qty; break; } // <- OSC-70 : Gestion HT/TTC : affichage prix HT et TTC $this->weight += ($qty * $products_weight); } // attributes price if (isset($this->contents[$products_id]['attributes'])) { reset($this->contents[$products_id]['attributes']); while (list($option, $value) = each($this->contents[$products_id]['attributes'])) { $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$prid . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'"); $attribute_price = tep_db_fetch_array($attribute_price_query); // -> OSC-70 : Gestion HT/TTC : affichage prix HT et TTC /* * pour les attributs de produits: * V?ifie si on calcule en HT (via (tep_add_tax()) ou en TTC (via prix_avec_taxes()) * */ switch ($type_de_prix) { case 'HT': // sans le calcul des taxes : if ($attribute_price['price_prefix'] == '+') { $this->total += $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax); } else { $this->total -= $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax); } break; case 'TTC': // avec le calcul des taxes : if ($attribute_price['price_prefix'] == '+') { $this->total += $qty * prix_avec_taxes($attribute_price['options_values_price'], $products_tax); } else { $this->total -= $qty * prix_avec_taxes($attribute_price['options_values_price'], $products_tax); } break; } // <- OSC-70 : Gestion HT/TTC : affichage prix HT et TTC } } } } function attributes_price($products_id) { $attributes_price = 0; if (isset($this->contents[$products_id]['attributes'])) { reset($this->contents[$products_id]['attributes']); while (list($option, $value) = each($this->contents[$products_id]['attributes'])) { $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'"); $attribute_price = tep_db_fetch_array($attribute_price_query); if ($attribute_price['price_prefix'] == '+') { $attributes_price += $attribute_price['options_values_price']; } else { $attributes_price -= $attribute_price['options_values_price']; } } } return $attributes_price; } function get_products() { // -> QPBPP135 global $languages_id, $pfs; // <- QPBPP135 if (!is_array($this->contents)) return false; // -> QPBPP135 $discount_category_quantity = array(); foreach ($this->contents as $products_id => $contents_array) { if(tep_not_null($contents_array['discount_categories_id'])) { if (!isset($discount_category_quantity[$contents_array['discount_categories_id']])) { $discount_category_quantity[$contents_array['discount_categories_id']] = $contents_array['qty']; } else { $discount_category_quantity[$contents_array['discount_categories_id']] += $contents_array['qty']; } } } // end foreach $pf = new PriceFormatter; // <- QPBPP135 $products_array = array(); reset($this->contents); while (list($products_id, ) = each($this->contents)) { // -> QPBPP135 $pf->loadProduct($products_id, $languages_id); // does query if necessary and adds to // PriceFormatterStore or gets info from it next if ($products = $pfs->getPriceFormatterData($products_id)) { if (tep_not_null($this->contents[$products_id]['discount_categories_id'])) { $nof_items_in_cart_same_cat = $discount_category_quantity[$this->contents[$products_id]['discount_categories_id']]; $nof_other_items_in_cart_same_cat = $nof_items_in_cart_same_cat - $this->contents[$products_id]['qty']; } else { $nof_other_items_in_cart_same_cat = 0; } $products_price = $pf->computePrice($this->contents[$products_id]['qty'], $nof_other_items_in_cart_same_cat); // <- QPBPP135 $products_array[] = array('id' => $products_id, 'name' => $products['products_name'], 'model' => $products['products_model'], 'image' => $products['products_image'], // -> QPBPP135 'discount_categories_id' => $this->contents[$products_id]['discount_categories_id'], // <- QPBPP135 'price' => $products_price, // -> MARP256 'cost' => $products['products_cost'], 'quantity' => $this->contents[$products_id]['qty'], 'weight' => $products['products_weight'], 'final_price' => ($products_price + $this->attributes_price($products_id)), 'tax_class_id' => $products['products_tax_class_id'], 'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : '')); } } return $products_array; } // -> OSC-70 : Gestion HT/TTC : affichage prix HT et TTC /* * Ajoute un mode $type_de_prix ?la m?hode 'calculate()' de la classe 'shopping_cart'. * * Si l'on ne sp?ifie pas de valeur ?$type_de_prix lorsque l'on appelle cette m?hode, on aura par d?aut le calcul du PANIER en HT. * Si l'on sp?ifie 'HT' : le mode de calcul du PANIER sera en calcul?en HT (via la fonction tep_add_tax() situ? dans includes/functions/general.php) * Si l'on sp?ifie 'TTC' : le mode de calcul du PANIER sera en calcul?en TTC (via la fonction ajout_prix_ttc() situ? dans includes/functions/general.php) * */ function show_total( $type_de_prix = 'HT') { // appel de calculate() avec en param?re le param?re d'appel pass??show_total(). $this->calculate($type_de_prix); // -> OSC-70 : Gestion HT/TTC : affichage prix HT et TTC return $this->total; } function show_weight() { $this->calculate(); return $this->weight; } function generate_cart_id($length = 5) { return tep_create_random_value($length, 'digits'); } function get_content_type() { $this->content_type = false; if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) ) { reset($this->contents); while (list($products_id, ) = each($this->contents)) { if (isset($this->contents[$products_id]['attributes'])) { reset($this->contents[$products_id]['attributes']); while (list(, $value) = each($this->contents[$products_id]['attributes'])) { $virtual_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad where pa.products_id = '" . (int)$products_id . "' and pa.options_values_id = '" . (int)$value . "' and pa.products_attributes_id = pad.products_attributes_id"); $virtual_check = tep_db_fetch_array($virtual_check_query); if ($virtual_check['total'] > 0) { switch ($this->content_type) { case 'physical': $this->content_type = 'mixed'; return $this->content_type; break; default: $this->content_type = 'virtual'; break; } } else { switch ($this->content_type) { case 'virtual': $this->content_type = 'mixed'; return $this->content_type; break; default: $this->content_type = 'physical'; break; } } } } else { switch ($this->content_type) { case 'virtual': $this->content_type = 'mixed'; return $this->content_type; break; default: $this->content_type = 'physical'; break; } } } } else { $this->content_type = 'physical'; } return $this->content_type; } function unserialize($broken) { for(reset($broken);$kv=each($broken);) { $key=$kv['key']; if (gettype($this->$key)!="user function") $this->$key=$kv['value']; } } // -> VDOFS : gestion des articles épuisés function check_after_any_site_changes() { foreach ($this->contents as $products_id => $value) { $check_product_query = tep_db_query("select products_status, products_out_of_stock, products_quantity from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'"); $check_product = tep_db_fetch_array($check_product_query); if ( $check_product['products_status'] == '0' ) { return true; // product has been deactivated } else if ( $check_product['products_out_of_stock'] == 1 ) { return true; // product is out of stock } } return false; } function update_after_any_site_changes() { global $languages_id; $speech = array(); foreach ($this->contents as $products_id => $value) { $check_product_query = tep_db_query("select pd.products_name, p.products_status, p.products_out_of_stock, p.products_quantity from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = '" . (int)$products_id . "'"); $check_product = tep_db_fetch_array($check_product_query); if ( $check_product['products_status'] == '0' ) { $speech[] = $check_product['products_name'] . " retiré du panier : article supprimé du catalogue"; $this->remove($products_id); } else if ( $check_product['products_out_of_stock'] == 1 ) { $speech[] = $check_product['products_name'] . " retiré du panier : article épuisé"; $this->remove($products_id); } } return $speech; } // <- VDOFS // -> VDVCAT : gestion des produits virtuels function has_only_virtual_products() { foreach ($this->contents as $products_id => $value) { $check_product_query = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$products_id . "'"); $check_product = tep_db_fetch_array($check_product_query); if ( $check_product['categories_id'] != '62' ) return false; // 62 : well known virtual category } return true; } // <- VDVCAT } ?>