You are here:  » multiple brands in the product


multiple brands in the product

Submitted by MarcoCH on Wed, 2020-11-25 08:44 in

hello David

I have a product feed where the brands are listed for the product, e.g. Brand1, Brand2, Brand3

Do I have the opportunity to link all brands individually? Do you understand what I mean?

regards
Marco

Submitted by support on Wed, 2020-11-25 11:42

Hi Marco,

This is relatively easy to set-up (several users have asked to do the same for categories). Firstly, how are the multiple brands represented in your feeds - are they in separate fields e.g. BRAND1,BRAND2 etc. or a single, already comma separated list of brand names for example?

Cheers,
David.
--
PriceTapestry.com

Submitted by MarcoCH on Wed, 2020-11-25 13:38

hello David

Unfortunately, all brands are in one column, so Brand1, Brand2, Brand3 separated by a comma.

Greeting
Marco

Submitted by support on Wed, 2020-11-25 14:41

Hi Marco,

Actually that makes it easier as the mod is to make the brand field a comma separated list rather than a single brand. Firstly, as the field is only VARCHAR(64) by default this needs to be increased to hold a comma separated list that may be longer so create and run the following dbmod.php script to apply the changes;

<?php
  set_time_limit
(0);
  require(
"includes/common.php");
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."products`
           CHANGE `brand` `brand` VARCHAR(255) NOT NULL default ''"
;
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

Then edit includes/admin.php and look for the following code at line 222:

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

...and REPLACE with:

      $importRecord["brand"] = ",".tapestry_normalise($importRecord["brand"],",").",";

Edit brands.php and look for the following code at line 10:

    foreach($rows as $product)

...and REPLACE with:

    $newRows = array();
    foreach($rows as $row)
    {
      $brands = explode(",",$row["brand"]);
      foreach($brands as $brand)
      {
        $brand = trim($brand);
        if ($brand)
        {
          $newRows[$brand]["brand"] = $brand;
        }
      }
    }
    ksort($newRows);
    $rows = $newRows;
    foreach($rows as $product)

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

    $priceWhere .= " AND brand = '".database_safe($brandFilter)."' ";

...and REPLACE with:

    $priceWhere .= " AND brand LIKE '%,".database_safe($brandFilter).",%' ";

And then the following code at (now) line 160:

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

...AND replace WITH:

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

Finally edit html/searchfilters.php and look for the following code at line 138:

          print "<div class='small-12 medium-2 columns'>";

...and REPLACE with:

          $newRows1 = array();
          foreach($rows1 as $row)
          {
            $brands = explode(",",$row["brand"]);
            foreach($brands as $brand)
            {
              $brand = trim($brand);
              if ($brand)
              {
                $newRows1[$brand]["brand"] = $brand;
              }
            }
          }
          ksort($newRows1);
          $rows1 = $newRows1;
          print "<div class='small-12 medium-2 columns'>";

(and then re-import all feeds for the changes to take effect...)

Cheers,
David.
--
PriceTapestry.com

Submitted by MarcoCH on Wed, 2020-11-25 18:46

hello David

it works so far, only I have adjusted the searchfilters.php and product.php files. Can you help me here?

searchfilters.php

                <select class="hide-for-large" name="brandFilter">
                  <option value=""><?= translate("All") ?></option>
                    <?php foreach($rows1 as $row): ?>
                      <option value="<?= htmlspecialchars($row["brand"],ENT_QUOTES,$config_charset) ?>" <?php print ($brandFilter==$row["brand"] ? 'selected="selected"' ""?>>
                        <?= $row["brand"] ?>
                      </option>
                    <?php endforeach; ?>
                </select>

product.php

          <?php if ($product_main["brand"]):  ?>
            <tr>
              <td>
                <strong>Marke:</strong>
              </td>
              <td>
                <a href="/marke/<?php print ($product_main["brand"]); ?>/"><?php print ($product_main["brand"]); ?></a>
              </td>
            </tr>
        <?php endif; ?>

Greets
Marco

Submitted by support on Thu, 2020-11-26 08:43

Hello Marco,

In the brand filter section of html/searchfilters.php where you have this line:

                     <?php foreach($rows1 as $row): ?>

...REPLACE with:

                     <?php
                     $newRows1 = array();
                     foreach($rows1 as $row)
                     {
                       $brands = explode(",",$row["brand"]);
                       foreach($brands as $brand)
                       {
                         $brand = trim($brand);
                         if ($brand)
                         {
                           $newRows1[$brand]["brand"] = $brand;
                         }
                       }
                     }
                     ksort($newRows1);
                     $rows1 = $newRows1;
                     ?>
                     <?php foreach($rows1 as $row): ?>

And in product.php where you are currently displaying the brand:

                 <a href="/marke/<?php print ($product_main["brand"]); ?>/"><?php print ($product_main["brand"]); ?></a>

...have a go with something like this to display all brands in a list;

                 <?php
                 $brands = explode(",",$product_main["brand"]);
                 print "<ul>";
                 foreach($brands as $brand)
                 {
                   print "<li><a href='/marke/".tapestry_hyphenate($brand)."/'>".$brand."</a></li>";
                 }
                 print "</ul>";
                 ?>

Cheers,
David.
--
PriceTapestry.com

Submitted by MarcoCH on Thu, 2020-11-26 09:40

hello David

as always perfect ... :-)

Thanks David

Submitted by MarcoCH on Thu, 2020-11-26 20:45

hello David

Is the same for the category?

Greeting
Marco

Submitted by support on Fri, 2020-11-27 08:39

Hi Marco,

Yes it's pretty much identical mods, first the dbmod.php to increase the size of the category field as required;

<?php
  set_time_limit
(0);
  require(
"includes/common.php");
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."products`
           CHANGE `category` `category` VARCHAR(255) NOT NULL default ''"
;
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

Then 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"],",").",";

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

    foreach($rows as $product)

...and REPLACE with:

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

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).",%' ";

And then further to the brand mods you will have the following code at line 160:
<code>
          if ($field=="brand")

...replace WITH:

          if (($field=="brand") || ($field=="category"))

Then in html/searchfilters.php locate the same section as modified for brand (note there are separate sections for category hierarchy and basic category filters) and where you have the opening foreach() loop as follows:

                     <?php foreach($rows1 as $row): ?>

...REPLACE with:

                     <?php
                     $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;
                     ?>
                     <?php foreach($rows1 as $row): ?>

Cheers,
David.
--
PriceTapestry.com

Submitted by MarcoCH on Mon, 2020-11-30 20:06

hello David

How come, the first brand or category works fine, but the second and subsequent brands and categories generate a "-" in front of the file. The link works for the file "product.php", but the link does not work for the overview / brand / and / category /.

First brand or category: https://example.com/brand/Alpinamed/
Second brand or category: https://example.com/brand/-Biofarm/

Do you understand me?

regards
Marco

Submitted by support on Tue, 2020-12-01 08:14

Hi Marco,

Ah - I didn't include the trim() call in the product page display code, instead of the above have a go with:

                 <?php
                 $brands = explode(",",$product_main["brand"]);
                 print "<ul>";
                 foreach($brands as $brand)
                 {
                   $brand = trim($brand);
                   if ($brand)
                   {
                     print "<li><a href='/marke/".tapestry_hyphenate($brand)."/'>".$brand."</a></li>";
                   }
                 }
                 print "</ul>";
                 ?>

Cheers,
David.
--
PriceTapestry.com

Submitted by MarcoCH on Tue, 2020-12-01 18:33

hello David

Now all are on the left without "-", but the links no longer work. Has something to do with the import, right?

$importRecord["brand"] = ",".tapestry_normalise($importRecord["brand"],",").",";

Greeting
Marco

Submitted by support on Wed, 2020-12-02 08:15

Hi Marco,

It's probably best to remove the leading / trailing SPACE characters at import time, so in includes/admin.php where you now have the following code at line 222:

      $importRecord["brand"] = ",".tapestry_normalise($importRecord["brand"],",").",";

...REPLACE with:

      $importRecord["brand"] = ",".tapestry_normalise($importRecord["brand"],",").",";
      $importRecord["brand"] = str_replace(array(", "," ,"),",",$importRecord["brand"]);

And for category, where you have the following code at line 198:

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

...REPLACE with:

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

Cheers,
David.
--
PriceTapestry.com

Submitted by MarcoCH on Wed, 2020-12-02 16:23

hello David
Now it fits, perfectly.

Thanks and Greetings
Marco

Submitted by MarcoCH on Mon, 2020-12-07 19:31

hello David

Can I also limit the output to e.g. 128 characters and ,...?

print "<a href='/marke/".tapestry_hyphenate($brand)."/'>".$brand."</a>, ";

Doesn't work for me.

regards
Marco

Submitted by support on Tue, 2020-12-08 08:35

Hi Marco,

Have a go with something like this;

<?php
  $brands 
explode(",",$product_main["brand"]);
  
$html "";
  
$length 0;
  foreach(
$brands as $brand)
  {
    
$brand trim($brand);
    if (
$brand)
    {
      if (
$html$html .= ", ";
      
$html .= "<a href='/marke/".tapestry_hyphenate($brand)."/'>".$brand."</a>";
      
$length += (strlen($brand)+2);
      if (
$length 128)
      {
        
$html .= ",...";
        break;
      }
    }
  }
  print 
$html;
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by MarcoCH on Tue, 2020-12-08 09:19

Perfect David!

Thanks!