Zen Cart custom software development, Zen Cart modules, Zen Cart Expert eCommerce with Zen Cart!

Using Files Containing Boilerplate Text in the Zen Cart Product Description

Donate: This is free software. Show your appreciation by supporting my efforts. Donate

Relevance: Zen Cart™ 1.3.0 - 1.3.9, 1.5.x.

Cost: Free, but donation appreciated

Related:

*** Please note: This code still works but is not recommended; using Define Page Anywhere is more flexible and easier. This documentation is provided for historical reference only.
Including entire files of boilerplate in the description can be done in a manner similar to that described in a prior tip called Adding Boilerplate Text to the Product Description. However, instead of the keyword being replaced by a string, I'll show you how to replace a keyword with a file.

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)

Create the directory
includes/languages/<your-language>/boilerplate
You will populate this directory with files whose names are <some-keyword>.html. For instance, suppose you have some text you want to add to all your DVD products. Create a file called DVD.html in this directory, i.e.
includes/languages/<your-language>/boilerplate/DVD.html
Put whatever HTML marked-up text you want in that file.

Now go back to
includes/templates/custom/templates/tpl_product_info_display.php
Just before
 <!--bof Product description -->

insert this code:

<!-- bof html description -->
<?php
function html_include_cb($match) {
      $prefix = "HTML_INCLUDE_";
      $match_filename = str_replace($prefix, '', $match[0]); 
      $filename = DIR_WS_LANGUAGES . $_SESSION['language'] .  
                 '/boilerplate/' . 
                 $match_filename  . ".html";
      $buffer = ""; 
      if (file_exists($filename)) { 
          $handle = @fopen($filename, "r"); 
          while (!feof($handle)) {
              $buffer .= fgets($handle, 4096); 
          }
          fclose($handle);
      }
      return $buffer;
}
if ($products_description != '') { 
   $pattern = "/HTML_INCLUDE_[a-zA-Z0-9]*/"; 
   $products_description = preg_replace_callback($pattern, "html_include_cb", 
           $products_description, -1);
}
?>
<!-- eof html description -->

Now to use this logic, all you need to do is enter
HTML_INCLUDE_DVD
in the product description (under Admin - Catalog - Categories and Products), and when you display the product info page, the file
includes/languages/<your-language>/boilerplate/DVD.html
will be included.
For any
HTML_INCLUDE_SomeAlphanumericString
you add to a product description, simply create a file called
includes/languages/<your-language>/boilerplate/SomeAlphanumericString.html
and it will automatically be displayed.
Note that the string really does have to be alphanumeric; don't use dashes, underscores, spaces or special characters in the name.

One more example: If you add to your product description
<h2>Measurement</h2>
HTML_INCLUDE_Measurement 

Then when the product info page was displayed on the catalog side, you'd see

Backset Measurement

The contents of the file includes/languages/<your-language>/boilerplate/Measurement.html

If you want to do replacements from the database in your file, it's only a little more work. Let's suppose you want to insert the actual product's name into your boilerplate. Change
if ($products_description != '') { 
   $pattern = "/HTML_INCLUDE_[a-zA-Z0-9]*/"; 
   $products_description = preg_replace_callback($pattern, "html_include_cb", 
           $products_description, -1);
}
?>
<!-- eof html description -->
to
if ($products_description != '') { 
   $pattern = "/HTML_INCLUDE_[a-zA-Z0-9]*/"; 
   $products_description = preg_replace_callback($pattern, "html_include_cb", 
           $products_description, -1);
   $products_description = str_replace('%PRODUCTS_NAME%', $products_name, $products_description); 
}
?>
<!-- eof html description -->

Now, in your boilerplate file, use %PRODUCTS_NAME% wherever you want to have the product name inserted. You can do other fields similarly.

This tip was developed in June, 2008, and was first submitted to the Zen Cart Support Forum here.

NB: Since this modification only changes the description text on the product info page, you must either not display description text on listing pages or use this logic on any listing page where you wish to display description text. For example, to turn off description display in New Products listing, go to Admin > Configuration > New Listing and modify "Display Product Description", setting the value to 0 (off). To modify New Products to use this boilerplate change, customize the file includes/templates/template_default/templates/tpl_modules_products_new_listing.php, adapting the technique described here. Similarly,
  • for the All Products page, apply this change to includes/templates/template_default/templates/tpl_modules_products_all_listing.php
  • for the Featured Products page, apply this change to includes/templates/template_default/templates/tpl_modules_products_featured_listing.php
  • for the regular index listing pages, apply this change to includes/modules/product_listing.php
See Adding Boilerplate Text to the Product Description for a more detailed discussion.

Although this tip describes the additive use of HTML Boilerplate, it can also be used to completely replace the product description which is entered in the Zen Cart Admin Panel.