Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

[CONTRIBUTION] Multiple Categories per product quick entry


Guest

Recommended Posts

Multiple Categories per product quick entry (Download here)

 

This is a cool contribution by the authors Pavel Rojkov, Snowbird and CommLog.

 

The installation instructions can be somewhat confusing, therefore I have updated the instructions and included the code snippets below, just so you can see what the changes should look like.

 

All credits goto the original code writers.

 

	 case 'insert_product':
  case 'update_product':

	  // copy image only if modified
	$products_image = new upload('products_image');
	$products_image->set_destination(DIR_FS_CATALOG_IMAGES);
	if ($products_image->parse() && $products_image->save()) {
	  $products_image_name = $products_image->filename;
	} else {
	  $products_image_name = (isset($HTTP_POST_VARS['products_previous_image']) ? $HTTP_POST_VARS['products_previous_image'] : '');
	}
	if (isset($HTTP_POST_VARS['edit_x']) || isset($HTTP_POST_VARS['edit_y'])) {

 

								  'manufacturers_id' => tep_db_prepare_input($HTTP_POST_VARS['manufacturers_id']));

	  if (isset($products_image_name) && tep_not_null($products_image_name) && ($products_image_name != 'none')) {
		$sql_data_array['products_image'] = tep_db_prepare_input($products_image_name);
	  }

	  if ($action == 'insert_product') {

 

 

		  if ($action == 'insert_product') {
		$insert_sql_data = array('products_date_added' => 'now()');

		$sql_data_array = array_merge($sql_data_array, $insert_sql_data);

		tep_db_perform(TABLE_PRODUCTS, $sql_data_array);
		$products_id = tep_db_insert_id();

	  } elseif ($action == 'update_product') {
		$update_sql_data = array('products_last_modified' => 'now()');

		$sql_data_array = array_merge($sql_data_array, $update_sql_data);

		tep_db_perform(TABLE_PRODUCTS, $sql_data_array, 'update', "products_id = '" . (int)$products_id . "'");

	#delete categories saved in the tables
	tep_db_query("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '". $products_id . "'");
	  }

# create loop here to insert rows for multiple categories
$selected_catids = $HTTP_POST_VARS['categories_ids'];
 if (!$selected_catids) $selected_catids[0] = 0;
	  foreach ($selected_catids as $current_category_id)
	  {
		  tep_db_query("insert into " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id, categories_id) values ('" . $products_id . "', '" . $current_category_id . "')");
	  }

	  $languages = tep_get_languages();
	  for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
		$language_id = $languages[$i]['id'];

 

	$manufacturers_array = array(array('id' => '', 'text' => TEXT_NONE));
$manufacturers_query = tep_db_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS . " order by manufacturers_name");
while ($manufacturers = tep_db_fetch_array($manufacturers_query)) {
  $manufacturers_array[] = array('id' => $manufacturers['manufacturers_id'],
								 'text' => $manufacturers['manufacturers_name']);
}

# get selected categories
$categories_query_selected = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . $HTTP_GET_VARS['pID'] . "'");
$categories_array_selected = array(array('id' => ''));
while ($categories = tep_db_fetch_array($categories_query_selected)) {
  $categories_array_selected[] = array('id' => $categories['categories_id']);
}

$categories_array = array(array('id' => '', 'text' => TEXT_NONE));
#Categories list displays only for one languge (Default is English)
$language_id = 1;
$categories_array = tep_get_category_tree(); // added by R Calder

$form_action = ($HTTP_GET_VARS['pID']) ? 'update_product' : 'insert_product';

$tax_class_array = array(array('id' => '0', 'text' => TEXT_NONE));

 

//--></script>
<?php echo tep_draw_form('new_product', FILENAME_CATEGORIES, 'cPath=' . $cPath . '&pID=' . $HTTP_GET_VARS['pID'] . '&action='. $form_action, 'post', 'enctype="multipart/form-data"'); ?>
<table border="0" width="100%" cellspacing="0" cellpadding="2">

Link to comment
Share on other sites

Once this contribution is installed, simply goto your admin control panel and add a product, when you come to adding the product to a category, to select multiple categories, simply hold down the <CONTROL> Key on your keyboard and select the categories you want the product to appear in.

Edited by stu2000
Link to comment
Share on other sites

  • 5 weeks later...

Made a correction to correctly display "Top" as a category... wasn't in the latest version I just downloaded from Aug 22nd.

 

Also, I'm currently working on printing the categories as checkboxes instead of the multi select to be a little more forgiving ;) i'll keep ya posted.

 

In admin/includes/functions/html_output.php file

 

 

 

ADD at the end of the file Before " ?>"

 

 

 

// Output a form multiple select menu

 function tep_draw_mselect_menu($name, $values, $selected_vals, $params = '', $required = false) {

$field = '<select name="' . $name . '"';

if ($params) $field .= ' ' . $params;

$field .= ' multiple>';

for ($i=0; $i<sizeof($values); $i++) {

if ($values[$i]['id'])

{

	  $field .= '<option value="' . $values[$i]['id'] . '"';

	  if ( ((strlen($values[$i]['id']) > 0) && ($GLOBALS[$name] == $values[$i]['id'])) ) {

		$field .= ' SELECTED';

	  }

		else 

	{

		for ($j=0; $j<sizeof($selected_vals); $j++) {

			if ($selected_vals[$j]['id'] == $values[$i]['id'])

			{

				$field .= ' SELECTED';

			}

		}

	}

} else { $field .= '<option value="0"'; } // Added 09/20/06 To correctly offer "Top" as category

  $field .= '>' . $values[$i]['text'] . '</option>';

}

$field .= '</select>';



if ($required) $field .= TEXT_FIELD_REQUIRED;



return $field;

 }

 

 

 

=================================

Edited by pointlessjon
Link to comment
Share on other sites

Here we go...

 

  // Output a cat select  checkbox menu

 function tep_draw_chbox_cat_menu($name, $values, $selected_vals) {

  $num_cols	= 5; // Specify number of columns here

$td_width = ( ( 1 / $num_cols ) * 100); // Auto calculate percentage of width for tds for balance

$field = '<table width="100%" cellpadding="10" style="border:1px solid #CCCCCC;">'; // start the table

//Cycle through cats and print checkboxes...
for ($i=0; $i<sizeof($values); $i++) {

if ( ($i % $num_cols) == 0 ) { $field .= '<tr>'; } // Start new table row when appropriate

$field .= '<td width="'.$td_width.'%" valign="top" class="main" style="background:#e3e3e3;">';

if ($values[$i]['id'])

{

	  $field .= '<input name="' . $name . '" type="checkbox" value="' . $values[$i]['id'] . '"';

	  if ( ((strlen($values[$i]['id']) > 0) && ($GLOBALS[$name] == $values[$i]['id'])) ) {

		$field .= ' checked="checked"';

	  }

		else 

	{

		for ($j=0; $j<sizeof($selected_vals); $j++) {

			if ($selected_vals[$j]['id'] == $values[$i]['id'])

			{

				$field .= ' checked="checked"';

			}

		}

	}

} else  { 
	$field .= '<input name=' . $name . '" type="checkbox" value="0"';  // print Top as checkbox
}

  $field .= '> ' . $values[$i]['text'] . '</td>'; // print Cat name

  if ( ($i % $num_cols) == ($num_cols - 1) ) { $field .= '</tr>'; }

}

$field .= '</table>'; // end the table

return $field;

 }

 

Add that to the bottom of admin/includes/functions/html_output.php

 

and then in admin/categories.php replace:

 

<?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . ' ' . tep_draw_mselect_menu('categories_ids[]', $categories_array, $categories_array_selected, 'size=10'); ?>

 

with:

 

Select Categories ?<br />
<div id="categoryDiv"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . ' ' . tep_draw_chbox_cat_menu('categories_ids[]', $categories_array, $categories_array_selected, 'size=10'); ?></div>

Link to comment
Share on other sites

  • 1 month later...

Alright,

 

My website has MANY categoris. Which means I have like a million checkboxes....

 

Any way around this?

 

What I basically want, is the ability toadd one item, to any/all categories I choose. My website will be neatly categorized.

Link to comment
Share on other sites

  • 3 months later...
Alright,

 

My website has MANY categoris. Which means I have like a million checkboxes....

 

Any way around this?

 

What I basically want, is the ability toadd one item, to any/all categories I choose. My website will be neatly categorized.

 

I have the same problem here... The contri is good, but not for stores with many categories like mine (4000+)

 

I have also another problem: when i copie a product witch is linked by multi categories (done with this contribution), then will the new product not linked on the multi categories, but only on the categorie the product is in at that moment.

Link to comment
Share on other sites

  • 1 month later...

Firstly great contrib!

Problem im having is when i add a product to say three dif categories, it shows up three times on the whats new page?

Even tho they all link to the same product...?

can anyone suggest what i should so so it only shows the one product ? no matter how many categories its added to...

maybe defaults to the first category it was first added to? or alphabetical would be fine...

 

thanks in advance if anyone can give me some assistance with this!

 

Kym

Link to comment
Share on other sites

  • 2 months later...

Hi, has anyone else come across a problem with this contribution where the homepage (and all the other pages) of your site goes down after adding products to multiply categories? Its displays a HTTP 500 error, then after about an hour or two the site loads correctly.

 

This is a huge problem as adding products to multiply categories is vital for me, but at the moment customers cannot access my site if I am adding products via admin.

 

If you have come across this please let me know how you overcame it.

 

Thanks, David.

Link to comment
Share on other sites

This contribution is amazing... it is just what I needed. THANK YOU!!!!!!!

 

I have been trying to figure out a way to do something, using this contribution.

 

Say that I have a product that I am listing in 6 different categories.

 

Is there a way that when I pull up the product, somewhere in or after the description I can have a listing displayed showing all the categories this product is listed in?

 

Any help that can be provided, I would appreciate

 

 

 

Mark

Link to comment
Share on other sites

I do not mean to be rude..... I do not know if anybody was able to review my above question.....

 

Any help you can provide I would appreciate it....

 

"I have been trying to figure out a way to do something, using this contribution.

 

Say that I have a product that I am listing in 6 different categories.

 

Is there a way that when I pull up the product, somewhere in or after the description I can have a listing displayed showing all the categories this product is listed in?"

 

 

Thanks

 

Mark

Link to comment
Share on other sites

  • 2 weeks later...
Just wondering why the Preview was eliminated. Is it directly related to the contribution?

 

Hi there

Great contribution!

Like HSMagic, I am also wondering why the Preview function was eliminated? I've looked through the source code and can't determine why it was removed.

Any ideas? I've tried putting it back on, but without success. Would be good to see it in use again.

Thanks,

Awbrys

Link to comment
Share on other sites

  • 3 months later...
  • 2 weeks later...

Slightly off topic the contribution works great, but ive been using it to populate several categories with the one product them change them to suit that exact category. SO my problem is how can i get osc admin orders page to tell me which category did the item come from (all named the same, but are slightly different depending on category :(). Any help would be greatly appreciated.

Link to comment
Share on other sites

  • 3 weeks later...
  • 8 months later...

Installed this but am seeing this error:

 

1062 - Duplicate entry '4880-39' for key 1

 

insert into products_to_categories (products_id, categories_id) values ('4880', '39')

 

Anyone had this problem before.

 

Ideally, there would be a mod that would use ajax and have categories that would expand into subcats for checking and instant saving in the product edit field. Anyone know of anything like this? Up for the challenge and a donation? :rolleyes:

 

Carry

Link to comment
Share on other sites

  • 5 months later...

My Message is nearly the same

 

1062 - Duplicate entry '113-34' for key 1

 

insert into products_to_categories (products_id, categories_id) values ('113', '34')

 

[TEP STOP]

 

I have sent an email using the support email and then saw this thread, but it looks like none of the mods visit this thread anyway. :blink:

Debbie Harrison

 

Link to comment
Share on other sites

  • 2 weeks later...

Help

 

I hope there is someone on this forum that can help. I am using the multiple categories at http://addons.oscommerce.com/info/1537 and have finished tying my products up in it.

 

However, now when I add products to the catalog panel, I cannot find them again. Despite me using the drop down tree, they display under Home > product number rather than the sub-category I choose.

 

Example can be found at: http://rocket.unitedhosting.co.uk/~admin34...products_id=850

 

As further insight I did cancel several products off a couple of weeks ago. as these were duplicates. Would this have affected them? Or is this written in PHP 4 or 5?

 

I hope you can help me as I am truly stumped. i am meant to be training someone to use this next week and may have to cancel it as nothing can be found once added.

 

Please help. i can provide further info if you PM me.

 

Best regards

Edited by dvharrison

Debbie Harrison

 

Link to comment
Share on other sites

  • 2 weeks later...
Great Contribution

 

It doesn't works correctly on osCommerce 2.2 rc 2a

 

On a existyng product it works perfectly, but when you try to add a new product... the product is not added.

 

Regards!

 

I Can't edit my previous post... the contribution works perfectly, I haven't installa correctly :blush:

 

Regards!

Link to comment
Share on other sites

  • 1 year later...

Excellent contribution,

 

May I ask a question?

Does this add-on give a parent - child - child - child search facility? For example I am building a parts business for motor vehicles and I want to have the client choose vehicle make, model, engine type and year. I can´t seem to find a solution on os commerce. Perhaps you can enlighten me?

 

Thanks

Link to comment
Share on other sites

  • 2 months later...

Excellent Contribution,

 

I have added one product in multiple category, but i want to give sorting position for the product in each category, but sort position should be different for different category. could any one help me for this.

 

Thanks

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.

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...