Jump to content

Short Product Descriptions


Homar

Recommended Posts

Does anyone know how to set a word limit on the product description so as perhaps 20 words can be added to each product in the 'Latest Products' box - followed by a '...' to show that there is more to read.

I probably didn't make what I'm trying to achieve clear, so here's an example:

Cheers!

Link to comment
Share on other sites

Never mind, I just found a great little tutorial on CC3.biz that does this perfectly - implemented in 2 minutes! Thanks for the great guide MarksCarts! I just wish that I'd found it sooner!

OK... MarksCarts guide shortens the description using the 'substr' PHP function. That results in words being cut off. For example:

Original Description: This is a description of a product.

Truncated Description: This is a descr...

If you want to truncate (shorten) the description without cutting into words, you should follow MarksCarts guide on CC3.biz (titled 'Add Description to Latest Product Images'). However, instead of doing step 4 & 5, do the following:

Add the following code exactly to the top of the index.inc.php file:

//Function to trim string using words



function strtrim($str, $maxlen=500, $elli=NULL, $maxoverflow=15) {

	global $CONF;

		

	if (strlen($str) > $maxlen) {

			

		if ($CONF["BODY_TRIM_METHOD_STRLEN"]) {

			return substr($str, 0, $maxlen);

		}

			

		$output = NULL;

		$body = explode(" ", $str);

		$body_count = count($body);

		

		$i=0;

	

		do {

			$output .= $body[$i]." ";

			$thisLen = strlen($output);

			$cycle = ($thisLen < $maxlen && $i < $body_count-1 && ($thisLen+strlen($body[$i+1])) < $maxlen+$maxoverflow?true:false);

			$i++;

		} while ($cycle);

		return $output.$elli;

	}

	else return $str;

}




Now find  the following line of code:




$index->assign("VAL_PRODUCT_NAME",validHTML($latestProducts[$i]['name']));




Below it, add  this line of code:




$index->assign("VAL_PROD_DESC",strtrim(strip_tags($latestProducts[$i]['description']),300)."…");

Now, you see in the above code the number '300'. This tells the script to shorten the description to 300 characters without cutting into words! Simply change the value '300' to what you want the maximum character length of your short description to be.

You can perform pretty much the same mod to the /includes/content/viewCat.inc.php and SKIN_NAME/styleTemplates/content/viewCat.tpl files in order to prevent incomplete words being displayed when browsing through the categories and a search result.

I hope this helps anyone who was having the same problems as me!

P.S. I'm not that great with PHP so there is probably a much easier and quicker way to perform this task. :)

Link to comment
Share on other sites

To add this mod to the viewCat files:

Add the function to the top of the viewCat.inc.php file, then find the following line:

$view_cat->assign("TXT_DESC",substr(strip_tags($productResults[$i]['description']),0,$config['productPrecis'])."&hellip;");




Replace this with:




$view_cat->assign("TXT_DESC",strtrim(strip_tags($productResults[$i]['description']),300)."&hellip;");

Link to comment
Share on other sites

Absolutely BEAUTIFUL, homar, in fact, this is a must-have and with your permission I will add it into the original tutorial!

It is a must-have because . . . I have seen CubeCart pages fail validation due to this truncating. When something like &amp; gets truncated to &am it throws validation errors.

Thank you for sharing this code function :blink:

Edit: here is an example of the importance: http://www.cubecart.com/site/forums/index....showtopic=26873 :cry:

Link to comment
Share on other sites

Absolutely BEAUTIFUL, homar, in fact, this is a must-have and with your permission I will add it into the original tutorial!

It is a must-have because . . . I have seen CubeCart pages fail validation due to this truncating. When something like & gets truncated to &am it throws validation errors.

Thank you for sharing this code function :blink:

Edit: here is an example of the importance: http://www.cubecart.com/site/forums/index....showtopic=26873 :cry:

Absolutely, please feel free to add it to your guide!

With regards to Robsta's comment, that would probably be a better way of doing things. You'll need to integrate the $config['productPrecis']) variable, replacing the '300' character limit that I stated above. That will allow users to change the variable from their admin panel.

Cheers All!

Link to comment
Share on other sites

  • 2 years later...
Guest dannedrama

This is brilliant! Thanks very much!

I have a similar question that I am reeally hoping someone can help me with.. how do you apply this on the TITLE for the latest products? Having long titles creates a problem for me, so I would like them shorter with a "..." in the end on the frontpage/latest products and then the original lenght on the product page. Is this possible? I guess you could use substr some way, i just don't know how since I am not that good with php.

Thanks in advance!

Dan

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...