shopping_cart.php
来自「this the oscommerce 3.0 aplha 4」· PHP 代码 · 共 820 行 · 第 1/3 页
PHP
820 行
<?php/* $Id: shopping_cart.php 1498 2007-03-29 14:04:50Z hpdl $ osCommerce, Open Source E-Commerce Solutions http://www.oscommerce.com Copyright (c) 2007 osCommerce This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License v2 (1991) as published by the Free Software Foundation.*/ class osC_ShoppingCart { var $_contents = array(), $_sub_total = 0, $_total = 0, $_weight = 0, $_tax = 0, $_tax_groups = array(), $_content_type, $_products_in_stock = true; function osC_ShoppingCart() { if (!isset($_SESSION['osC_ShoppingCart_data'])) { $_SESSION['osC_ShoppingCart_data'] = array('contents' => array(), 'sub_total_cost' => 0, 'total_cost' => 0, 'total_weight' => 0, 'tax' => 0, 'tax_groups' => array(), 'shipping_boxes_weight' => 0, 'shipping_boxes' => 1, 'shipping_address' => array('zone_id' => STORE_ZONE, 'country_id' => STORE_COUNTRY), 'shipping_method' => array(), 'billing_address' => array('zone_id' => STORE_ZONE, 'country_id' => STORE_COUNTRY), 'billing_method' => array(), 'shipping_quotes' => array(), 'order_totals' => array()); $this->resetShippingAddress(); $this->resetBillingAddress(); } $this->_contents =& $_SESSION['osC_ShoppingCart_data']['contents']; $this->_sub_total =& $_SESSION['osC_ShoppingCart_data']['sub_total_cost']; $this->_total =& $_SESSION['osC_ShoppingCart_data']['total_cost']; $this->_weight =& $_SESSION['osC_ShoppingCart_data']['total_weight']; $this->_tax =& $_SESSION['osC_ShoppingCart_data']['tax']; $this->_tax_groups =& $_SESSION['osC_ShoppingCart_data']['tax_groups']; $this->_shipping_boxes_weight =& $_SESSION['osC_ShoppingCart_data']['shipping_boxes_weight']; $this->_shipping_boxes =& $_SESSION['osC_ShoppingCart_data']['shipping_boxes']; $this->_shipping_address =& $_SESSION['osC_ShoppingCart_data']['shipping_address']; $this->_shipping_method =& $_SESSION['osC_ShoppingCart_data']['shipping_method']; $this->_billing_address =& $_SESSION['osC_ShoppingCart_data']['billing_address']; $this->_billing_method =& $_SESSION['osC_ShoppingCart_data']['billing_method']; $this->_shipping_quotes =& $_SESSION['osC_ShoppingCart_data']['shipping_quotes']; $this->_order_totals =& $_SESSION['osC_ShoppingCart_data']['order_totals']; } function update() { if ( !isset($_SESSION['cartID']) ) { $this->_calculate(); } } function hasContents() { return !empty($this->_contents); } function synchronizeWithDatabase() { global $osC_Database, $osC_Services, $osC_Language, $osC_Customer, $osC_Image; if (!$osC_Customer->isLoggedOn()) { return false; }// insert current cart contents in database if ($this->hasContents()) { foreach ($this->_contents as $products_id_string => $data) { $Qproduct = $osC_Database->query('select products_id, customers_basket_quantity from :table_customers_basket where customers_id = :customers_id and products_id = :products_id'); $Qproduct->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); $Qproduct->bindInt(':customers_id', $osC_Customer->getID()); $Qproduct->bindValue(':products_id', $products_id_string); $Qproduct->execute(); if ($Qproduct->numberOfRows() > 0) { $Qupdate = $osC_Database->query('update :table_customers_basket set customers_basket_quantity = :customers_basket_quantity where customers_id = :customers_id and products_id = :products_id'); $Qupdate->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); $Qupdate->bindInt(':customers_basket_quantity', $data['quantity'] + $Qproduct->valueInt('customers_basket_quantity')); $Qupdate->bindInt(':customers_id', $osC_Customer->getID()); $Qupdate->bindValue(':products_id', $products_id_string); $Qupdate->execute(); } else { $Qnew = $osC_Database->query('insert into :table_customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values (:customers_id, :products_id, :customers_basket_quantity, now())'); $Qnew->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); $Qnew->bindInt(':customers_id', $osC_Customer->getID()); $Qnew->bindValue(':products_id', $products_id_string); $Qnew->bindInt(':customers_basket_quantity', $data['quantity']); $Qnew->execute(); } } }// reset per-session cart contents, but not the database contents $this->reset(); $Qproducts = $osC_Database->query('select cb.products_id, cb.customers_basket_quantity, cb.customers_basket_date_added, p.products_price, p.products_tax_class_id, p.products_weight, p.products_weight_class, pd.products_name, pd.products_keyword, i.image from :table_customers_basket cb, :table_products p left join :table_products_images i on (p.products_id = i.products_id and i.default_flag = :default_flag), :table_products_description pd where cb.customers_id = :customers_id and cb.products_id = p.products_id and p.products_id = pd.products_id and pd.language_id = :language_id order by cb.customers_basket_date_added desc'); $Qproducts->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); $Qproducts->bindTable(':table_products', TABLE_PRODUCTS); $Qproducts->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES); $Qproducts->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION); $Qproducts->bindInt(':default_flag', 1); $Qproducts->bindInt(':customers_id', $osC_Customer->getID()); $Qproducts->bindInt(':language_id', $osC_Language->getID()); $Qproducts->execute(); while ($Qproducts->next()) { $product = explode('#', $Qproducts->value('products_id'), 2); $attributes_array = array(); if (isset($product[1])) { $attributes = explode(';', $product[1]); foreach ($attributes as $set) { $attribute = explode(':', $set); if (!is_numeric($attribute[0]) || !is_numeric($attribute[1])) { continue 2; // skip product } $attributes_array[$attribute[0]] = $attribute[1]; } } $price = $Qproducts->value('products_price'); if ($osC_Services->isStarted('specials')) { global $osC_Specials; if ($new_price = $osC_Specials->getPrice(osc_get_product_id($Qproducts->value('products_id')))) { $price = $new_price; } } $this->_contents[$Qproducts->value('products_id')] = array('id' => $Qproducts->value('products_id'), 'name' => $Qproducts->value('products_name'), 'keyword' => $Qproducts->value('products_keyword'), 'image' => $Qproducts->value('image'), 'price' => $price, 'final_price' => $price, 'quantity' => $Qproducts->valueInt('customers_basket_quantity'), 'weight' => $Qproducts->value('products_weight'), 'tax_class_id' => $Qproducts->valueInt('products_tax_class_id'), 'date_added' => osC_DateTime::getShort($Qproducts->value('customers_basket_date_added')), 'weight_class_id' => $Qproducts->valueInt('products_weight_class')); if (!empty($attributes_array)) { foreach ($attributes_array as $option_id => $value_id) { $Qattributes = $osC_Database->query('select pa.options_values_price, pa.price_prefix, po.products_options_name, pov.products_options_values_name from :table_products_attributes pa, :table_products_options po, :table_products_options_values pov where pa.products_id = :products_id and pa.options_id = :options_id and pa.options_values_id = :options_values_id and pa.options_id = po.products_options_id and po.language_id = :language_id and pa.options_values_id = pov.products_options_values_id and pov.language_id = :language_id'); $Qattributes->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES); $Qattributes->bindTable(':table_products_options', TABLE_PRODUCTS_OPTIONS); $Qattributes->bindTable(':table_products_options_values', TABLE_PRODUCTS_OPTIONS_VALUES); $Qattributes->bindInt(':products_id', osc_get_product_id($Qproducts->value('products_id'))); $Qattributes->bindInt(':options_id', $option_id); $Qattributes->bindInt(':options_values_id', $value_id); $Qattributes->bindInt(':language_id', $osC_Language->getID()); $Qattributes->bindInt(':language_id', $osC_Language->getID()); $Qattributes->execute(); if ($Qattributes->numberOfRows() > 0) { $this->_contents[$Qproducts->value('products_id')]['attributes'][$option_id] = array('options_id' => $option_id, 'options_values_id' => $value_id, 'products_options_name' => $Qattributes->value('products_options_name'), 'products_options_values_name' => $Qattributes->value('products_options_values_name'), 'options_values_price' => $Qattributes->value('options_values_price'), 'price_prefix' => $Qattributes->value('price_prefix')); if ($Qattributes->value('price_prefix') == '+') { $this->_contents[$Qproducts->value('products_id')]['final_price'] += $Qattributes->value('options_values_price'); } else { $this->_contents[$Qproducts->value('products_id')]['final_price'] -= $Qattributes->value('options_values_price'); } } else { unset($this->_contents[$Qproducts->value('products_id')]); continue 2; // skip product } } } } $this->_cleanUp(); $this->_calculate(); } function reset($reset_database = false) { global $osC_Database, $osC_Customer; if (($reset_database === true) && $osC_Customer->isLoggedOn()) { $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id'); $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); $Qdelete->bindInt(':customers_id', $osC_Customer->getID()); $Qdelete->execute(); } $this->_contents = array(); $this->_sub_total = 0; $this->_total = 0; $this->_weight = 0; $this->_tax = 0; $this->_tax_groups = array(); $this->_content_type = null; $this->resetShippingAddress(); $this->resetShippingMethod(); $this->resetBillingAddress(); $this->resetBillingMethod(); if ( isset($_SESSION['cartID']) ) { unset($_SESSION['cartID']); } } function add($products_id_string, $attributes = null, $quantity = null) { global $osC_Database, $osC_Services, $osC_Language, $osC_Customer, $osC_Image; $products_id_string = osc_get_product_id_string($products_id_string, $attributes); $products_id = osc_get_product_id($products_id_string); if (is_numeric($products_id)) { $Qcheck = $osC_Database->query('select p.products_price, p.products_tax_class_id, p.products_weight, p.products_weight_class, p.products_status, i.image from :table_products p left join :table_products_images i on (p.products_id = i.products_id and i.default_flag = :default_flag) where p.products_id = :products_id'); $Qcheck->bindTable(':table_products', TABLE_PRODUCTS); $Qcheck->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES); $Qcheck->bindInt(':default_flag', 1); $Qcheck->bindInt(':products_id', $products_id); $Qcheck->execute(); if ($Qcheck->valueInt('products_status') === 1) { if ($this->exists($products_id_string)) { if (!is_numeric($quantity)) { $quantity = $this->getQuantity($products_id_string) + 1; } $this->_contents[$products_id_string]['quantity'] = $quantity;// update database if ($osC_Customer->isLoggedOn()) { $Qupdate = $osC_Database->query('update :table_customers_basket set customers_basket_quantity = :customers_basket_quantity where customers_id = :customers_id and products_id = :products_id'); $Qupdate->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET); $Qupdate->bindInt(':customers_basket_quantity', $quantity); $Qupdate->bindInt(':customers_id', $osC_Customer->getID()); $Qupdate->bindValue(':products_id', $products_id_string); $Qupdate->execute(); } } else { if (!is_numeric($quantity)) { $quantity = 1; } $Qproduct = $osC_Database->query('select products_name, products_keyword from :table_products_description where products_id = :products_id and language_id = :language_id'); $Qproduct->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION); $Qproduct->bindInt(':products_id', $products_id); $Qproduct->bindInt(':language_id', $osC_Language->getID()); $Qproduct->execute(); $price = $Qcheck->value('products_price'); if ($osC_Services->isStarted('specials')) { global $osC_Specials; if ($new_price = $osC_Specials->getPrice($products_id)) { $price = $new_price; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?