Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / Specific text in product listings based on category

Specific text in product listings based on category

Locked

Views: 1,588

Results 1 to 16 of 16
This thread is locked. New replies are disabled.
27 Oct 2008, 5:24 PM
#1
alexwoolfson avatar

alexwoolfson

Zen Follower

Join Date:
Sep 2008
Posts:
97
Plugin Contributions:
0

Specific text in product listings based on category

Hello,

I am going to be selling comics in e-book form and, as a promotion, I am offering a 100% discount on certain comics to my mailing list subscribers. These comics will live in their regular category and then also be linked into a Free Comics category.

I'd like to add some specific language to the product listing for comics that are linked into the Free Comics category. Specifically, I'd like to add the words "Free for newsletter subscribers!" underneath the "$2.99" price I have listed for the comic. And then I'd also like to add some instructions underneath the Add to Cart and Attribute Radio Buttons explaining how to get the free comic and how to subscribe.

I'll probably be able to figure out how to add the custom text to the right places of the PHP file. What I'm not sure how to do is structure the IF-ELSE statements in PHP to only add these bits of text for products linked to the Free Comics category. I'm not sure how to reference the Free Comics category in the statement -- and frankly, I wouldn't mind a little help structuring the statement itself. I have some experience modifying PHP with my WordPress install, but I'm still a beginner. (But eager to learn!)

(And of course if there is a module that does this or some smart Admin manipulation, please let me know. I've been searching around the forums for a bit, but the search terms I've been using haven't pulled up what I need.)

Any help would be greatly appreciated.

Cheers!

Alex

28 Oct 2008, 12:45 AM
#2
alexwoolfson avatar

alexwoolfson

Zen Follower

Join Date:
Sep 2008
Posts:
97
Plugin Contributions:
0

Re: Specific text in product listings based on category

Well, I'm part of the way there now. I've added

<?php if (in_array($cPath,explode(",",'4')) ) { ?>
<?php echo "<h2>FREE for Newsletter Subscribers!</h2>"; ?>
<?php } ?>

to at the end of the Product Price Block of a copy of the tpl_document_product_info_display.php.

This gets me the text when I am looking at the Product while in the Free Comics category (which has a Category ID of "4"). But when I click at the same product in another category, the text is not there. Not surprising considering the command I wrote, but not what I need.

How can I get it to check to see if a product is LINKED to Category ID 4? Is there a way?

Any help anyone could offer me would be greatly appreciated.

Thanks!

Alex

28 Oct 2008, 1:03 AM
#3
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Specific text in product listings based on category

Tp know if a product is linked to category 4, you would have to do a SQL query on the database for products_to_categories or something like that.
Others can tell you how to accomplish this.

Alternatively, you could include a distinctive bit of text in the description of every product that will be linked, and test $product_description for the presence of that text. Then you could act on that information with the relevant added text display.

28 Oct 2008, 1:10 AM
#4
alexwoolfson avatar

alexwoolfson

Zen Follower

Join Date:
Sep 2008
Posts:
97
Plugin Contributions:
0

Re: Specific text in product listings based on category

Thank you for your reply, Glenn. :smile: While I think the second method would be simplest code-wise, if someone knew how to construct that SQL Query, that would really help me with exactly what I need.

Does anyone know how to construct that Query? (Or can give me a link?)

Thanks again!

Alex

28 Oct 2008, 1:38 AM
#5
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Specific text in product listings based on category

/includes/functions/functions_categories.php has a function that looks like it would do the job:```php
function zen_product_in_category($product_id, $cat_id) {
global $db;
$in_cat=false;
$category_query_raw = "select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . "
where products_id = '" . (int)$product_id . "'";

$category = $db->Execute($category_query_raw);

while (!$category->EOF) {
  if ($category->fields['categories_id'] == $cat_id) $in_cat = true;
  if (!$in_cat) {
    $parent_categories_query = "select parent_id from " . TABLE_CATEGORIES . "
                                where categories_id = '" . $category->fields['categories_id'] . "'";

    $parent_categories = $db->Execute($parent_categories_query);

//echo 'cat='.$category->fields['categories_id'].'#'. $cat_id;

    while (!$parent_categories->EOF) {
      if (($parent_categories->fields['parent_id'] !=0) ) {
        if (!$in_cat) $in_cat = zen_product_in_parent_category($product_id, $cat_id, $parent_categories->fields['parent_id']);
      }
      $parent_categories->MoveNext();
    }
  }
  $category->MoveNext();
}
return $in_cat;

}


$cat4link = zen_product_in_category($product_id, 4);

where $product_id is fed the value of the current product's id. It should return "true" or "false".
Looking closer, I think it will return "true" or the id of the category the product really is in.
28 Oct 2008, 2:19 AM
#6
alexwoolfson avatar

alexwoolfson

Zen Follower

Join Date:
Sep 2008
Posts:
97
Plugin Contributions:
0

Re: Specific text in product listings based on category

That does look promising! I tried this code right under the Product Price Block of that template file to see what the result was

<?php
$cat4link = zen_product_in_category($product_id, 4);
echo $cat4link;
?>

but nothing came out of it. Nothing was echoed to my web page.

Did I make a stupid PHP syntax mistake?

(I see other examples of using 'true' in IF statements in the same file. I can see how to make this work -- if only I can get a result... :smile:)

Thank you for your help with this! It's much appreciated!

Alex

28 Oct 2008, 2:39 AM
#7
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Specific text in product listings based on category

You need to adapt the code or code above it to get the product info into the $product_id variable. Like

$product_id = whatever variable holds the id;

Without that, $product_id will be blank/null/0 and you will get no results.

28 Oct 2008, 2:54 AM
#8
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Specific text in product listings based on category

It looks like $_GET['products_id'] is the correct variable to use. Try```php

<?php $cat4link = zen_product_in_category($_GET['products_id'], 4); echo $cat4link; ?>
28 Oct 2008, 2:59 AM
#9
alexwoolfson avatar

alexwoolfson

Zen Follower

Join Date:
Sep 2008
Posts:
97
Plugin Contributions:
0

Re: Specific text in product listings based on category

Ah, OK. Learning a lot here. :smile:

So I changed the code to

<?php
$cat4link = zen_product_in_category($_GET['products_id'], 4);
echo $cat4link;
?>

and now it seems I am getting a result of "1" for the products linked in Category 4 and a result of " " when it's not. Very promising.

Thank you for your help. I'm now going to try to create my 'IF' statement....

Cheers,

Alex

EDIT: Just checked my mailbox and saw you had posted the answer already! Sorry for being redundant here, but yes, that seems to work!

28 Oct 2008, 3:11 AM
#10
alexwoolfson avatar

alexwoolfson

Zen Follower

Join Date:
Sep 2008
Posts:
97
Plugin Contributions:
0

Re: Specific text in product listings based on category

This is what I came up with:

<?php 
$cat4link = zen_product_in_category($_GET['products_id'], 4);
if ($cat4link == 1) { ?>
<?php echo "<h2>FREE for Newsletter Subscribers!</h2>"; ?>
<?php } ?>

It seems to work! Thank you for all your help! :clap:

Alex

28 Oct 2008, 3:23 AM
#11
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Specific text in product listings based on category

Excellent!

29 Oct 2008, 7:20 AM
#12
alexwoolfson avatar

alexwoolfson

Zen Follower

Join Date:
Sep 2008
Posts:
97
Plugin Contributions:
0

Re: Specific text in product listings based on category

OK, so I've now gone through and added my "FREE for Newsletter Subscribers!" to the New Products, Featured Products and the All Products Listings that are found in the Categories sidebox.

I'd like to also make sure it shows up (for the products linked to Category 4) in the listings one gets when one clicks on individual categories links in this sidebox, but I'm having a hard time figuring out which template file controls the Price display for those Category Listings. Likewise, I'd also like to add it to products linked to Category 4 that appear in the big New Products box on the main page. (As well as the big Featured Box -- if there such a thing -- haven't used it yet...) -- yet I'm also having a hard time finding those files. A search in the Developers Took Kit for "$display_products_price" hasn't pulled anything up other than the files I've already changed.

What files do I need to change to add this? Any help would be appreciated.

Thanks!

Alex

Oh, and just in case anyone else wants to do anything like this -- this is how I changed the New Products listing files, etc. Just add in the Commented code...

tpl_modules_products_all_listing.php

Around line 77:

      if ((PRODUCT_ALL_LIST_PRICE != '0' and zen_get_products_allow_add_to_cart($products_all->fields['products_id']) == 'Y') and zen_check_show_prices() == true) {
        $products_price = zen_get_products_display_price($products_all->fields['products_id']);
        $display_products_price = TEXT_PRICE . ' ' . $products_price . str_repeat('<br clear="all" />', substr(PRODUCT_ALL_LIST_PRICE, 3, 1)) . (zen_get_show_product_switch($products_all->fields['products_id'], 'ALWAYS_FREE_SHIPPING_IMAGE_SWITCH') ? (zen_get_product_is_always_free_shipping($products_all->fields['products_id']) ? TEXT_PRODUCT_FREE_SHIPPING_ICON . '<br />' : '') : '');
//BOF OF FREE FOR NEWSLETTER SUBSCRIBERS - PART 1 (SEE BELOW)     
        $cat4link = zen_product_in_category($products_all->fields['products_id'], 4);
//EOF OF FREE FOR NEWSLETTER SUBSCRIBERS - PART 1 (SEE BELOW)
        
      } else {
        $display_products_price = '';
      }

Around Line 185

                  if ($disp_sort_order->fields['configuration_key'] == 'PRODUCT_ALL_LIST_PRICE') {
                    echo $display_products_price;
    //BOF OF FREE FOR NEWSLETTER SUBSCRIBERS -- PART 2 (SEE ABOVE)
                    if ($cat4link == 1) { 
                  echo "<strong>FREE for Newsletter Subscribers!</strong>"; 
                  }
    //EOF OF FREE FOR NEWSLETTER SUBSCRIBERS -- PART 2 (SEE ABOVE)             
                  }

For tpl_modules_products_featured_listing.php, the second part of the code is the same but the first part should be changed to

 //BOF OF FREE FOR NEWSLETTER SUBSCRIBERS - PART 1 (SEE BELOW)     
        $cat4link = zen_product_in_category($featured_products->fields['products_id'], 4);
//EOF OF FREE FOR NEWSLETTER SUBSCRIBERS - PART 1 (SEE BELOW)

And for tpl_modules_products_new_listing.php, it should be

 //BOF OF FREE FOR NEWSLETTER SUBSCRIBERS - PART 1 (SEE BELOW)     
        $cat4link = zen_product_in_category($products_new->fields['products_id'], 4);
//EOF OF FREE FOR NEWSLETTER SUBSCRIBERS - PART 1 (SEE BELOW)

Hope that's helpful to somebody! :smile:

29 Oct 2008, 7:26 AM
#13
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Specific text in product listings based on category

The main category product listing pages can be modified in
/includes/modules/your_template/product_listing.php.

29 Oct 2008, 7:43 AM
#14
alexwoolfson avatar

alexwoolfson

Zen Follower

Join Date:
Sep 2008
Posts:
97
Plugin Contributions:
0

Re: Specific text in product listings based on category

Gosh, you're up late! Isn't it like 3:30 A.M. over there? Well, your insomnia is my gain -- thank you for referring me to that file. :smile:

I've looked it over and it's quite different than the other files I've changed. I'm looking for the ECHO statements to let me know where I should ECHO out my new text, but I'm not finding any. Just a $lc_text string that seems to keep being reused throughout.

If it's not too much trouble, how would I go about adding the text I want using this file?

I imagine that I would change my code that says this:

 //BOF OF FREE FOR NEWSLETTER SUBSCRIBERS - PART 1 (SEE BELOW)     
        $cat4link = zen_product_in_category($products_new->fields['products_id'], 4);
//EOF OF FREE FOR NEWSLETTER SUBSCRIBERS - PART 1 (SEE BELOW)

to

 //BOF OF FREE FOR NEWSLETTER SUBSCRIBERS - PART 1 (SEE BELOW)     
        $cat4link = zen_product_in_category($listing->fields['products_id'], 4);
//EOF OF FREE FOR NEWSLETTER SUBSCRIBERS - PART 1 (SEE BELOW)

and insert it somewhere around the place it talks about price.

But where would I add

     //BOF OF FREE FOR NEWSLETTER SUBSCRIBERS -- PART 2 (SEE ABOVE)
                    if ($cat4link == 1) { 
                  echo "<strong>FREE for Newsletter Subscribers!</strong>"; 
                  }
    //EOF OF FREE FOR NEWSLETTER SUBSCRIBERS -- PART 2 (SEE ABOVE)

Would I need to somehow concatenate it (if that's the right work) into the $lc_text string? Or am I overthinking it?

Thanks again for your help!

Alex

31 Oct 2008, 6:50 AM
#15
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Specific text in product listings based on category

Yes, it would go into $lc_text. That variable accumulates the output for the listing element being assembled and dumps it into the final output array, before going back and repeating for the next element.

Edit this around line 107 in product_listing.php:```php
case 'PRODUCT_LIST_PRICE':
$lc_price = zen_get_products_display_price($listing->fields['products_id']) . '<br />';
$lc_align = 'right';
$lc_text = $lc_price;

    // more info in place of buy now
    $lc_button = '';

with code similar to your description to getphp
case 'PRODUCT_LIST_PRICE':
$lc_price = zen_get_products_display_price($listing->fields['products_id']) . '<br />';
$lc_align = 'right';
$lc_text = $lc_price;
//BOF OF FREE FOR NEWSLETTER SUBSCRIBERS
$cat4link = zen_product_in_category($listing->fields['products_id'], 4);
if ($cat4link == 1) {
$lc_text .= "<strong>FREE for Newsletter Subscribers!</strong><br />";
}
//EOF OF FREE FOR NEWSLETTER SUBSCRIBERS
// more info in place of buy now
$lc_button = '';


You could also replace the whole content of your added code with this line:```php
        $lc_text .= (zen_product_in_category($listing->fields['products_id'], 4) == 1)? '<strong>FREE for Newsletter Subscribers!</strong><br />':''; 
```which says "if the product is in cat 4, add this to $lc_text, else add nothing."
31 Oct 2008, 7:20 AM
#16
alexwoolfson avatar

alexwoolfson

Zen Follower

Join Date:
Sep 2008
Posts:
97
Plugin Contributions:
0

Re: Specific text in product listings based on category

Glenn, you ROCK! :thumbsup:

That works great! And... I've learned more PHP from you. So, you taught me a bit of fishing as well.

Thank you so much for your help! Now this works exactly as I had hoped -- and things will be clear to my shoppers.

Yay! :clap:

Alex