Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

[TIP] $variables in the product descript


Guest

Recommended Posts

To use osc variables or php in the product description

(tested on 2.2)

Broken into two parts

-----------------------------------------------

OUTLINE:

 

1) You will need to edit file: catalog/product_info.php to add the functionality to the page....

 

2) When you add a product description to the catalog and wish to use php....

 

 

POSSIBLE USES:

I use the product price in the description area for some of my products. Now when the user changes currencies the description values are also updated to match the currency they selected, thus removing any possible confusion about the products pricing.

-----------------------------------------------

 

PART 1)

 

At approx. line 120 of a standard product_info.php file you will find:

<p><?php echo stripslashes($product_info['products_description']); ?></p>

 

Change to:

<p><?php 
$product_description = '';
eval('$product_description= \'' .$product_info['products_description']. '\';'); 
echo stripslashes($product_description); 
?></p>

 

-----------------------------------------------

PART 2)

 

When you add a product description to the catalog and wish to use php:

In short use:

'. xxxxx .'

where xxxxx is the php you wish to incorporate.

NOTE: Dont include <?php ?> tags.

The eval() statement that was added to the product_info.php file treats the "product description" you enter via the admin interface as a php string.

You dont have to use any opening and closing ' as they are included in the product_info.php page adjustment described in part 1. (see the example below)

 

Existing entries in your catalog will not be affected.

 

Example:

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="main"><tr><td><b><font color="#FF0000">[B]' .$products_price. '[/B]</font> -1 years </b></td></tr></table>

 

Thats it. Hope people find this usefull.

Link to comment
Share on other sites

I just noticed a small bug in my previous code.

If your description contained any words such as dog's or cat's the char quote aould cause a parse error.

 

The version below fixes this by replacing any existing ' with \' which are later removed by the stripslashes() function.

 

Anyway please use the folowing code for PART 1 of my above post instead:

<p><?php 
$product_description = $product_info['products_description'];
$product_description = str_replace('\'', '\\\'', $product_description);
eval('$product_description= \'' .$product_description. '\';'); 
echo stripslashes($product_description); 
?></p>

Link to comment
Share on other sites

Sorry. Its 7am where I am and I havent had any sleep yet.

Use the following replacement instead.

 

The above correction was not tested properly

 

<p><?php 
// NOTE: keep formatting in str_replace() and in the product description the same
// EG: use ' .phpcode. ' OR '.phpcode.' Not a mixture of the two.
// The masks below contain a space char like my first eg: (line above).

$product_description = $product_info['products_description'];
// replaces all char quotes in the description text
$product_description = str_replace('\'', '\\\'', $product_description);
// repares left side php indicator
$product_description = str_replace('\\\' .', '\' .', $product_description);
// replaces right side php indicator
$product_description = str_replace('. \\\'', '. \'', $product_description);
eval('$product_description= \'' .$product_description. '\';'); 
echo stripslashes($product_description); 
?></p>

 

Any problems, please email me.

Link to comment
Share on other sites

Thanks for the tip. I've been thinking about doing something like this (I'm glad you took the first shot :) ).

 

I've followed your steps, however functions are not working. For example, my code looks like this...

 

product_info.php

          <p><?php
         // NOTE: keep formatting in str_replace() and in the product description the same
         // EG: use ' .phpcode. ' OR '.phpcode.' Not a mixture of the two.
         // The masks below contain a space char like my first eg: (line above).

         $product_description = $product_info['products_description'];
         // replaces all char quotes in the description text
         $product_description = str_replace('\'', '\\\'', $product_description);
         // repares left side php indicator
         $product_description = str_replace('\\\' .', '\' .', $product_description);
         // replaces right side php indicator
         $product_description = str_replace('. \\\'', '. \'', $product_description);
         eval('$product_description= \'' .$product_description. '\';');
         echo stripslashes($product_description); ?></p>

 

My product description is

This is the test text. Here's a single quote. Here are "double quotes".
<p>Here is the <a href=" . tep_href_link(FILENAME_PRIVACY, '', 'NONSSL') . ">link</a>.</p>

 

But my link isn't working. It's not executing the tep_href_link function. The resulting link is http://mySite.com/ . tep_href_link(FILENAME_PRIVACY, '', 'NONSSL') .

 

Any thoughts on how to get functions working within the product description?

 

Thanks.

-Fred

Link to comment
Share on other sites

Theres a few things I should add.

 

Firstly, as with any code alterations/development please be carefull and back up the files you change incase something goes wrong. It saves a lot of grief in the long run.

 

Secondly the php code will not run in the admin area unless you modify the relevant admin file using the same code you use for the product_info.php mod. I will post more on the admin changes after I get round to looking into it myself. Im a bit busy lately.

 

Anyway back to the solution etc...

 

I have come up with 2 possible ways to solve the above problem. I have tested them both on my live shop and they work fine.

 

1) Comment out the string modifiers in the code I posted like the following example. NOTE: if you do this you must ensure that all single quote's in your product description content are preceeded by the non printing character like so: quote\'s

 

The best way to do this is by using the search and replace tool in your editor to make sure (most text editors have this ability).

<p><?php 
$product_description = $product_info['products_description'];

//$product_description = str_replace('\'', '\\\'', $product_description);
//$product_description = str_replace('\\\' .', '\' .', $product_description);
//$product_description = str_replace('. \\\'', '. \'', $product_description);

eval('$product_description= \'' .$product_description. '\';'); 
echo stripslashes($product_description); 
?></p>

 

 

 

 

2) The other alternative, is to hardcode php into your product info page and only pass a variable in the products description. I use this technique to allow me to use the osc curreny functions on currency values in the product desc..

 

Firstly I added the following code to "catalog/includes/classes/currencies.php" to allow me to do currency conversions without tax.

 

// Simple curreny conversion. (tax not included)
function convert_currency($products_price, $quantity = 1) {
return $this->format($products_price * $quantity);
}

 

Then I added something like the following to my product_info.php file:

<?php 
$someItem_1=($currencies->convert_currency(50)); 
$someItem_2=($currencies->convert_currency(2500)); 

$someOtherItem_1=($currencies->convert_currency(10)); 
$someOtherItem_2=($currencies->convert_currency(8.75));
?>

<p><?php 
// Nothing different from my example above
$product_description = $product_info['products_description'];
$product_description = str_replace('\'', '\\\'', $product_description);
$product_description = str_replace('\\\' .', '\' .', $product_description);
$product_description = str_replace('. \\\'', '. \'', $product_description);
eval('$product_description= \'' .$product_description. '\';'); 
echo stripslashes($product_description); 
?></p>

 

Another EG this bit would be harcoded somewhere in the product_info.php file: <?php $foo = '<p>Here is the <a href="' . tep_href_link(FILENAME_PRIVACY, '', 'NONSSL') . '">link</a>.</p>' ?>

 

You would then use the variable $foo like so in the description text:

 

blah blah ' .$foo. ' blah blah blah

 

 

 

 

For Fred (the above post). If I were you, for what you want, I would use explantion 1 above.

 

Also you had:

<a href=" . tep_href_link(FILENAME_PRIVACY, '', 'NONSSL') . ">link</a>.</p>

 

To suit the code I have posted, should be:

<a href="' . tep_href_link(FILENAME_PRIVACY, '', 'NONSSL') . '">link</a>.</p>

 

or

<a href=" ' . tep_href_link(FILENAME_PRIVACY, '', 'NONSSL') . ' ">link</a>.</p>

 

or

<a href=" ' .tep_href_link(FILENAME_PRIVACY, '', 'NONSSL'). ' ">link</a>.</p>

 

 

 

Lastly: The PHP documentation is a great source of help although, like most technical docs can be a bit heavy if you are new to programming.

 

To find out more about the PHP eval() function try here: http://au3.php.net/eval

 

hth

Link to comment
Share on other sites

  • 2 weeks later...
  • 4 weeks later...

Ok, so I've been using this for about a month now.

 

If you're interested in trying this, there's one thing to watch out for. If your browser is set to not to use cookies, when you edit existing product descriptions that include functions such as tep_href_link, your osCAdminsID (session ID) will be appended to the link. This will cause major problems unless you manually strip it out.

 

It will give you something like...

<a href="' . tep_href_link('product_info.php/cPath/19/products_id/59', '', 'NONSSL') . '?osCAdminsID=62c4c54eab63eda0d8bf0a8a9ebc59cb"><img src="images/button_tshirt.gif" border="0" align="right"></a>

 

Stripping out the SID looks like this...

<a href="' . tep_href_link('product_info.php/cPath/19/products_id/59', '', 'NONSSL') . '"><img src="images/button_tshirt.gif" border="0" align="right"></a>

 

Other than that, it's working great.

 

-fred

-Fred

Link to comment
Share on other sites

  • 7 months later...

hello there!

I am trying to use this code to add php in the product description area with partly success.

it is seems to be a problem to use the include statement .

the code I am trying to use is something like:

 

 <table width="100%" border="0" cellspacing="0" cellpadding="0" class="main"><tr><td><b><font color="#FF0000">'.$products_price.'</font> -1 years </b></td></tr></table><br>

'.include  ('http://www.my-site.com/some-file/page.htm').'

 

the problems I am seeing are:

 

1. the include part jumps to the top of the page before the table part of the code

2. when the include part of the code is added the number "1" is printed on the page

it looks like the ";" (semicolon or break) is missing with the code past to the browser , therefore anything added after the

include ('http://www.my-site.com/some-file/page.htm') part is added to the included address part.

 

I have went through the documentation at: http://us4.php.net/manual/en/function.include.php and tried any thinkable combination to avoid the printing of the "1" (indicating the include was successful ) with no successes.

 

also if there is a tip of how to use the php code in the category description part.

any input will be greatly appreciated.

yos

Link to comment
Share on other sites

  • 5 months later...

I am having a similiar problem using php in the description...

I need to also set variables in the dseription as well.

 

I have a table with info in it all based off a few variables....

 

the only way I have actually been able to use php in the description is like so:

 

' .$var = 10;

$var. '

 

this sets the variable and prints it... but then it does not print the table...

 

if I use:

 

' .$var = 10;

$var;. '

 

Then I get a parse error...

 

How would I go about using php correctly?

Link to comment
Share on other sites

  • 1 year later...

Thanks so much for the help.

One question. It says not to use the php tags, but just ' .xxxx. '

I am trying to insert this:

<td class="main" align="center">Square Feet Needed:<input type="text" name="quantity" value="1" maxlength="2" size="2"><br><?php echo tep_draw_hidden_field('products_id', $product_info['products_id']) . tep_image_submit('button_in_cart.gif', IMAGE_BUTTON_IN_CART); ?></td>

Which part of the php would be taken out then?

Thanks for all the help.

Link to comment
Share on other sites

  • 9 months later...

just wanted to bump this tip up!

 

Thanks so much for the help.

One question. It says not to use the php tags, but just ' .xxxx. '

I am trying to insert this:

<td class="main" align="center">Square Feet Needed:<input type="text" name="quantity" value="1" maxlength="2" size="2"><br><?php echo tep_draw_hidden_field('products_id', $product_info['products_id']) . tep_image_submit('button_in_cart.gif', IMAGE_BUTTON_IN_CART); ?></td>

Which part of the php would be taken out then?

Thanks for all the help.

 

I just got in here to do a few things so I can't be sure... but if you still need to know i beleive it should be

<td class="main" align="center">Square Feet Needed:<input type="text" name="quantity" value="1" maxlength="2" size="2"><br>' . tep_draw_hidden_field('products_id', $product_info['products_id']) . tep_image_submit('button_in_cart.gif', IMAGE_BUTTON_IN_CART) . '</td>

Kristine

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...