1

I used below code to limit the short description in product page:

1) it truncates all other attributes with text field in product page not only if ($_product->getData('short_description')). How make the if to check if this attribute is short_description then do the task?

2) what should be in <a href="#">Read More</a> that after clicking on read more it expands the text?

<div class="value" <?php /* @escapeNotVerified */ echo $_attributeAddAttribute;?>>
        <?php  if ($_product->getData('short_description')) {
            $string = strip_tags($_product->getData('short_description'));

            if (strlen($string) > 50) {
                // truncate string
                $stringCut = substr($string, 0, 50);
                $string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="#">Read More</a>';
            }
            echo $string;
        }else {?>
        <?php /* @escapeNotVerified */ echo $_attributeValue; }?>

    </div>
  • If u want on same Page, then you have other div with Full Description. Once it clicked "Read More" you have to show that Div & hide Your Current One – Ankit Shah Oct 27 '16 at 2:21
  • Is there Other div for short description! Where? Yes I want short description with read more link in product page – Zinat Oct 27 '16 at 4:23
  • You have to do it via Custom Code – Ankit Shah Oct 27 '16 at 4:25
  • simply you can achieve this using jquery. – Shyam Krishna Sreekumar Oct 27 '16 at 4:30
  • @shyam: php is the requirement :/ – Zinat Oct 27 '16 at 4:32
4

You have to just overrider attribute.phtml file from module-catalog core module.

Copy file inside your theme,

app/design/frontend/Vendor/luma/Magento_Catalog/templates/product/view/attribute.phtml

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * Product view template
 *
 * @see \Magento\Catalog\Block\Product\View\Description
 */
?>
<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_call = $block->getAtCall();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();

if ($_attributeLabel && $_attributeLabel == 'default') {
    $_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
if ($_attributeType && $_attributeType == 'text') {
    $_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code)) ? $_product->getAttributeText($_code) : '';
} else {
    $_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code);
}
?>



<?php if ($_attributeValue): ?>
    <?php if($_code === 'short_description') { ?>
        <div class="value more" <?php /* @escapeNotVerified */ echo $_attributeAddAttribute;?> >
        <?php  if ($_product->getData('short_description')) {
            $string = strip_tags($_product->getData('description'));

            if (strlen($string) > 50) {
                // truncate string
                $stringCut = substr($string, 0, 50);
                $string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="javascript:void(0);" class="readmore">Read More</a>';
            }
            echo $string;
            ?>
        <?php
        }else {?>
            <?php /* @escapeNotVerified */ echo $_attributeValue; 
            }
        ?>
        </div>

        <div class="less" style="display:none">
            <?php echo $_attributeValue; ?>
            <a href="javascript:void(0);" class="readless">Read Less</a>
        </div>
        <script type="text/javascript">
            console.log('test');
            require(["jquery"],function($){
                $('.readmore').on("click",function(){
                    $('.less').show();
                    $('.more').hide();
                });
                $('.readless').on("click",function(){
                    $('.less').hide();
                    $('.more').show();
                });
            });
        </script>
    <?php } else { ?>
        <div class="product attribute <?php /* @escapeNotVerified */ echo $_className?>">
            <?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php /* @escapeNotVerified */ echo $_attributeLabel?></strong><?php endif; ?>
            <div class="value" <?php /* @escapeNotVerified */ echo $_attributeAddAttribute;?>><?php /* @escapeNotVerified */ echo $_attributeValue; ?></div>
        </div>
    <?php } ?>
<?php endif; ?>
0

I made it like below : php + jQuery ;)

<?php  if ($_code =="short_description") {
            $decsriptionstr = $_product->getData('short_description');
            $string = strip_tags($decsriptionstr );

            if (strlen($string) > 50) {

                // truncate string
                $stringCut = substr($string, 0, 50);
                $string = substr($stringCut, 0, strrpos($stringCut, ' '));
            }?> <span class="teaser"> <?php   echo $string; ?> </span>
            <span  class="completeDescription"><?php echo $decsriptionstr; ?> </span>
            <span class="moreLink">more...</span>
            <?php

        }

And jQuery part:

<script type="text/javascript">
$(".moreLink").toggle(function () {
                        $(this).text("less..").siblings(".completeDescription").show();
                    }, function () {
                        $(this).text("more..").siblings(".completeDescription").hide();
                    });
</script>

And css:

.completeDescription{
  display:none;
}

.moreLink{
  cursor:pointer;
}
  • add jquery part in answer. – Gopal Patel Oct 27 '16 at 6:00
  • You can do also above way. – Rakesh Jesadiya Oct 27 '16 at 7:40
  • @zinat, you can add js to complete answer. – Rakesh Jesadiya Oct 27 '16 at 7:45
  • @Rakesh, It's there now ;) – Zinat Oct 27 '16 at 9:34
  • @GopalPatel, here you go ;) – Zinat Oct 27 '16 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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