You are here:  » Adding extra class to selected category images


Adding extra class to selected category images

Submitted by nanaz on Thu, 2016-01-21 11:16 in

Hi David,

On my new site I use multiple feeds, most of them containing 1 productcategory. About half of these feeds contain categories with productimages I want to style in a different way than the productimages in the rest of the categories, by adding an extra class to them.

So what I need is a filter to specify these categories manually (by adding their ID or name) and add it to the WP Pricetapestry Settings with a placeholder %EXTRACLASS% or something like that - with an extra whitespace in front of the phpscript to keep the code neat :-)

<img class='pto_product_image%EXTRACLASS%' src='%IMAGE_URL%' />

I really hope you can help me out!

Cheers, Marlies

Submitted by support on Thu, 2016-01-21 11:33

Hello Marlies,

If you're happy to manage the list of categories for which to add the extra class in the code it would be very straight forward to implement this at the code level. With the modification to the Product / Main template exactly as per your example, in the plugin pto_product.php look for the following code at line 392:

  $html_product = str_replace("%IMAGE_URL%",$product->image_url,$html_product);

...and REPLACE with:

  $html_product = str_replace("%IMAGE_URL%",$product->image_url,$html_product);
  $extraClassCategories = array(
    "Category 1",
    "Category 2",
    "Category 3"
    ); // no comma after last ^
  if (in_array($product->category,$extraClassCategories))
  {
    $html_product = str_replace("%EXTRACLASS%"," extraClassName",$html_product);
  }
  else
  {
    $html_product = str_replace("%EXTRACLASS%","",$html_product);
  }

Edit the list of categories for which to apply the extra class name in the $extraClassCategories array, and in line 9 of the replacement change extraClassName as required to correspond with the entry in your CSS...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by nanaz on Thu, 2016-01-21 13:28

Thank you David, exactly what I needed!

Could you also provide me with the same script for pto_search.php? I tried to do it myself but just can't get it right. Thank you!

Submitted by support on Thu, 2016-01-21 13:54

Hello Marlies,

A couple of changes required in pto_search.php since the category is not included in the SELECT by default. First look for the following code at line 516:

      $each = str_replace("%IMAGE_URL%",$row->image_url,$each);

...and REPLACE with:

      $each = str_replace("%IMAGE_URL%",$row->image_url,$each);
      $extraClassCategories = array(
        "Category 1",
        "Category 2",
        "Category 3"
        ); // no comma after last ^
      if (in_array($row->category,$extraClassCategories))
      {
        $each = str_replace("%EXTRACLASS%"," extraClassName",$each);
      }
      else
      {
        $each = str_replace("%EXTRACLASS%","",$each);
      }

And then the following code at line 357:

    $sql2 = "SELECT id,name,normalised_name,image_url,description,price,rating FROM `".$pto_config_databaseTablePrefix."products` WHERE id IN (".$in.")";

...and REPLACE with:

    $sql2 = "SELECT id,name,normalised_name,image_url,description,price,rating,category FROM `".$pto_config_databaseTablePrefix."products` WHERE id IN (".$in.")";

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by nanaz on Thu, 2016-01-21 16:19

Hi David,

I've implemented the last code in pto_search.php, but it doesn't produce an extra class in the search results image. Added the same code to pto_featured.php and there it's working perfectly.

I've got another addition (Adding infopages to categories http://www.pricetapestry.org/node/329) in pto_search.php that also doesn't work - the categoryname has no content in WP. Maybe it's related?

Cheers, Marlies

Submitted by support on Fri, 2016-01-22 09:21

Hello Marlies,

Since search results are generated using a summary query, it is possible that the category field is not populated for every product. Rather than re-query where required, a better solution is normally to "fill-in" the category field where empty but is populated for another product record with the same name as a post-import process.

This is easy to plumb in to the backfill reviews process which takes place at the end of any import operation. If this is likely to be the case here, and you'd like to give this a go, edit includes/admin.php of the Price Tapestry installation and look for the following code at line 766:

    $sql = "UPDATE `".$config_databaseTablePrefix."products` SET rating='0',reviews='0'";

...and REPLACE with:

    $sql = "SELECT DISTINCT(category),name FROM `".$config_databaseTablePrefix."products`";
    if (database_querySelect($sql,$rows))
    {
      foreach($rows as $row)
      {
        $sql = "UPDATE `".$config_databaseTablePrefix."products` SET category='".database_safe($row["category"])."' WHERE name='".database_safe($row["name"])."'";
        database_queryModify($sql,$result);
      }
    }
    $sql = "UPDATE `".$config_databaseTablePrefix."products` SET rating='0',reviews='0'";

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by nanaz on Fri, 2016-01-22 12:08

Hi David,

I understand the problem could be caused by some products not having a categoryname, but I typed in 90% of the Categorynames myself at the registration of the feeds. When I look at the productrecords in the DB with PhpMyAdmin, the categorynames are all filled. The categoryID is always 0 with every record though.

I implemented your update sql code in admin.php, but I had to revert since it slowed down and eventually hung the import of the feeds.

Thank you for your patience!

Submitted by support on Fri, 2016-01-22 12:39

Hello Marlies,

Ah sorry about that - there is an additional change required in pto_search.php - look for the following code at line 382:

  $pto_searchResults[$k]->rating = $rows3[$product->id]->rating;

...and REPLACE with:

  $pto_searchResults[$k]->rating = $rows3[$product->id]->rating;
  $pto_searchResults[$k]->category = $rows3[$product->id]->category;

Regarding the info/ mod, that doesn't rely on the category value from the database at that point so would indicate that maybe the same issue as in this comment - check if that's the case, if not let me know and I'll check it out further with you...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by nanaz on Fri, 2016-01-22 14:37

Hi David,

Super, that last one did the trick! Verry happy customer :-)

On regards to the infopages in WP: the basehref is the same as described in Support Info in the admin (I use a folder named ptlk instead of pto for the script)
/var/www/vhosts/example.com/httpdocs/ptlk/
I uploaded into the root of the PT installation the required folder category_info + html files with the appropriate Categoryname.

The standalone version of PT works fine with the code from http://www.pricetapestry.com/node/3237, the infopage shows up on the categorypages.

Hope you can help me out again!

Submitted by support on Fri, 2016-01-22 14:45

Hello Marlies,

I've just realised that there is a discrepancy between;

http://www.pricetapestry.com/node/3237
...which is based on a folder name of:
info_category/

...and:

http://www.pricetapestry.org/node/329
...which is based on a folder name of:
category_info/

So if everything is working in the standalone Price Tapestry installation (/ptlk/) that would indicate that you have the original folder name of info_category in place, so if that's the case, where you have added the following line in the REPLACEment to pto_search.php;

     $filename = $pto_config_externalPath.$parts[0]."_info/".$parts[1].".html";

...make the switch here to:

     $filename = $pto_config_externalPath."info_".$parts[0]."/".$parts[1].".html";

Hope this helps! Once you're up and running I'll consolidate both threads so that they related to the same info directory names...

Cheers,
David.
--
PriceTapestry.com

Submitted by nanaz on Fri, 2016-01-22 15:55

Hi David,

You're right about the different folder names, though it's not the case for me, sorry. I had already changed the name of the folder in the PT standalone script, therefore it worked.

My folder is called category_info and I use
$filename = $pto_config_externalPath.$parts[0]."_info/".$parts[1].".html";
for WP in pto_search.php and
$filename = "category_info/".$parts[1].".html";
in search.php.

So no luck yet...

Submitted by support on Fri, 2016-01-22 16:21

Hello Marlies,

I wonder if it's an open_basedir restriction...

Instead of:

$filename = $pto_config_externalPath.$parts[0]."_info/".$parts[1].".html";

...try:

$filename = "ptlk/".$parts[0]."_info/".$parts[1].".html";

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by nanaz on Sat, 2016-01-23 11:57

Hi David,

I've been going over this a loooong time, but I'm at a loss. I've tried all kinds of combinations, hardcoded the $filename in different ways:

{code saved}

As an experiment I changed some code in pto_search.php and printed $pto_config_externalPath on the page and got {code saved} as result in the html.

Then I replaced the complete pto-folder with a newly download one, then applied the code, but no luck. Nothing happens. Just to make sure I also uploaded the file bloemen.html in lowercase.

I changed the WP theme just to make sure the infopage text is not hidden somewhere, eventhough I also scan the source of the html-page everytime I apply a change.

I added an else statement, so I guess there should be an output on every page, but nothing is printed.

{code saved}

I'm sure I'm overlooking something, it shouldn't be so difficult. But at this point I have no clue what to try next. Maybe you have an idea?

Thank you!

Cheers, Marlies

Submitted by support on Sat, 2016-01-23 18:05

Hi Marlies,

It looks like you do have the code in the correct position but the line number referred to in node 329 refers to PriceTapestry.org for WordPress version 1, for version 2 the original code:

  $html .= pto_search_html();

...is at line 390, so the category_info mod needs to be inserted immediately prior to this point - I wondered whether this might be related to the issue when you mentioned that your ELSE case was not generating any output either.

If still no joy, if you could email me your modified pto_search.php I'll check it out further and follow up by email with you...

Cheers,
David.
--
PriceTapestry.com