You are here:  » Category Update Without Feed Re-load

Active Forum Topics


Category Update Without Feed Re-load

Submitted by ChrisNBC on Fri, 2017-09-08 08:03 in

Hi David,

Hope all is going well.

I wondered if you could tell me if there is a way to run just category (hierarchy) mapping for all feeds without re-loading the data?

Thanks in advance.

Best regards
Chris

Submitted by support on Fri, 2017-09-08 11:03

Hi Chris,

This can be done by taking the Category Hierarchy Mapping application code from the import record handler and applying it against the live products table - create a new script containing the following code saved as admin/categories_hierarchy_apply.php:

<?php
  
require("../includes/common.php");
  
$sql "UPDATE `".$config_databaseTablePrefix."products` SET categoryid='0'";
  
database_queryModify($sql,$result);
  
$admin_importCategoryHierarchyMappings = array();
  
$sql "SELECT * FROM `".$config_databaseTablePrefix."categories_hierarchy`";
  if (
database_querySelect($sql,$rows))
  {
    foreach(
$rows as $category)
    {
      
$alternates explode("\n",$category["alternates"]);
      foreach(
$alternates as $alternate)
      {
        
$alternate trim($alternate);
        
$admin_importCategoryHierarchyMappings[$alternate] = $category["id"];
      }
    }
  }
  
$sql "SELECT id,category FROM `".$config_databaseTablePrefix."products`";
  
database_querySelect($sql,$importRecords);
  foreach(
$importRecords as $importRecord)
  {
    if (isset(
$admin_importCategoryHierarchyMappings["=".$importRecord["category"]]))
    {
      
$importRecord["categoryid"] = $admin_importCategoryHierarchyMappings["=".$importRecord["category"]];
    }
    else
    {
      foreach(
$admin_importCategoryHierarchyMappings as $k => $v)
      {
        if (
substr($k,0,1) !== "=")
        {
          
$found 0;
          
$words explode(" ",$k);
          foreach(
$words as $word)
          {
            if (
$word)
            {
              if (
strpos($importRecord["category"],$word) !== FALSE$found++;
            }
          }
          if (
$found == count($words))
          {
            
$importRecord["categoryid"] = $v;
            break;
          }
        }
      }
    }
    if (isset(
$importRecord["categoryid"]))
    {
      
$sql "UPDATE `".$config_databaseTablePrefix."products`
                SET categoryid='"
.$importRecord["categoryid"]."'
                WHERE id='"
.$importRecord["id"]."'";
      
database_queryModify($sql,$result);
    }
  }
  
header("Location: categories_hierarchy.php");
  exit();
?>

And then to create a link to apply changes at the bottom of the Category Hierarchy Mapping page, edit admin/categories_hierarchy.php and look for the following code at line 203:

  widget_formEnd();

...and REPLACE with:

  widget_formEnd();
  print "<p><a href='categories_hierarchy_apply.php'>Apply</a></p>";

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Tue, 2017-09-12 13:16

Hi David,

Thanks for the above code which works perfectly.

Best regards
Chris

Submitted by stevebi on Thu, 2017-09-14 17:51

Hello David,

I have applied the code also.

It saved me a lot of time.

Thank you for your support.

Cheers

Steve

Submitted by zoder on Mon, 2019-01-07 14:39

Hi David,

This code help me a lot, thanks.
But when I import a big CSV datafeed then click apply button, it give me a http 500 error within 10s,
I changed connect timeout to 1 hour in these files my.cnf and php.ini, and it seems there is no use.
What should I do to avoid this error?

Thanks!

Submitted by support on Mon, 2019-01-07 15:06

Hi and welcome to the forum!

I wasn't sure if you configured the maximum execution time in php.ini to zero to enable unlimited time, you could try adding to the top of admin/categories_hierarchy_apply.php just after the opening php tag:

  set_time_limit(0);

Otherwise the reason for a 500 Internal Server Error is normally reported to your web server's error log which you may have access to via your hosting control panel, or from the command line in a logs folder or at system level for example /var/log/httpd/error_log. If that's the case and an error is being reported but you're not sure where the problem lies; or if there is no output but still not working let me know and I'll check it out further with you...

Cheers,
David.
--
PriceTapestry.com