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

Adding Conditional Messages on the Zen Cart Product Info page

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

Conditional Messages are messages which are switched on and off based on certain conditions, such as whether the product has a certain id or is in a certain category. For instance, if you are using the Quantity Discounts Contribution for certain products only, you would want this sort of logic.

Create your custom template if you haven't already done so.

Customize the product_info page. Assuming your template is called "custom," copy
includes/templates/template_default/templates/tpl_product_info_display.php
to
includes/templates/custom/templates
Now let's suppose you're going to use the text from the Quantity Discount FAQ, which is
<?php
  $value = "ot_quantity_discount.php"; 
  include(zen_get_file_directory(DIR_WS_LANGUAGES . $_SESSION['language'] . 
          '/modules/order_total/', $value, 'false'));
  include(DIR_WS_MODULES . "order_total/" . $value);
  $discount = new ot_quantity_discount();  
  echo '<div class="content" id="discountPolicy">'; 
  echo '<h2>' . STORE_POLICY . '</h2>'; 
  echo $discount->get_html_policy();
  echo '<br /></div>'; 
?>
For brevity, in remaining examples, the code between <?php and ?> will be referred to as simply "***CODE***".

Suppose we want to add this code right after the product description. Look for
<!--eof Product description -->
<br class="clearBoth" />
and add it right there. Now how do we make it conditional?

If this text only applies to product 25, use this logic:
<?php
if ((int)$_GET['products_id'] == 25) 
{
   ***CODE***
}
?>

If the text only applies to product 25 or product 26, use
<?php
if ( ((int)$_GET['products_id'] == 25) ||
     ((int)$_GET['products_id'] == 26) )
{
   ***CODE***
}
?>

If the text applies to everything EXCEPT product 25 or 26, use
<?php
if ( ((int)$_GET['products_id'] != 25) &&
     ((int)$_GET['products_id'] != 26) )
{
   ***CODE***
}
?>

If you want to specify a category instead of a product id, use the variable $current_category_id. For instance, if the text applies to everything EXCEPT category 1 or category 2, use
<?php
if ( ($current_category_id != 1) &&
     ($current_category_id != 2) )
{
   ***CODE***
}
?>

Note that the variable $current_category_id is not reliable in a situation where you have linked products; there, you must query the database to determine the master category id.
     $product_to_categories = $db->Execute("select master_categories_id from " . 
       TABLE_PRODUCTS . " where products_id = '" . (int)$_GET['products_id'] . "'");
     $true_category_id = $product_to_categories->fields['master_categories_id'];


If you want to do something special for call for price products, use
<?php
if (zen_get_products_price_is_call((int)$_GET['products_id']))
{
   ***CODE***
}
?>


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