2

On my webshop I have the option to quickshop from the category overview, which opens a lightbox if the product have different sizes. In this lightbox I have added the short description for the given product, but the text gets too long. Therefor i would like to limit the text, so it is cut off at some point with "..." following it. Hereafter I have a "Read More" link. The code I use to display the description with is;

echo $_product->getShortDescription();

Is there some way to limit it? It should only be limited in this lightbox and no where else on the homepage.

3 Answers 3

3

Okay, I found a solution for this. Magento has a built in function called substr that can be used. The function looks like this;

public function substr($string, $offset, $length = null)

So for example if i want to shorten the product name down to 26 character I can use;

echo substr($_helper->productAttribute($_product, $_product->getName(), 'name'),0, 26)

The following shorter snippet also works;

echo substr($_product->getName(),0, 26)

So if I want to shorten my product description down to 100 character I can use;

echo substr($_product->getShortDescription(),0,96);

I found the information on http://frontlinedev.com/how-to-limit-the-characters-displayed-magento/ where you can read more.

1
3

Someone posted this as an answer - https://stackoverflow.com/questions/16417380/truncating-description-text-in-magento

Magento can natively help you with this problem. Just tried it myself, it works great.

<p id="maintext"><?php echo Mage::helper('core/string')->truncate('text', 2); ?></p>

It even outputs ellipses at the end.

2

I have used CSS before to limit text in exactly the manner you describe; this would save you changing code however it would not be supported in earlier browsers. I'm guessing as you are using lightboxes this shouldn't be an issue for you.

There's a great article on how to do it here:

http://www.mobify.com/blog/multiline-ellipsis-in-pure-css/

1
  • Thank you Ben. Sadly I have to support older browsers then this fix can handle, so I will try and see if there is a php solution. Although I'll have this as my backup plan :)
    – McKeene
    Commented Sep 16, 2014 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.