You are here:  » Product in multiple categories


Product in multiple categories

Submitted by Tobix on Sun, 2022-01-09 17:52 in

How can I place a product in multiple categories?
Category example
Single door refrigerator | dispenser category

thank you

Submitted by support on Mon, 2022-01-10 09:46

Hi Tobix,

The `category` field can be modified to hold a comma separated list of multiple category names to contain the product; firstly however as the field is quite short the length needs to be increased with a dbmod.php script as follows:

<?php
  
require("includes/common.php");
  
$sql "DROP INDEX search_name_category_price_id ON `".$config_databaseTablePrefix."products`";
  
database_queryModify($sql,$result);
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."products` CHANGE `category` `category` VARCHAR(255) NOT NULL";
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

(run once from top level of Price Tapestry installation and then delete file)

Edit includes/admin.php and look for the following code at line 198:

      $importRecord["category"] = tapestry_normalise($importRecord["category"]);

...and REPLACE with:

      $importRecord["category"] = tapestry_normalise($importRecord["category"],",");

Then look for the following comment around line 414:

    /* apply category hierarchy mappings */

...and REPLACE with:

    if ($importRecord["category"])
    {
      $importRecord["category"] = ",".$importRecord["category"].",";
    }
    /* apply category hierarchy mappings */

Edit categories.php and look for the following code beginning at line 72:

      if (database_querySelect($sql,$rows))
      {
        foreach($rows as $product)
        {
          if ($product["category"])
          {
            $item = array();
            $item["name"] = $product["category"];
            $item["href"] = tapestry_indexHREF("category",$product["category"]);
            $atoz["items"][] = $item;
          }
        }
      }

...and REPLACE with:

      $allCategories = array();
      if (database_querySelect($sql,$rows))
      {
        foreach($rows as $product)
        {
          $product["category"] = trim($product["category"],",");
          $categories = explode(",",$product["category"]);
          foreach($categories as $category)
          {
            $allCategories[$category] = 1;
          }
        }
      }
      ksort($allCategories);
      foreach($allCategories as $category => $v)
      {
        if ($category)
        {
          $item = array();
          $item["name"] = $category;
          $item["href"] = tapestry_indexHREF("category",$category);
          $atoz["items"][] = $item;
        }
      }

Edit search.php and look for the following code at line 68:

      $priceWhere .= " AND category = '".database_safe($categoryFilter)."' ";

...and REPLACE with:

      $priceWhere .= " AND category LIKE '%,".database_safe($categoryFilter).",%' ";

Then look for the following code at line 160:

          $where .= " ".$field." = '".database_safe($parts[$i])."' ";

...and REPLACE with

          if ($field == "category")
          {
            $where .= " `category` LIKE '%,".database_safe($parts[$i]).",%' ";
          }
          else
          {
            $where .= " ".$field." = '".database_safe($parts[$i])."' ";
          }

Edit html/searchfilters.php and look for the following code at line 117:

          foreach($rows1 as $row)

...and REPLACE with:

          $newRows1 = array();
          foreach($rows1 as $row)
          {
            $categories = explode(",",$row["category"]);
            foreach($categories as $category)
            {
              $category = trim($category);
              if ($category)
              {
                $newRows1[$category]["category"] = $category;
              }
            }
          }
          ksort($newRows1);
          $rows1 = $newRows1;
          foreach($rows1 as $row)

Finally edit admin/categories.php and look for the following code at line 16:

    widget_validate("name",TRUE,"normalised");

...and REPLACE with:

    widget_validate("name",TRUE,"");

This will support a category field from a feed that is already comma separated; otherwise to set multiple categories for an existing product create a new category mapping with the required multiple categories comma separated e.g.

Single Door Refrigerator,Dispenser

...and on the configuration page for the mapping enter an exact match (or as required) e.g.

=Samsung 375L

Cheers,
David.
--
PriceTapestry.com

Submitted by shaunmac on Mon, 2023-03-20 20:41

I've been trying for some time now without success to implement this. After I edit the files my category page seems to work ok but when you click on a category it displays (no product). Not sure how to change the navbar to accept multiple categories. I am using 23/03A if you have time my link is {link saved} so you can see what I'm talking about. Thank You

Submitted by support on Tue, 2023-03-21 09:49

Hi Shaun,

My apologies there is a second modification required to search.php. I have added to the above with line numbering for 22/10A, for 23/03A look for the following code at line 118:

          $where .= " ".$field." = '".database_safe($parts[$i])."' ";

...and REPLACE with:

          if ($field == "category")
          {
            $where .= " `category` LIKE '%,".database_safe($parts[$i]).",%' ";
          }
          else
          {
            $where .= " ".$field." = '".database_safe($parts[$i])."' ";
          }

Cheers,
David.
--
PriceTapestry.com