You are here:  » Retrieve only the active sizes from products table


Retrieve only the active sizes from products table

Submitted by affinity-group on Wed, 2019-01-09 07:46 in

Hi David,

I was wondering if you'd have any suggestions on how to retrieve a list of sizes that are available in the current selection.

For example, I have around 1M products in our webshop, that all contain sizes like:
S, XXL, XXXL
19,20,21,22,23,24,25,26,27
4,5,8½,6,7½,7,6½,5½,8,4½,9,3½

Also, I've created a table with sizes (category_sizes) I want to display per main category, so lets say category 'women' can have these sizes:
S, M, XL, XXL, 18, 19, 20 (each in a single row)

I hope you understand the problem. Exploding the sizes in the pt_products table would result in a very large array of distinct sizes, and then comparing it to the category_sizes table entries would simply take way too long.

Maybe the category_sizes is not the way to go, but I'm not sure. Unfortunately it's pretty hard to normalize the sizes since the feeds we use contain very precarious data.

Hope you have any ideas :-)

Thanks in advance

Submitted by support on Wed, 2019-01-09 09:37

Hi,

It would probably be best to build up the list of sizes at import time - if you create two new global variables (for example by adding to config.advanced.php) for example:

  $sizes_regexp = "/\b(S|M|L|XL|XXL|19|20|21)\b/";
  $sizes_categories = array();

...including in the $sizes_regexp value (a regular expression) all the sizes you are looking for, pipe separated between the brackets.

Then edit includes/admin.php and look for the following comment at line 450:

    /* niche mode */

...and REPLACE with:

    if ($import_record["category"])
    {
      global $sizes_regexp;
      global $sizes_categories;
      preg_match_all($sizes_regexp,$import_record["name"],$matches);
      if (count($matches[0]))
      {
        foreach($matches[0] as $size)
        {
          $sizes_categories[$import_record["category"]][$size] = $size;
        }
      }
    }
    /* niche mode */

Then, at the end of a full import you will have the $sizes_categories array populated with every unique category with sizes and each size (note how it is populated using $size as both the key and value to prevent duplication).

You could then add code at the end of cron.php or as required, using foreach for example;

  foreach($sizes_categories as $category => $sizes)
  {
    foreach($sizes as $size)
    {
      ...
    }
  }

Hope this points you in the right direction...

Cheers,
David.
--
PriceTapestry.com

Submitted by affinity-group on Mon, 2019-01-14 16:09

This works exactly as I'd hope :-)

Thanks again David!