Magento

eCommerce Software for Online Growth

Magento Forum

From setting up your store to managing your products, pages and promotions to generating detailed reports, the Magento User Guide empowers the user to utilize the platform for all of its vast capacity.
Available in eBook and Print formats – Download Now!!!
   
Page 1 of 3
Is There A Way To Truncate Long Text? 
 
Baseballtips
Sr. Member
 
Total Posts:  83
Joined:  2008-08-01
 

I want to truncate my short descriptions after so many characters so that the wordy ones don’t destroy my design.

Basically I want to make this:
“Multi-position hitting tee with home plate base and durable construction. “

look like this:
“Multi-position hitting tee with home plate base...”

So that the items in my product list don’t overlap each other in the case of an unexpectedly long short description.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  1361
Joined:  2009-02-26
Minneapolis MN
 

Basically I want to make this:
“Multi-position hitting tee with home plate base and durable construction. “

look like this:
“Multi-position hitting tee with home plate base...”

You can use

$str “Multi-position hitting tee with home plate base and durable construction
echo substr($str020).'...'

That will give you 20 characters starting at the beginning, then adding on a ...

 Signature 

Brent W. Peterson S.C.J.E.
What cool thing did I do lately?:
Quick Order
What am I working on now?: Auto add to cart now with discount
Bored? Read my non-techie blog

 
Magento Community Magento Community
Magento Community
Magento Community
 
Baseballtips
Sr. Member
 
Total Posts:  83
Joined:  2008-08-01
 

Awesome! Thanks dude.

 
Magento Community Magento Community
Magento Community
Magento Community
 
CT Schubert
Sr. Member
 
Avatar
Total Posts:  157
Joined:  2008-10-08
Southern California
 

Be careful with that, as it doesn’t detect when a word ends and when one begins so you could end up with

“Multi-position hitting tee with home pl...”

You may be better off using a function such as this…

function myTruncate($string$limit$break="."$pad="..."
  
// return with no change if string is shorter than $limit  
  
if(strlen($string) <= $limit
    return 
$string;
  
// is $break present between $limit and the end of the string? 
  
if(false !== ($breakpoint strpos($string$break$limit))) {
    
if($breakpoint strlen($string) - 1
      $string 
substr($string0$breakpoint) . $pad
    
}
  }
  
return $string;
}

 Signature 

CT Schubert
http://www.CTSchubert.com
Website Development - Hosting - IT Consulting/Support

 
Magento Community Magento Community
Magento Community
Magento Community
 
robbo23
Sr. Member
 
Total Posts:  81
Joined:  2008-10-20
 

I’m looking to do something similar and was wondering if you had managed to get this working?

What I’d like to do is add the main product description to the category pages but truncate it like you were saying.

Can someone explain the steps I would need to take to achieve this please?

Thanks.

 Signature 

My Magento sites: Pretty & Witty | Whisky Online | ProSwimwear

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  1361
Joined:  2009-02-26
Minneapolis MN
 

You are running a Whiskey site and you have to ask if anyone will help!

function myTruncate($string$limit$break="."$pad="..."
  
// return with no change if string is shorter than $limit  
  
if(strlen($string) <= $limit
    return 
$string;
  
// is $break present between $limit and the end of the string? 
  
if(false !== ($breakpoint strpos($string$break$limit))) {
    
if($breakpoint strlen($string) - 1
      $string 
substr($string0$breakpoint) . $pad
    
}
  }
  
return $string;
}

For example on the view.phtml page you can do this

<?php echo myTruncate($_description20?>

 Signature 

Brent W. Peterson S.C.J.E.
What cool thing did I do lately?:
Quick Order
What am I working on now?: Auto add to cart now with discount
Bored? Read my non-techie blog

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  1361
Joined:  2009-02-26
Minneapolis MN
 

http://new.midwestsupplies.com/

Also, you should think about breaking down your scotch by region. i.e. Islay, Highland etc…

We are developing a searchable attribute module if you are interested.

 Signature 

Brent W. Peterson S.C.J.E.
What cool thing did I do lately?:
Quick Order
What am I working on now?: Auto add to cart now with discount
Bored? Read my non-techie blog

 
Magento Community Magento Community
Magento Community
Magento Community
 
robbo23
Sr. Member
 
Total Posts:  81
Joined:  2008-10-20
 

Hi Brent,

Thanks for your replies. The site belongs to my client, not me personally.

It’s difficult to please everyone when deciding how to display sections like scotch. For example there are 144 different distilleries listed in the single malt a to z - that’s for people that know what they want. Trying to list over 800 different single malts for people who don’t know one from another is quite difficult, but I take on board your suggestion. We may well look at that in due course.

Thanks for the truncate tip - what we want to do is have the full product description on the category pages (ie the list/grid pages) e.g Johnnie Walker but I have tried adding

<?php echo $_description ?>

to the list.phtml page and it just returns an error. Do you have any thoughts on how we can do this?

The reason is that we are using an invoice module that adds the short description to the invoice and we want to keep the content of the short description to a minimum.

Thanks again

Rob

 Signature 

My Magento sites: Pretty & Witty | Whisky Online | ProSwimwear

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  1361
Joined:  2009-02-26
Minneapolis MN
 

I think on the list.phtml you can use this

<?php echo nl2br($_product->getShortDescription()) ?>

Then add the truncate function around it.

We are always up for barter if you want to do anything in the future!

 Signature 

Brent W. Peterson S.C.J.E.
What cool thing did I do lately?:
Quick Order
What am I working on now?: Auto add to cart now with discount
Bored? Read my non-techie blog

 
Magento Community Magento Community
Magento Community
Magento Community
 
robbo23
Sr. Member
 
Total Posts:  81
Joined:  2008-10-20
 

Is there any chance you could paste the code with the truncate function already wrapped around it please Brent? I can’t seem to get it to work.

Thanks.

 Signature 

My Magento sites: Pretty & Witty | Whisky Online | ProSwimwear

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  1361
Joined:  2009-02-26
Minneapolis MN
 

Sure, I renamed the function (and I put it in my module) btw.. thanks CT

<?php echo setShortDescription(nl2br($_product->getShortDescription()), 20?>

works great!

Thanks CT

 Signature 

Brent W. Peterson S.C.J.E.
What cool thing did I do lately?:
Quick Order
What am I working on now?: Auto add to cart now with discount
Bored? Read my non-techie blog

 
Magento Community Magento Community
Magento Community
Magento Community
 
robbo23
Sr. Member
 
Total Posts:  81
Joined:  2008-10-20
 

I don’t know why but I can’t get it to work. I’m getting the following error:

Fatal error: Call to undefined function setshortdescription()

I have this in list.phtml

<?php echo setShortDescription(nl2br($_product->getShortDescription()), 20?>

and this in my js file

function setShortDescription($string$limit$break="."$pad="..."
  
// return with no change if string is shorter than $limit  
  
if(strlen($string) <= $limit
    return 
$string;
  
// is $break present between $limit and the end of the string? 
  
if(false !== ($breakpoint strpos($string$break$limit))) {
    
if($breakpoint strlen($string) - 1
      $string 
substr($string0$breakpoint) . $pad
    
}
  }
  
return $string;
}

Where am I going wrong with this?

 Signature 

My Magento sites: Pretty & Witty | Whisky Online | ProSwimwear

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  1361
Joined:  2009-02-26
Minneapolis MN
 

It is a php function, so you would have to put it on your page someplace, it should work then

 Signature 

Brent W. Peterson S.C.J.E.
What cool thing did I do lately?:
Quick Order
What am I working on now?: Auto add to cart now with discount
Bored? Read my non-techie blog

 
Magento Community Magento Community
Magento Community
Magento Community
 
robbo23
Sr. Member
 
Total Posts:  81
Joined:  2008-10-20
 

Ok so obviously I don’t know what I’m doing with JS or PHP LOL!

I’ve tried adding it but get errors. Does it need some PHP code wrapping around it or does it just go on the page as it is?

 Signature 

My Magento sites: Pretty & Witty | Whisky Online | ProSwimwear

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  1361
Joined:  2009-02-26
Minneapolis MN
 

<?php
function setShortDescription($string$limit$break="."$pad="..."
  
// return with no change if string is shorter than $limit  
  
if(strlen($string) <= $limit
    return 
$string;
  
// is $break present between $limit and the end of the string? 
  
if(false !== ($breakpoint strpos($string$break$limit))) {
    
if($breakpoint strlen($string) - 1
      $string 
substr($string0$breakpoint) . $pad
    
}
  }
  
return $string;
}
?>

That should do it,

(Shouldn’t you be at the pub?)

 Signature 

Brent W. Peterson S.C.J.E.
What cool thing did I do lately?:
Quick Order
What am I working on now?: Auto add to cart now with discount
Bored? Read my non-techie blog

 
Magento Community Magento Community
Magento Community
Magento Community
 
robbo23
Sr. Member
 
Total Posts:  81
Joined:  2008-10-20
 

LOL yeah I should be somewhere else I’m sure!

Still having probs. It won’t work on list.phtml. It displays the first product and then says

Fatal error: Cannot redeclare setshortdescription()

 Signature 

My Magento sites: Pretty & Witty | Whisky Online | ProSwimwear

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 3
 
© Copyright 2010 Magento Inc.
Privacy Policy|Terms of Service
Magento Community Count
279101 users|1304 users currently online|348102 forum posts