Homar Posted June 3, 2007 Share Posted June 3, 2007 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! Quote Link to comment Share on other sites More sharing options...
Homar Posted June 3, 2007 Author Share Posted June 3, 2007 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. :) Quote Link to comment Share on other sites More sharing options...
Homar Posted June 3, 2007 Author Share Posted June 3, 2007 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'])."…"); Replace this with: $view_cat->assign("TXT_DESC",strtrim(strip_tags($productResults[$i]['description']),300)."…"); Quote Link to comment Share on other sites More sharing options...
markscarts Posted June 5, 2007 Share Posted June 5, 2007 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 Edit: here is an example of the importance: http://www.cubecart.com/site/forums/index....showtopic=26873 :cry: Quote Link to comment Share on other sites More sharing options...
Robsta Posted June 5, 2007 Share Posted June 5, 2007 I would also like to mention that the length of the description is controlled by a setting in 'Admin > General Settings' under 'Styles & Misc': Length of product precis:(Chars) Quote Link to comment Share on other sites More sharing options...
Homar Posted June 5, 2007 Author Share Posted June 5, 2007 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 Edit: here is an example of the importance: http://www.cubecart.com/site/forums/index....showtopic=26873 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! Quote Link to comment Share on other sites More sharing options...
Guest dannedrama Posted August 20, 2009 Share Posted August 20, 2009 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 Quote Link to comment Share on other sites More sharing options...
Guest dannedrama Posted August 24, 2009 Share Posted August 24, 2009 Could someone please help me with this! All I want to do is to shorten the title on the latest products page! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.