0

Magento 2.4.2

enter image description here

I want to limit the length of the products name while displaying it in front end. I tried all the ways recommended earlier but no luck. I found the code as mentioned below

escapeHtmlAttr($_item->getName()) ?>" href="escapeUrl($block->getProductUrl($_item)) ?>"> escapeHtml($_item->getName()) ?>

Can anyone please help to sort this..

1

2 Answers 2

0

You can use like below sample code.

<?php
$productName = $_item->getName();
$len = strlen($productName);
?>
<a class="product-item-link" href="<?php echo /* @escapeNotVerified */ $_item->getProductUrl() ?>">
    <?php echo substr($productName,0,50); ?>
    <?php if($len > 50) echo '...'; ?>
</a>

substr function will truncate product name (string)

0

You also do it with pure css. no need to update phtml file.

text-overflow: ellipsis;

Ref: https://css-tricks.com/snippets/css/truncate-string-with-ellipsis/

Get you product name class and apply above css.

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.