Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Cart contents - Product count per category


rratt

Recommended Posts

Posted

Could anybody tell me if there is a way to find out how many items from a given category there are in a shopping cart?

 

I'd like to set up minimum and maximum order quantities per category as well as other stuff.

Posted

What I have come up with so far is below.

I've stated by taking the tep_count_products_in_category() function in general.php and adapting it.

 

The first funtion returns the number of different products in the customers cart from a given category. The second returns the total quantity per category.

 

First of all does anybody know if there is already a method to find this info out in osCommerce?

 

If not than the next step I need to work on is making this work for users who are not logged in. Since these functions look in the databse table customers_basket.

 

 

////
// Return the number of different products per category in a customers basket
// TABLES: customers_basket, products_to_categories
 function tep_count_products_per_category($category_id) {
$products_count = 0;
$products_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS_BASKET . " cb, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where cb.products_id = p2c.products_id and p2c.categories_id = '" . (int)$category_id . "' and cb.customers_id = 2");
$products = tep_db_fetch_array($products_query);
$products_count += $products['total'];
return $products_count;
 }

////
// Return the number quantity of products per category in a customers basket
// TABLES: customers_basket, products_to_categories
 function tep_count_products_qty_per_category($category_id) {
$products_query = tep_db_query("select * from " . TABLE_CUSTOMERS_BASKET . " cb, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where cb.products_id = p2c.products_id and p2c.categories_id = '" . (int)$category_id . "' and cb.customers_id = 2");
while($products = tep_db_fetch_array($products_query)) {
  $qty += $products[customers_basket_quantity];
}
return $qty;
 }

Posted

The two functions below were tests using my own customer account only (id 2).

To make them check for products in the customers basket I've ammended the query as blow

 

From:

$products_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS_BASKET . " cb, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where cb.products_id = p2c.products_id and p2c.categories_id = '" . (int)$category_id . "' and cb.customers_id = 2");

 

To:

$products_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS_BASKET . " cb, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where cb.products_id = p2c.products_id and p2c.categories_id = '" . (int)$category_id . "' and cb.customers_id = '" . (int)$customer_id . "'");

 

Also to get the (int)$customer_id variable to work I had to add in

 

global $customer_id;

 

to the top of both functions.

 

I still need to be able to do the same thing for users who are not logged in.

Posted

I've reworked my first function, to count the number of products per category in the cart, as below.

This seems to work properly. It works for users who are not logged in as well. This ignores attributes though, so with my store for example if I have the same polo shirt in the cart with different colours it's counted as being 1 item from the polo shirt category rather than 2. I'll need to work this out next.

 

I've tried a different approach this time and it's very amateur but it seems to do the job. I can't find information on doing this anywhere - if anyone could improve this I think it would make a useful contribution.

 

This time I've made use of the $cart->get_products(); function but I couldn't get it to work from inside the below function. So I use it as a function paramter instead like this (where 99 is the category I'm looking at):

 

tep_count_products_per_category('99', $cart->get_products());

 

////
// Return the number of different products per category in a customers basket
// TABLES: customers_basket, products_to_categories
 function tep_count_products_per_category($category_id, $products_in_cart) {
// remove attribute info from product id and put into list sperated with a comma
for ($i=0, $n=sizeof($products_in_cart); $i<$n; $i++) {
  $product = substr($products_in_cart[$i]['id'], '0', strcspn($products_in_cart[$i]['id'], '{'));
  $products_list .= "(products_id = '" . $product . "') OR ";
}
// remove the last OR and white space from the list
$products_list =  '(' . substr_replace($products_list,"",-4) . ')';

// select each row in the product to categories table, for each product in the cart belonging to the category id supplied in function paramater
// return the total number of rows selected - this will be the number of products for a given category
$p2c_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . (int)$category_id . "' and " . $products_list);
$p2c = tep_db_fetch_array($p2c_query);
$p2c_count += $p2c['total'];

return $p2c_count;
 }

 

As soon as I have this function worked out I'll try to get the other function, to count the total quantity per category working for usere before they are logged in.

Posted

get the tep_generate_category_path from the admin end in general.php copy it in the general.php file of the catalog end and use it. For each product in the cart call the function to generate the path and this returned path use it as a key into your array to count the products per category.

 

if( !isset($some_array[$returned_path]) )
$some_array[$returned_path] = 1;
else
$some_array[$returned_path]++;

Instead of doing all these complicated things.

Posted

Thanks enigma, appreciate your help very much. I've been going around the houses with this, looking at PHP reference sites etc trying to work it out.

Posted

Enigma, sorry to be a pain but could you exlpain how to go about doing this with the method you suggest?

I'm not that PHP savy and tend to just copy paste code and read up on the functions until I understand them.

 

Thanks for any help you can give.

 

Cheers,

Richard

Posted

so at which point you're stuck? Did you copy the function to the catalog end?

Posted

I've copied the function into the catalogue side (general.php).

 

This is the part I'm stuck on:

For each product in the cart call the function to generate the path and this returned path use it as a key into your array to count the products per category.

 

How would you go about that? I can get a basic foreach() going but the key is beyond my level.

Could you give me an example of how to do this please?

 

Thanks again for you help.

Posted

in your catalog\includes\classes\shopping_cart.php there is a member function called get_products. This function builds the various fields for each product.

 

So right after this:

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

you have the product id. So you could call it right after this like:

		  $temp_array = tep_generate_category_path($prid, 'product');
	  if( is_array($temp_array) ) {
		$key = implode('_',$temp_array);
		if( isset($this->category_counts[$key]) ) {
		  $this->category_counts[$key]++;
		} else {
		  $this->category_counts[$key] = 1;
		}
	  }

Where category_counts is a member variable of the shopping cart class. You need to define it as an array. Then when you call the get_products function this array will be poplulated. The $key var stores the full cpath. If you only need the categories_id you could use the last element of the array if I remember correctly. Finally to access the array its $cart->category_counts

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...