Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Seperate Pricing Per Customer v3.5


scendent

Recommended Posts

Im running SPC 4.15 and so far are quite happy with it untill faced following probz:

 

http://www.???.??/catalog/index.php?cPath=...mp;filter_id=25

 

select count(p.products_id) as total from (products p, products_description pd, manufacturers m, products_to_categories p2c) left join specials_retail_prices s using(products_id) where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '25' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '4' and p2c.categories_id = '36'

 

I really can't get clear WHICH part of the code performing it... therefore can't get clear.

 

Any suggestions and/or assistance?

Link to comment
Share on other sites

Im running SPC 4.15 and so far are quite happy with it untill faced following probz:

 

http://www.???.??/catalog/index.php?cPath=...mp;filter_id=25

 

select count(p.products_id) as total from (products p, products_description pd, manufacturers m, products_to_categories p2c) left join specials_retail_prices s using(products_id) where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '25' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '4' and p2c.categories_id = '36'

 

I really can't get clear WHICH part of the code performing it... therefore can't get clear.

Of course it would be helpful to tell which error you get... I presume it has to do with MySQL5 and those fixes on index.php can be found in the latest version of SPPC. You would also need to fix advanced_search_results.php.

Link to comment
Share on other sites

I just finished installing this mod in a VERY HEAVILY modified store and I am thrilled to say everything is working! (so far!) I wand to thank the contributors who wrote this... It is the most elaborate mod I've used and so well done.. you should get a prize or award or something! Anyway I did want to share one problem I had and it's solution in case anyone else has it...

 

I was getting a Fatal Error: Call to a member function calculate_price() on a non-object in my shopping_cart.php file. I looked through the forum and the only lead was to look at the includes/classes/currencies.php file to make sure it was the current version.. and it is. So I was stumped. I then found this, posted by Draeisme in a different forum -

 

"Hey, i had the same problem. I fixed it by making a change. The currencies variable has to be defined earlier in the code.

 

Find this code below

CODE

function calculate() {

global $customer_id, $sendto, $ship_zip;

 

 

Change it to

 

CODE

function calculate() {

global $currencies, $customer_id, $sendto, $ship_zip;"

 

It solved my problem! I only had the

 

function calculate() {

 

part of the code but I added the rest as suggested and it worked! Hopefully it will help someone else, too!

 

Cheers!

Link to comment
Share on other sites

Of course it would be helpful to tell which error you get... I presume it has to do with MySQL5 and those fixes on index.php can be found in the latest version of SPPC. You would also need to fix advanced_search_results.php.

 

Upsss.... Sorry not to mention it since beginning :blush:

 

index.php?cPath=29_31_36&sort=3a&filter_id=23

 

1052 - Column 'products_id' in from clause is ambiguous

 

select count(p.products_id) as total from (products p, products_description pd, manufacturers m, products_to_categories p2c) left join specials_retail_prices s using(products_id) where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '23' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '4' and p2c.categories_id = '36'

 

You're correct that I'm running under MySQL - 5.0.27

Link to comment
Share on other sites

Im running SPC 4.15 and so far are quite happy with it untill faced following probz:

 

http://www.???.??/catalog/index.php?cPath=...mp;filter_id=25

 

select count(p.products_id) as total from (products p, products_description pd, manufacturers m, products_to_categories p2c) left join specials_retail_prices s using(products_id) where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '25' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '4' and p2c.categories_id = '36'

 

I really can't get clear WHICH part of the code performing it... therefore can't get clear.

 

Any suggestions and/or assistance?

 

Special thnks to Jan Zonjee!

 

I got Your message and sorted out what's up! Rest was just technical matters and coused no problems - thks for idea where to search!

 

By the way - I'd suggest to switch to $_get, instead of heavy$HTTP_ ... ;-)

Link to comment
Share on other sites

Version 2 of the Quantity Price Breaks Per Product for Separate Pricing Per Customer contribution has been uploaded now. It's an enhanced implementation of QPBPP 1.3.5 for SPPC 4.2.2.

A minimum order quantity (different for groups of course) has been implemented (using parts of other contributions).

Improvements on the admin side were made and the display of the price breaks on product_info.php can now be changed to a dropdrown (triggered by a setting in the admin [Configuration->Price breaks] using the number of price breaks for a product.)

You need to have at least MySQL 4.1 to be able to use this addon because of the use of subselects.

Link to comment
Share on other sites

Hello,

 

I have posted this on the Order Editor contribution forum but thought that some here in the SPPC forum might have a clue how to get this fixed...

 

My question relates to Order Editor working along with SPPC. It seems to work fine except for when I try to add a product. The price next to the product in the dropdown is only reflecting the set "retail" price and not the SPPC price according to the customers group. Is there a workaround for this?

 

If this has been mentioned elsewhere I apologize... perhaps a kick in the right direction?

 

Thanks.

 

 

To make it more clear, when I add a product in the order editor the price defaults to retail regardless of the customer group. Is there a way to reflect the special pricing in this dropdown? It's important to mention that although the incorrect price shows in the dropdown when I add the product the correct price for the customer group is calculated in the order.

 

Thanks again.

Link to comment
Share on other sites

Is there a way to reflect the special pricing in this dropdown? It's important to mention that although the incorrect price shows in the dropdown when I add the product the correct price for the customer group is calculated in the order.

Never seen that contribution but if it can get the price from the table products then surely it can look for a price in products_groups too. Just need to find the right place for the code :)

Link to comment
Share on other sites

Just need to find the right place for the code :)

 

That's the hard part... I have narrowed the code down to the following sections. The first is where, I believe, the information is being pulled from the db:

 

$products_query = tep_db_query("select distinct p.products_id, p.products_price, p.products_model, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c " . $product_search . " order by pd.products_name");
$not_found = ((tep_db_num_rows($products_query)) ? false : true);
 }

 $category_array = array(array('id' => '', 'text' => TEXT_SELECT_CATEGORY),
					  array('id' => '0', 'text' => TEXT_ALL_CATEGORIES));

 

This second bit is what is being displayed in the dropdown:

 

if (($step > 1) && (!$not_found)) {
$product_array = array(array('id' => 0, 'text' => TEXT_SELECT_PRODUCT));
while($products = tep_db_fetch_array($products_query)) {
  $product_array[] = array('id' => $products['products_id'],
						   'text' => $products['products_name'] . ' (' . $products['products_model'] . ')' . ': ' . $currencies->format($products['products_price'], true, $order->info['currency'], $order->info['currency_value']));
}
 }

 

Do you have any ideas as to what I could modify to show the correct price? I tried to modify it a bit but bad things happen when i do that :)

 

Thanks

Link to comment
Share on other sites

I have just added this contrib, seems to be a problem, as soon as you try to go to your cart contents it just shows a blank screen. I have tried both a manual install and replacing the files with the modded ones included.

Any ideas?

Yes, find out what the error is. Ask your hosting provider if and where they keep an error log. Without error message it could be anything.

Link to comment
Share on other sites

That's the hard part... I have narrowed the code down to the following sections. The first is where, I believe, the information is being pulled from the db:

 

$products_query = tep_db_query("select distinct p.products_id, p.products_price, p.products_model, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c " . $product_search . " order by pd.products_name");
$not_found = ((tep_db_num_rows($products_query)) ? false : true);
 }

 $category_array = array(array('id' => '', 'text' => TEXT_SELECT_CATEGORY),
					  array('id' => '0', 'text' => TEXT_ALL_CATEGORIES));

I think if you change the

$products_query = tep_db_query("select distinct p.products_id, p.products_price, p.products_model, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c " . $product_search . " order by pd.products_name");

to:

$cg_id_query = ("select customers_group_id from orders, customers where orders.customers_id = customers.customers_id and orders.orders_id = '" . (int)$_GET['oId'] . "'");
$cg_id_result = tep_db_fetch_array($cg_id_query);
$customer_group_id = $cg_id_result['customers_group_id'];

if (isset($customer_group_id) && $customer_group_id > 0) {
 $products_query = tep_db_query("select distinct p.products_id, IF(pg.customers_group_price IS NULL, p.products_price, pg.customers_group_price) as products_price, p.products_model, pd.products_name from " . TABLE_PRODUCTS . " p left join (select products_id, customers_group_price from " . TABLE_PRODUCTS_GROUPS . " where customers_group_id = '" . $customer_group_id . "') as pg,  " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c " . $product_search . " order by pd.products_name");
} else {
 $products_query = tep_db_query("select distinct p.products_id, p.products_price, p.products_model, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c " . $product_search . " order by pd.products_name");
}

that it could work.

Link to comment
Share on other sites

I think if you change the

$products_query = tep_db_query("select distinct p.products_id, p.products_price, p.products_model, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c " . $product_search . " order by pd.products_name");

to:

$cg_id_query = ("select customers_group_id from orders, customers where orders.customers_id = customers.customers_id and orders.orders_id = '" . (int)$_GET['oId'] . "'");
$cg_id_result = tep_db_fetch_array($cg_id_query);
$customer_group_id = $cg_id_result['customers_group_id'];

if (isset($customer_group_id) && $customer_group_id > 0) {
 $products_query = tep_db_query("select distinct p.products_id, IF(pg.customers_group_price IS NULL, p.products_price, pg.customers_group_price) as products_price, p.products_model, pd.products_name from " . TABLE_PRODUCTS . " p left join (select products_id, customers_group_price from " . TABLE_PRODUCTS_GROUPS . " where customers_group_id = '" . $customer_group_id . "') as pg,  " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c " . $product_search . " order by pd.products_name");
} else {
 $products_query = tep_db_query("select distinct p.products_id, p.products_price, p.products_model, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c " . $product_search . " order by pd.products_name");
}

that it could work.

 

 

Thanks Jan. I tried that code but received an error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/28/d244355237/htdocs/temp/admin/includes/functions/database.php on line 99

 

I looked at this file and at line 99 is the following function (line 99 is bold):

 

function tep_db_fetch_array($db_query) {

return mysql_fetch_array($db_query, MYSQL_ASSOC);

}

 

Any other suggestions? I've been trying to play with the code for the last few days and I must say that although I'm not getting anything to work I am learning a lot about PHP and what not to do... ;)

 

Thanks again Jan.

Link to comment
Share on other sites

Thanks Jan. I tried that code but received an error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/28/d244355237/htdocs/temp/admin/includes/functions/database.php on line 99

That's a very generic error. I think this should be changed to:

$cg_id_query = tep_db_query("select customers_group_id from orders, customers where orders.customers_id = customers.customers_id and orders.orders_id = '" . (int)$_GET['oId'] . "'");

Link to comment
Share on other sites

Ok, I have tried installing this on a new install, using the modified files included in the addon.

For some reason I'm unable to create a customer account, I get this message

 

1146 - Table 'awdcifc_osc1.TABLE_CUSTOMERS_GROUPS' doesn't exist

 

select c.customers_group_id, cg.customers_group_show_tax, cg.customers_group_tax_exempt, cg.group_specific_taxes_exempt from customers c left join TABLE_CUSTOMERS_GROUPS cg using(customers_group_id) where c.customers_id = '2'

 

[TEP STOP]

 

Any ideas?

Is this a code problem, or a database problem, I'm fairly new to this and am worried I'm gonna make a mess if I try to fix this myself.

Link to comment
Share on other sites

You didn't add that define to one of the two includes/database_tables.php files.

 

I know this is a stupid question, but how and where do I add the code? And what code? Will I have to fully re-install?

 

I sorry for my apparent stupidity.

Edited by mileeaters
Link to comment
Share on other sites

I know this is a stupid question, but how and where do I add the code? And what code? Will I have to fully re-install?

 

I sorry for my apparent stupidity.

 

I used the included database_tables files included in the add-on. So not sure how the define has been ommited. Any ideas how to fix this?

Link to comment
Share on other sites

I used the included database_tables files included in the add-on. So not sure how the define has been ommited. Any ideas how to fix this?

And have you checked those to see if they contain those defines for SPPC? People do make simple mistakes, backups get reloaded, FTP transfers not succesfull etcetera.

Link to comment
Share on other sites

That's a very generic error. I think this should be changed to:

$cg_id_query = tep_db_query("select customers_group_id from orders, customers where orders.customers_id = customers.customers_id and orders.orders_id = '" . (int)$_GET['oId'] . "'");

 

 

Jan, that did eliminate the error I was receiving however it still does not accurately show the group price in the dropdown. I think that I might abandon this mission just because I don't know enough to make it work and I know you have much more important endeavors to deal with. I don't want you to feel like it is a problem that you need to fix.

 

I really appreciate you taking the time to work with me on this Jan and if you do have any other ideas to offer I will be more than willing to try them but otherwise I'll just have to try to tackle this when my skills develop a bit more.

 

Thanks again for all of your effort.

Edited by DimeNote
Link to comment
Share on other sites

if you do have any other ideas to offer

There are two occurrences of $product_query in the file edit_orders_add_product.php (I think you are using that). Maybe it is using the first one. Try making an obvious error in the $product_query like changing products_id in products___id and if that query is used you will see an error. If it uses another one, you don't get an error :)

 

If so, then move the query and code for getting the $customer_group_id higher (to above the first occurence of $product_query) and adapt the first query likewise as the second one.

Link to comment
Share on other sites

Hi

 

I have installed SPPC 4.2.2 on a clean 2.2RC2a on a separate local site just to give it a good play about, with a view to doing a manual install on my shop.

 

I'm loving it so far and i would like to ask about when corporate customers registering for 'trade' with a Company's Tax ID no...

 

I would like to know if it's possible to validate the Tax ID no immediately, so that the customer would immediately become a 'trade' customer?

 

For instance, validating the Tax No format and performing a remote look up to verify that the Tax ID is currently in use by a real company?

 

I have been trying to modify GCDiscountSpecials 1.4b so that the customer could opt for a trade account at the create account stage. I've managed to implement the relevant entry & database updates but alas, not the validation of the company so it's not really a goer as yet.

 

Then I found this contrib and would like to try the same idea with this. Maybe others would too?

 

Any ideas would be greatly appreciated

 

Bee

:)

There are 10 types of people in this world, those who understand binary and those who don't...

Link to comment
Share on other sites

For instance, validating the Tax No format and performing a remote look up to verify that the Tax ID is currently in use by a real company?

That is a contribution in itself since there is no uniform tax number format all over the world and a remote lookup will undoubtedly not be possible everywhere also. I think there was something for the EU (TVA Intracommunautaire v2.0, 1590 and/or E.U. VAT Intracom Number, 1848) but I'm not sure how those contributions work and how fast.

Link to comment
Share on other sites

Hi Jan.

 

I am once again in need of your assistance. I've loaded price breaks for sppc and I have hide products also installed. My current problem is with the includes/classes/shopping_cart.php. When I try to bring up the store I get this error.

 

Parse error: syntax error, unexpected T_CLASS, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /mnt/blablablabla/catalog/includes/classes/shopping_cart.php on line 714

 

The admin side seems to work perfectly. If I delete the code for hide products then the store comes up without an issue. However when I add a product to the cart it automatically adds 2 items.

 

Please help!!!!

 

Code with hide products installed that give parse error

<?php
/*
 $Id: shopping_cart.php 1739 2007-12-20 00:52:16Z hpdl $
 adapted for Separate Pricing Per Customer v4.2 2008/03/07

 osCommerce, Open Source E-Commerce Solutions
 [url="http://www.oscommerce.com"]http://www.oscommerce.com[/url]

 Copyright © 2003 osCommerce

 Released under the GNU General Public License
*/

 class shoppingCart {
var $contents, $total, $weight, $cartID, $content_type;

function shoppingCart() {
  $this->reset();
}

function restore_contents() {
  global $customer_id, $languages_id; // languages_id needed for PriceFormatter - QPBPP

  if (!tep_session_is_registered('customer_id')) return false;

// insert current cart contents in database
  if (is_array($this->contents)) {
	reset($this->contents);
// BOF SPPC attribute hide/invalid check: loop through the shopping cart and check the attributes if they
// are hidden for the now logged-in customer
  $this->cg_id = $this->get_customer_group_id();
	while (list($products_id, ) = each($this->contents)) {
				// only check attributes if they are set for the product in the cart
			   if (isset($this->contents[$products_id]['attributes'])) {
			$check_attributes_query = tep_db_query("select options_id, options_values_id, IF(find_in_set('" . $this->cg_id . "', attributes_hide_from_groups) = 0, '0', '1') as hide_attr_status from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . tep_get_prid($products_id) . "'");
			while ($_check_attributes = tep_db_fetch_array($check_attributes_query)) {
				$check_attributes[] = $_check_attributes;
			} // end while ($_check_attributes = tep_db_fetch_array($check_attributes_query))
			$no_of_check_attributes = count($check_attributes);
			$change_products_id = '0';

			foreach($this->contents[$products_id]['attributes'] as $attr_option => $attr_option_value) {
				$valid_option = '0';
				for ($x = 0; $x < $no_of_check_attributes; $x++) {
					if ($attr_option == $check_attributes[$x]['options_id'] && $attr_option_value == $check_attributes[$x]['options_values_id']) {
						$valid_option = '1';
						if ($check_attributes[$x]['hide_attr_status'] == '1') {
						// delete hidden attributes from array attributes, change products_id accordingly later
						$change_products_id = '1';
						unset($this->contents[$products_id]['attributes'][$attr_option]);
						}
					} // end if ($attr_option == $check_attributes[$x]['options_id']....
				} // end for ($x = 0; $x < $no_of_check_attributes; $x++)
				if ($valid_option == '0') {
					// after having gone through the options for this product and not having found a matching one
					// we can conclude that apparently this is not a valid option for this product so remove it
					unset($this->contents[$products_id]['attributes'][$attr_option]);
					// change products_id accordingly later
					$change_products_id = '1';
				}
			} // end foreach($this->contents[$products_id]['attributes'] as $attr_option => $attr_option_value)

	  if ($change_products_id == '1') {
		   $original_products_id = $products_id;
		   $products_id = tep_get_prid($original_products_id);
		   $products_id = tep_get_uprid($products_id, $this->contents[$original_products_id]['attributes']);
					 // add the product without the hidden attributes to the cart
		   $this->contents[$products_id] = $this->contents[$original_products_id];
				 // delete the originally added product with the hidden attributes
		   unset($this->contents[$original_products_id]);
		}
			  } // end if (isset($this->contents[$products_id]['attributes']))
			} // end while (list($products_id, ) = each($this->contents))
   reset($this->contents); // reset the array otherwise the cart will be emptied
// EOF SPPC attribute hide/invalid check
	while (list($products_id, ) = each($this->contents)) {
	  $qty = $this->contents[$products_id]['qty'];
// BOF QPBPP for SPPC adjust quantity blocks and min_order_qty for this customer group
// warnings about this are raised in PriceFormatter
  $pf = new PriceFormatter;
  $pf->loadProduct(tep_get_prid($products_id), $languages_id);
  $qty = $pf->adjustQty($qty);
// EOF QPBPP for SPPC

	  $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) . "', '" . tep_db_input($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 = '" . tep_db_input($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);

  // BOF QPBPP for SPPC
  $products_query = tep_db_query("select cb.products_id, ptdc.discount_categories_id, customers_basket_quantity from " . TABLE_CUSTOMERS_BASKET . " cb left join (select products_id, discount_categories_id from " . TABLE_PRODUCTS_TO_DISCOUNT_CATEGORIES . " where customers_group_id = '" . $this->cg_id . "') as ptdc on cb.products_id = ptdc.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']);
// EOF QPBPP for SPPC

// BOF SPPC Hide products and categories from groups
	  $no_of_products_in_basket = 0;
  while ($_products = tep_db_fetch_array($products_query)) {
	$temp_post_get_array[] = $_products['products_id'];
		$products[] = $_products;
		$no_of_products_in_basket += 1;
   }
 if ($no_of_products_in_basket > 0) {
		$hide_status_products = array();
		$hide_status_products = tep_get_hide_status($hide_status_products, $this->cg_id, $temp_post_get_array);
		for ($i=0; $i < $no_of_products_in_basket; $i++) {
		  foreach($hide_status_products as $key => $subarray) {
			if ($subarray['products_id'] == tep_get_prid($products[$i]['products_id']) && $subarray['hidden'] == '0') {
// not hidden for this customer, can be added to the object shoppingCart
	$this->contents[$products[$i]['products_id']] = array('qty' => $products[$i]['customers_basket_quantity']);
// 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[$i]['products_id']) . "'");
			   while ($attributes = tep_db_fetch_array($attributes_query)) {  $this->contents[$products[$i]['products_id']]['attributes'][$attributes['products_options_id']] = $attributes['products_options_value_id'];
			   }
			} elseif ($subarray['products_id'] == tep_get_prid($products[$i]['products_id']) && $subarray['hidden'] == '1') {
// product is hidden for the customer, don't add to object shoppingCart, delete from db next
			$products_to_delete_from_cb[] = $products[$i]['products_id'];
			} // end if/elseif
		  }// end foreach ($hide_status_products as $key => $subarray)
		} // end for ($i=0; $i < $no_of_products_in_basket; $i++)

// delete from the database those products that are hidden from this customer
  if (tep_not_null($products_to_delete_from_cb)) {
	 $no_of_iterations = count($products_to_delete_from_cb);
// since the products_id in the table customer_basket and customer_basket_attributes can contain
// attributes like 1{4}2{3}6 we need to delete them one by one for the two tables
	for ($y = 0; $y < $no_of_iterations; $y++) {
	   tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and (products_id = '" . (int)$products_to_delete_from_cb[$y] . "' or products_id REGEXP '^" .  (int)$products_to_delete_from_cb[$y] . "{');");
	   tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and (products_id = '" . (int)$products_to_delete_from_cb[$y] . "' or products_id REGEXP '^" .  (int)$products_to_delete_from_cb[$y] . "{');");
	 } // end for ($y = 0; $y < $no_of_iterations; $y++)
  } // end if (tep_not_null($products_to_delete_from_cb))
} // end if ($no_of_products_in_basket > 0)
// EOF SPPC Hide products and categories from groups
  $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;
// BOF Separate Pricing Per Customer 
  $this->cg_id = $this->get_customer_group_id();
// EOF Separate Pricing Per Customer

  $products_id_string = tep_get_uprid($products_id, $attributes);
  $products_id = tep_get_prid($products_id_string);

  if (defined('MAX_QTY_IN_CART') && (MAX_QTY_IN_CART > 0) && ((int)$qty > MAX_QTY_IN_CART)) {
	$qty = MAX_QTY_IN_CART;
  }
// BOF QPBPP for SPPC
  $pf = new PriceFormatter;
  $pf->loadProduct($products_id);
  $qty = $pf->adjustQty($qty);
  $discount_category = $pf->get_discount_category();
// EOF QPBPP for SPPC

  $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)) {
// BOF SPPC attribute hide check, original query expanded to include attributes
			$check_product_query = tep_db_query("select p.products_status, options_id, options_values_id, IF(find_in_set('" . $this->cg_id . "', attributes_hide_from_groups) = 0, '0', '1') as hide_attr_status from " . TABLE_PRODUCTS . " p left join " . TABLE_PRODUCTS_ATTRIBUTES . " using(products_id) where p.products_id = '" . (int)$products_id . "'");
			while ($_check_product = tep_db_fetch_array($check_product_query)) {
				$check_product[] = $_check_product;
			} // end while ($_check_product = tep_db_fetch_array($check_product_query))
			$no_of_check_product = count($check_product);

 if (is_array($attributes)) {
			foreach($attributes as $attr_option => $attr_option_value) {
				$valid_option = '0';
				for ($x = 0; $x < $no_of_check_product; $x++) {
					if ($attr_option == $check_product[$x]['options_id'] && $attr_option_value == $check_product[$x]['options_values_id']) {
						$valid_option = '1';
						if ($check_product[$x]['hide_attr_status'] == '1') {
						// delete hidden attributes from array attributes
						unset($attributes[$attr_option]);
						}
					} // end if ($attr_option == $check_product[$x]['options_id']....
				} // end for ($x = 0; $x < $no_of_check_product; $x++)
				if ($valid_option == '0') {
					// after having gone through the options for this product and not having found a matching one
					// we can conclude that apparently this is not a valid option for this product so remove it
					unset($attributes[$attr_option]);
				}
			} // end foreach($attributes as $attr_option => $attr_option_value)
} // end if (is_array($attributes))
// now attributes have been checked and hidden and invalid ones deleted make the $products_id_string again
			$products_id_string = tep_get_uprid($products_id, $attributes);

	if ((isset($check_product) && tep_not_null($check_product)) && ($check_product[0]['products_status'] == '1')) {
// EOF SPPC attribute hide check
	  if ($notify == true) {
		$new_products_id_in_cart = $products_id;
		tep_session_register('new_products_id_in_cart');
	  }

// BOF QPBPP for SPPC
	  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);
// EOF QPBPP for SPPC

// 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();
	}
  }
}

 // BOF QPBPP for SPPC
function update_quantity($products_id, $quantity = '', $attributes = '', $discount_categories_id = NULL) {
// EOF QPBPP for SPPC

  global $customer_id;

  $products_id_string = tep_get_uprid($products_id, $attributes);
  $products_id = tep_get_prid($products_id_string);

  if (defined('MAX_QTY_IN_CART') && (MAX_QTY_IN_CART > 0) && ((int)$quantity > MAX_QTY_IN_CART)) {
	$quantity = MAX_QTY_IN_CART;
  }

  $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)) {
	// BOF QPBPP for SPPC
	$this->contents[$products_id_string] = array('qty' => (int)$quantity, 'discount_categories_id' => $discount_categories_id);
// EOF QPBPP for SPPC

// 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);
}

function calculate() {
  global $currencies, $languages_id, $pfs; // for QPBPP added: $languages_id, $pfs

  $this->total = 0;
  $this->weight = 0;
  if (!is_array($this->contents)) return 0;
// BOF Separate Pricing Per Customer
// global variable (session) $sppc_customer_group_id -> class variable cg_id
  $this->cg_id = $this->get_customer_group_id();
// EOF Separate Pricing Per Customer
// BOF QPBPP for SPPC
	$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;
// EOF QPBPP for SPPC

  reset($this->contents);
  while (list($products_id, ) = each($this->contents)) {
	$qty = $this->contents[$products_id]['qty'];
// BOF QPBPP for SPPC	   
  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;
  }
// EOF QPBPP for SPPC

// products price
// BOF QPBPP for SPPC
   $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);
// EOF QPBPP for SPPC


// BOF Separate Pricing Per Customer
  $specials_price = tep_get_products_special_price((int)$prid);
  if (tep_not_null($specials_price)) {
 $products_price = $specials_price;
  } elseif ($this->cg_id != 0){
	$customer_group_price_query = tep_db_query("select customers_group_price from " . TABLE_PRODUCTS_GROUPS . " where products_id = '" . (int)$prid . "' and customers_group_id =  '" . $this->cg_id . "'");
	if ($customer_group_price = tep_db_fetch_array($customer_group_price_query)) {
	$products_price = $customer_group_price['customers_group_price'];
	}
	  }
// EOF Separate Pricing Per Customer

	  $this->total += $currencies->calculate_price($products_price, $products_tax, $qty);
	  $this->weight += ($qty * $products_weight);
	}

// attributes price
// BOF SPPC attributes mod
	if (isset($this->contents[$products_id]['attributes'])) {
	  reset($this->contents[$products_id]['attributes']);
   $where = " AND ((";
	  while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
	 $where .= "options_id = '" . (int)$option . "' AND options_values_id = '" . (int)$value . "') OR (";
	  }
   $where=substr($where, 0, -5) . ')';

   $attribute_price_query = tep_db_query("SELECT products_attributes_id, options_values_price, price_prefix FROM " . TABLE_PRODUCTS_ATTRIBUTES . " WHERE products_id = '" . (int)$products_id . "'" . $where ."");

   if (tep_db_num_rows($attribute_price_query)) { 
	   $list_of_prdcts_attributes_id = '';
			 // empty array $attribute_price
			 $attribute_price = array();
	   while ($attributes_price_array = tep_db_fetch_array($attribute_price_query)) { 
	   $attribute_price[] =  $attributes_price_array;
	   $list_of_prdcts_attributes_id .= $attributes_price_array['products_attributes_id'].",";
		}
	   if (tep_not_null($list_of_prdcts_attributes_id) && $this->cg_id != '0') { 
	 $select_list_of_prdcts_attributes_ids = "(" . substr($list_of_prdcts_attributes_id, 0 , -1) . ")";
 $pag_query = tep_db_query("select products_attributes_id, options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES_GROUPS . " where products_attributes_id IN " . $select_list_of_prdcts_attributes_ids . " AND customers_group_id = '" . $this->cg_id . "'");
 while ($pag_array = tep_db_fetch_array($pag_query)) {
	 $cg_attr_prices[] = $pag_array;
 }

 // substitute options_values_price and prefix for those for the customer group (if available)
 if ($customer_group_id != '0' && tep_not_null($cg_attr_prices)) {
	for ($n = 0; $n < count($attribute_price); $n++) {
	 for ($i = 0; $i < count($cg_attr_prices); $i++) {
		 if ($cg_attr_prices[$i]['products_attributes_id'] == $attribute_price[$n]['products_attributes_id']) {
			$attribute_price[$n]['price_prefix'] = $cg_attr_prices[$i]['price_prefix'];
			$attribute_price[$n]['options_values_price'] = $cg_attr_prices[$i]['options_values_price'];
		 }
	 } // end for ($i = 0; $i < count($cg_att_prices); $i++)
	  }
	} // end if ($customer_group_id != '0' && (tep_not_null($cg_attr_prices))
  } // end if (tep_not_null($list_of_prdcts_attributes_id) && $customer_group_id != '0')
// now loop through array $attribute_price to add up/substract attribute prices

  for ($n = 0; $n < count($attribute_price); $n++) {
		if ($attribute_price[$n]['price_prefix'] == '+') {
		  $this->total += $currencies->calculate_price($attribute_price[$n]['options_values_price'], $products_tax, $qty);
		} else {
		  $this->total -= $currencies->calculate_price($attribute_price[$n]['options_values_price'], $products_tax, $qty);
	}
  } // end for ($n = 0; $n < count($attribute_price); $n++)
	  } // end if (tep_db_num_rows($attribute_price_query))
	} // end if (isset($this->contents[$products_id]['attributes'])) 
  }
}
// EOF SPPC attributes mod

// function attributes_price changed partially according to FalseDawn's post
// [url="http://www.oscommerce.com/forums/index.php?showtopic=139587"]http://www.oscommerce.com/forums/index.php?showtopic=139587[/url]
// changed completely for Separate Pricing Per Customer, attributes mod
function attributes_price($products_id) {
// global variable (session) $sppc_customer_group_id -> class variable cg_id
$this->cg_id = $this->get_customer_group_id();

  if (isset($this->contents[$products_id]['attributes'])) {
	reset($this->contents[$products_id]['attributes']);
   $where = " AND ((";
	while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
	 $where .= "options_id = '" . (int)$option . "' AND options_values_id = '" . (int)$value . "') OR (";
   }
   $where=substr($where, 0, -5) . ')';

   $attribute_price_query = tep_db_query("SELECT products_attributes_id, options_values_price, price_prefix FROM " . TABLE_PRODUCTS_ATTRIBUTES . " WHERE products_id = '" . (int)$products_id . "'" . $where ."");

  if (tep_db_num_rows($attribute_price_query)) {
	   $list_of_prdcts_attributes_id = '';
	   while ($attributes_price_array = tep_db_fetch_array($attribute_price_query)) { 
	   $attribute_price[] =  $attributes_price_array;
	   $list_of_prdcts_attributes_id .= $attributes_price_array['products_attributes_id'].",";
	  }

	   if (tep_not_null($list_of_prdcts_attributes_id) && $this->cg_id != '0') { 
	 $select_list_of_prdcts_attributes_ids = "(" . substr($list_of_prdcts_attributes_id, 0 , -1) . ")";
 $pag_query = tep_db_query("select products_attributes_id, options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES_GROUPS . " where products_attributes_id IN " . $select_list_of_prdcts_attributes_ids . " AND customers_group_id = '" . $this->cg_id . "'");
 while ($pag_array = tep_db_fetch_array($pag_query)) {
	 $cg_attr_prices[] = $pag_array;
	  }

 // substitute options_values_price and prefix for those for the customer group (if available)
 if ($customer_group_id != '0' && tep_not_null($cg_attr_prices)) {
	for ($n = 0; $n < count($attribute_price); $n++) {
	 for ($i = 0; $i < count($cg_attr_prices); $i++) {
		 if ($cg_attr_prices[$i]['products_attributes_id'] == $attribute_price[$n]['products_attributes_id']) {
			$attribute_price[$n]['price_prefix'] = $cg_attr_prices[$i]['price_prefix'];
			$attribute_price[$n]['options_values_price'] = $cg_attr_prices[$i]['options_values_price'];
	}
	 } // end for ($i = 0; $i < count($cg_att_prices); $i++)
  }
	} // end if ($customer_group_id != '0' && (tep_not_null($cg_attr_prices))
  } // end if (tep_not_null($list_of_prdcts_attributes_id) && $customer_group_id != '0')
// now loop through array $attribute_price to add up/substract attribute prices

  for ($n = 0; $n < count($attribute_price); $n++) {
		if ($attribute_price[$n]['price_prefix'] == '+') {
		  $attributes_price += $attribute_price[$n]['options_values_price'];
		} else {
		  $attributes_price -= $attribute_price[$n]['options_values_price'];
		}
  } // end for ($n = 0; $n < count($attribute_price); $n++)
  return $attributes_price;
   } else { // end if (tep_db_num_rows($attribute_price_query))
	 return 0;
}
 }  else { // end if (isset($this->contents[$products_id]['attributes']))
   return 0;
}
  } // end of function attributes_price, modified for SPPC with attributes

function get_products() {
  global $languages_id, $pfs; // PriceFormatterStore added
// BOF Separate Pricing Per Customer
$this->cg_id = $this->get_customer_group_id();
// EOF Separate Pricing Per Customer

  if (!is_array($this->contents)) return false;
// BOF QPBPP for SPPC
  $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;
// EOF QPBPP for SPPC

  $products_array = array();
  reset($this->contents);
  while (list($products_id, ) = each($this->contents)) {
// BOF QPBPP for SPPC
  $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);
// EOF QPBPP for SPPC

	  $products_array[] = array('id' => $products_id,
								'name' => $products['products_name'],
								'model' => $products['products_model'],
								'image' => $products['products_image'],
// BOF QPBPP for SPPC
								'discount_categories_id' => $this->contents[$products_id]['discount_categories_id'],
// EOF QPBPP for SPPC

								'price' => $products_price,
								'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;
}

function show_total() {
  $this->calculate();

  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'];
  }
}

// added for Separate Pricing Per Customer, returns customer_group_id
function get_customer_group_id() {
  if (isset($_SESSION['sppc_customer_group_id']) && $_SESSION['sppc_customer_group_id'] != '0') {
	$_cg_id = $_SESSION['sppc_customer_group_id'];
  } else {
	 $_cg_id = 0;
  }
  return $_cg_id;
}

 }
/*  

###	Class wishlist										###

*/

 class wishlist_class {
var $contents, $total, $weight, $cartID, $content_type;

function wishlist_class() {
  $this->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);
	while (list($products_id, ) = each($this->contents)) {
	  $qty = $this->contents[$products_id]['qty'];
	  $product_query = tep_db_query("select products_id from " . TABLE_CUSTOMERS_BASKET_WISHLIST . " 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_WISHLIST . " (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_WISHLIST_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_WISHLIST . " 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);

  $products_query = tep_db_query("select products_id, customers_basket_quantity from " . TABLE_CUSTOMERS_BASKET_WISHLIST . " 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']);
// attributes
	$attributes_query = tep_db_query("select products_options_id, products_options_value_id from " . TABLE_CUSTOMERS_BASKET_WISHLIST_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_WISHLIST . " where customers_id = '" . (int)$customer_id . "'");
	tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_WISHLIST_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_wishlist, $customer_id;

  $products_id = tep_get_uprid($products_id, $attributes);
  if ($notify == true) {
	$new_products_id_in_wishlist = $products_id;
	tep_session_register('new_products_id_in_wishlist');
  }

  if ($this->in_cart($products_id)) {
	$this->update_quantity($products_id, $qty, $attributes);
  } else {


	$this->contents[] = array($products_id);
	$this->contents[$products_id] = array('qty' => $qty);
// insert into database
	if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_WISHLIST . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . $qty . "', '" . date('Ymd') . "')");

	if (is_array($attributes)) {
	  reset($attributes);
	  while (list($option, $value) = each($attributes)) {
		$this->contents[$products_id]['attributes'][$option] = $value;
// insert into database
		if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_WISHLIST_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 . "')");
	  }
	}
  }
  $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();
}

function update_quantity($products_id, $quantity = '', $attributes = '') {
  global $customer_id;

  if (empty($quantity)) return true; // nothing needs to be updated if theres no quantity, so we return true..

  $this->contents[$products_id] = array('qty' => $quantity);
// update database
  if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET_WISHLIST . " set customers_basket_quantity = '" . $quantity . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");

  if (is_array($attributes)) {
	reset($attributes);
	while (list($option, $value) = each($attributes)) {
	  $this->contents[$products_id]['attributes'][$option] = $value;
// update database
	  if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET_WISHLIST_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "' 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_WISHLIST . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
		tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_WISHLIST_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_WISHLIST . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
	tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_WISHLIST_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);
}

function calculate() {
  $this->total = 0;
  $this->weight = 0;
  if (!is_array($this->contents)) return 0;

  reset($this->contents);
  while (list($products_id, ) = each($this->contents)) {
	$qty = $this->contents[$products_id]['qty'];

// products price
	$product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
	if ($product = tep_db_fetch_array($product_query)) {
	  $prid = $product['products_id'];
	  $products_tax = tep_get_tax_rate($product['products_tax_class_id']);
	  $products_price = $product['products_price'];
	  $products_weight = $product['products_weight'];

	  $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'];
	  }

	  $this->total += tep_add_tax($products_price, $products_tax) * $qty;
	  $this->weight += ($qty * $products_weight);   
	}

// attributes price
// [email protected]
// add-weight-to-product-attributes mod:
// added weight to db query
	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, options_values_weight 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);
		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);
		}
		 if(!empty($attribute_price['options_values_weight'])) {
			// [email protected]
			// add-weight-to-product-attributes mod:
			$this->weight += ($qty * $attribute_price['options_values_weight']);
		} // END if(!empty($attribute_price['options_values_weight'])) {
	   }
	}
  }
}

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() {
  global $languages_id;

  if (!is_array($this->contents)) return false;

  $products_array = array();
  reset($this->contents);
  while (list($products_id, ) = each($this->contents)) {
	$products_query = tep_db_query("select p.products_id, pd.products_name, p.products_model, p.products_image, p.products_price, p.products_weight, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$products_id . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'");

	if ($products = tep_db_fetch_array($products_query)) {
	  $prid = $products['products_id'];
	  $products_price = $products['products_price'];

	  $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'];
	  }
	  $products_array[] = array('id' => $products_id,
								'name' => $products['products_name'],
								'qproduct' => '', // added for Get 1 Free mod
								'model' => $products['products_model'],
								'image' => $products['products_image'],
								'price' => $products_price,
								'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;
}

function show_total() {
  $this->calculate();

  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'];
  }
}

 }  
?>

Edited by Jan Zonjee
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...