Adding Boilerplate text to the Product Description


Relevance: Zen Cart™ 1.3.0 - 1.3.9, 1.5.x. The same approach can be used in prior versions.
Cost: Free, but donation appreciated
Related: Using Files Containing Boilerplate Text in the Product Description

To save typing repetitive text in the product description field, and to facilitate easier mass changes of boilerplate text, it is advantageous to use PHP defined constants. However, Zen Cart does not support the use of PHP within the description field.

Here's how to add it yourself.

Create your custom template if you haven't already done so. We'll call the template "custom" in this example.

Note: If you are using Zen Cart 1.5.5 or higher, your template name will be "responsive_classic" if you have not changed it.

Create a customized copy of
includes/templates/custom/templates/tpl_product_info_display.php
(from includes/templates/template_default/templates/tpl_product_info_display.php)

You will be defining your boilerplate strings in a file called
includes/languages/english/extra_definitions/my_defines.php
Each string will have a PHP "define" statement, and all the strings will be listed in an array. In this way, users can simply add and delete strings by editing this one file, rather than having to dig into the internals of Zen Cart.

We'll use a simple example:
<?php

$descr_stringlist = array("PHP_ONE_WEEK_DELAY", "PHP_TWO_WEEK_DELAY", "PHP_THREE_WEEK_DELAY"); 

define('PHP_ONE_WEEK_DELAY', 'These decals will be shipped in one week after receiving your order.'); 
define('PHP_TWO_WEEK_DELAY', 'These decals will be shipped in two weeks after receiving your order.'); 
define('PHP_THREE_WEEK_DELAY', 'These decals will be shipped in three weeks after receiving your order.'); 

?>

We've defined three constants and added each one to a list called descr_stringlist.

Now go back to
includes/templates/custom/templates/tpl_product_info_display.php
Change
 <!--bof Product description -->
<?php if ($products_description != '') { ?>
<div id="productDescription" class="productGeneral biggerText"><?php echo stripslashes($products_description); ?></div>
<?php } ?>
<!--eof Product description -->
to
 <!--bof Product description -->
<?php if ($products_description != '') { ?>
<div id="productDescription" class="productGeneral biggerText">
<?php 
$stripped_products_description = $products_description;
foreach ($descr_stringlist as $varname) { 
   $stripped_products_description = str_replace($varname, constant($varname), $stripped_products_description);
}
?>
<?php echo stripslashes($stripped_products_description); ?></div>
<?php } ?>
<!--eof Product description -->

Now to use this logic, all you need to do is type PHP_ONE_WEEK_DELAY in your product description (under Admin - Catalog - Categories and Products), and when you display the product info page, it will be changed.

If you display the Description in any of the other lists (Product Listing, New Listing, Featured Listing, All Listing), you will need to modify code that handles the product description for listing pages (in includes/modules/your_template/product_listing.php) in the same way. Alternately, you can just set "Display Product Description" to 0 on those screens (available under Admin - Configuration).

If you just want to add some boilerplate text based on category, product name, or some other field, you can do that too, without having to type it in to each product in the Admin interface. For instance, here's some logic that adds the string CAT1_DESC - which you have defined in the file includes/languages/english/extra_definitions/my_defines.php - to every product in category 1:

<!--bof Product description -->
<?php if (($products_description != '') || ($current_category_id == 1) ) { ?>
<div id="productDescription" class="productGeneral biggerText">
<?php 
   $stripped_products_description = $products_description;
   if ($current_category_id == 1)  {
      $stripped_products_description = $stripped_products_description . CAT1_DESC;
   }
   echo stripslashes($stripped_products_description); 
   echo '</div>';
?>
<?php } ?> 
<!--eof Product description -->


Adding boilerplate to every product whose name includes some specific string is similar, but instead of using a simple numeric equality check, a regular expression must be used. This code fragment will add FISH_STR to the product description of every product whose product name ends with the string " fish."
<!--bof Product description -->
<?php 
if (preg_match("/.* fish$/", $products_name)) {
   $products_description = $products_description . FISH_STR;
}
if (($products_description != '') { 
   echo '<div id="productDescription" class="productGeneral biggerText">';
   echo stripslashes($products_description); 
   echo '</div>';
}
?>
<!--eof Product description -->


What if you really want to add links and not static strings? Well, this is done in a very similar way. Instead of using the static strings described above, in the file includes/languages/english/extra_definitions/my_defines.php add the code
<?php
$descr_stringlist = array("RED_LINK", "BLUE_LINK"); define('RED_LINK', '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=2') . '">' . 'red cars' . '</a>'); define('BLUE_LINK','<a href="' . zen_href_link(FILENAME_ADVANCED_SEARCH_RESULT, 'keyword=search+string')     . '">' . 'something else' . '</a>'); ?>

... and instead of using the PHP_*_WEEK_DELAY strings above, you would use one of these strings in your description.

This tip was developed in July 2006, and was first submitted to the Zen Cart Support Forum in this thread on July 30, 2006.

More Tips!

I will describe how to use this technique on the featured, new, all and product listing pages, using the first example (using the strings PHP_ONE_WEEK_DELAY, etc. in the description).
  • To do this for the featured products page, customize the template file
    includes/templates/template_default/templates/tpl_modules_products_featured_listing.php
    
    Look at where $display_products_description is created. Change this logic to look like this:
          if (PRODUCT_FEATURED_LIST_DESCRIPTION != '0') {
    
             $disp_text = zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id']);
             $disp_text = zen_clean_html($disp_text);
             foreach ($descr_stringlist as $varname) {
                $disp_text = str_replace($varname, constant($varname), $disp_text);
             }
             $more_text = '<a href="' . zen_href_link(zen_get_info_page($featured_products->fields['products_id']), 'products_id=' . $featured_products->fields['products_id']) . '"> ' . MORE_INFO_TEXT . '</a>';
             $display_products_description = stripslashes(zen_trunc_string($disp_text, 150, $more_text));
          } else {
            $display_products_description = '';
          }
    
  • To do this for the all products page, customize the template file
    includes/templates/template_default/templates/tpl_modules_products_all_listing.php
    
  • To do this for the new products page, customize the template file
    includes/templates/template_default/templates/tpl_modules_products_new_listing.php
    
  • To do this for the product listing page is more complicated, because the description is created inline in a large string (and is done in an if branch and an else branch). You need to move this code outside the if/else and use the technique described above. Customize the file includes/modules/product_listing.php and change
            case 'PRODUCT_LIST_NAME':
            $lc_align = '';
            if (isset($_GET['manufacturers_id'])) {
              $lc_text = '<h3 class="itemTitle"><a href="' . zen_href_link(zen_get_info_page($listing->fields['products_id']), 'products_id=' . $listing->fields['products_id']) . '">' . $listing->fields['products_name'] . '</a></h3><div class="listingDescription">' . zen_trunc_string(zen_clean_html(stripslashes(zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id']))), PRODUCT_LIST_DESCRIPTION) . '</div>' ;
            } else {
              $lc_text = '<h3 class="itemTitle"><a href="' . zen_href_link(zen_get_info_page($listing->fields['products_id']), ($_GET['cPath'] > 0 ? 'cPath=' . $_GET['cPath'] . '&' : '') . 'products_id=' . $listing->fields['products_id']) . '">' . $listing->fields['products_name'] . '</a></h3><div class="listingDescription">' . zen_trunc_string(zen_clean_html(stripslashes(zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id']))), PRODUCT_LIST_DESCRIPTION) . '</div>';
            }
            break;
    
    to
    
            case 'PRODUCT_LIST_NAME':
            $lc_align = '';
            $desc = zen_clean_html(stripslashes(zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id'])));
            foreach ($descr_stringlist as $varname) { 
              $desc = str_replace($varname, constant($varname), $desc);
            }
            $desc = zen_trunc_string($desc,PRODUCT_LIST_DESCRIPTION);
            if (isset($_GET['manufacturers_id'])) {
              $lc_text = '<h3 class="itemTitle"><a href="' . zen_href_link(zen_get_info_page($listing->fields['products_id']), 'products_id=' . $listing->fields['products_id']) . '">' . $listing->fields['products_name'] . '</a></h3><div class="listingDescription">' . $desc . '</div>'; 
            } else {
              $lc_text = '<h3 class="itemTitle"><a href="' . zen_href_link(zen_get_info_page($listing->fields['products_id']), ($_GET['cPath'] > 0 ? 'cPath=' . $_GET['cPath'] . '&' : '') . 'products_id=' . $listing->fields['products_id']) . '">' . $listing->fields['products_name'] . '</a></h3><div class="listingDescription">' . $desc . '</div>'; 
            }
            break;