You are here:  » Color Mapping


Color Mapping

Submitted by sociallyfamous on Tue, 2015-03-24 17:56 in

Its me again.

I am slowly getting a good idea on how to manage a huge amount of products and removing the limit in the category mapping tool was key along with some basic filters. Here I am facing another challenge.

I would like to do color mapping to be able map all the shades of blue out there to blue or even to dark.

David, I need your help here again. Do you see how I could tweak an existing mapping functionality to use it for color: Brand Mapping or Product Mapping?

Do you maybe already have something up your sleeve in that direction?

Submitted by support on Wed, 2015-03-25 09:11

Hi,

Whilst an independent tool can be created using either Category or Brand mapping as a template, it's straight forward to modify either to apply to another field.

As Brand Mapping is likely to be the least heavily utilised, to apply Brand Mapping to the `color` field, simply edit includes/admin.php and look for the following comment at line 261:

  /* apply brand mappings */

...and insert immediately before that point the following new code to apply brand mappings to the `color` field:

    if (isset($admin_importBrandMappings["=".$importRecord["color"]]))
    {
      $importRecord["color"] = $admin_importBrandMappings["=".$importRecord["color"]];
    }
    else
    {
      foreach($admin_importBrandMappings as $k => $v)
      {
        if (substr($k,0,1) !== "=")
        {
          $found = 0;
          $words = explode(" ",$k);
          foreach($words as $word)
          {
            if ($word)
            {
              if (strpos($importRecord["color"],$word) !== FALSE) $found++;
            }
          }
          if ($found == count($words))
          {
            $importRecord["color"] = $v;
            break;
          }
        }
      }
    }

Additionally, you can also modify the search tool on the Brand Mapping configuration page to include the color field. To do this, edit admin/helper.php and look for the following code at line 20:

  $sql = "SELECT DISTINCT(".$field.") FROM `".$config_databaseTablePrefix."products` WHERE ".$field." LIKE '%".database_safe($q)."%' ORDER BY ".$field;

(note: this is after your previous modification to remove the limit of 6 results)

...and REPLACE with:

  if ($field=="brand")
  {
    $sql = "SELECT DISTINCT(brand) FROM `".$config_databaseTablePrefix."products` WHERE brand LIKE '%".database_safe($q)."%'";
    database_querySelect($sql,$rows1);
    $sql = "SELECT DISTINCT(color) AS brand FROM `".$config_databaseTablePrefix."products` WHERE color LIKE '%".database_safe($q)."%'";
    database_querySelect($sql,$rows2);
    $rows = array_merge($rows1,$rows2);
  }
  else
  {
    $sql = "SELECT DISTINCT(".$field.") FROM `".$config_databaseTablePrefix."products` WHERE ".$field." LIKE '%".database_safe($q)."%' ORDER BY ".$field;
    database_querySelect($sql,$rows);
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stevebi on Wed, 2015-03-25 09:22

Hi David,

This seems to be a great modification for those who have big databases.

As I understand the color mapping will be applied through Brand mapping or it can be applied separately at Admin Panel as Color Mapping?

Is it possible to do the same thing with sizes field?

Best Regards

Steve

Submitted by support on Wed, 2015-03-25 10:05

Hi Steve,

It would be applied through Brand Mapping - exactly the same modifications can be applied to extend to a size field also - just replace "color" with "size" in the changes to includes/admin.php, and in admin/helper.php, to search brand / color / size; use the following alternative REPLACEment:

  if ($field=="brand")
  {
    $sql = "SELECT DISTINCT(brand) FROM `".$config_databaseTablePrefix."products` WHERE brand LIKE '%".database_safe($q)."%'";
    database_querySelect($sql,$rows1);
    $sql = "SELECT DISTINCT(color) AS brand FROM `".$config_databaseTablePrefix."products` WHERE color LIKE '%".database_safe($q)."%'";
    database_querySelect($sql,$rows2);
    $sql = "SELECT DISTINCT(size) AS brand FROM `".$config_databaseTablePrefix."products` WHERE size LIKE '%".database_safe($q)."%'";
    database_querySelect($sql,$rows3);
    $rows = array_merge($rows1,$rows2,$rows3);
  }
  else
  {
    $sql = "SELECT DISTINCT(".$field.") FROM `".$config_databaseTablePrefix."products` WHERE ".$field." LIKE '%".database_safe($q)."%' ORDER BY ".$field;
    database_querySelect($sql,$rows);
  }

It's worth bearing in mind that Mapping is essentially just a more convenient way of applying a global Search and Replace filter against particular fields - in some cases filters may be more appropriate if a mapping is only required with respect to a particular feed, and don't forget that Search and Replace RegExp can be used to match multiple search values e.g.

Search:
/(light blue|dark blue|navy)/i
Replace:
Blue

Cheers,
David.
--
PriceTapestry.com

Submitted by stevebi on Wed, 2015-03-25 10:10

Thanks a lot David!!!

I will give a shot and come back with my feedback.

Just to point that until now I have applied Search and Replace RegExp per feed and in some cases suffered a lot due to the not well organized XML feed

Cheers

S

Submitted by sociallyfamous on Wed, 2015-03-25 10:46

Thanks David, I will be trying this today. As brand mapping is less relevant to me as those are usually well done in most feeds, I will be doing just that!

Submitted by sociallyfamous on Wed, 2015-03-25 11:05

David,

I have applied the modification but I seem to get almost random brands inside the helper tools instead for color. What could be the reason for that?

Submitted by support on Wed, 2015-03-25 12:06

Hi,

Sorry about that - a slightly more comprehensive replacement to admin/helper.php is required, corrected above, and also in the equivalent follow-up to stevebi...

Cheers,
David.
--
PriceTapestry.com

Submitted by sociallyfamous on Wed, 2015-03-25 13:00

Thanks for the amendments. This works inside the helper tool now for every new color I try. For all previous color searches I still get the brands... Is this some kind cache that has been created I guess. Any experience with that?

Submitted by support on Wed, 2015-03-25 13:09

That does sound cache related - the AJAX response from admin/helper.php doesn't include the cache prevention headers as sent by the main admin pages, but these can be added easily. In that file, look for the following code at line 12:

  require("../includes/common.php");

...and REPLACE with:

  require("../includes/common.php");
  header('Cache-Control: no-cache, private, must-revalidate, max-stale=0, post-check=0, pre-check=0, no-store');
  header('Pragma: no-cache');
  header('Expires: Thu, 1 Jan 1970 00:00:00 GMT');

If the files have been cached locally that should do the trick after clearing your browser's cache, but if it's an ISP proxy that has cached the response you may need to wait for its cached copy to clear but all should be fine after that.

Cheers,
David.
--
PriceTapestry.com

Submitted by marco@flapper on Mon, 2016-10-03 12:25

Great modification. Is it case insensitive? If not can it be made like that?

An example:
If I currently have "Dark Blue" I can map that:
=Dark Blue

But if there is a new feed or change so that it is "dark blue" or "DARK BLUE"? It would be nice if it was case insensitive.

Submitted by support on Mon, 2016-10-03 12:54

Hi Marco,

Sure - in the modifications to includes/admin.php look for the following code:

  if (strpos($importRecord["color"],$word) !== FALSE) $found++;

...and REPLACE with (using stripos instead of strpos)

  if (stripos($importRecord["color"],$word) !== FALSE) $found++;

...will make it case-insensitive.

Cheers,
David.
--
PriceTapestry.com