Question

How can I hide product description when the description is long in Opencart (product page) to reduce the load product page, but after clicking on the detail link then came out a full description. In image you can see Example, Sorry for my bad english, Thanks! Here is a link to example image example

Était-ce utile?

La solution

Why not just truncate it? It will force it to be the right length for you every time!

Go to catalog/controller/product/category.php and when you see

foreach ($results as $result) {
    if ($result['image']) {
        $image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
    } else {
        $image = false;
    }

Add this next:

function truncate($description, $tLimit="20", $break=" ", $pad="...")
{
  if(strlen($string) <= $tlimit) return $string;

  if(false !== ($breakpoint = strpos($string, $break, $tlimit))) {
    if($breakpoint < strlen($string) - 1) {
      $string = substr($string, 0, $breakpoint) . $pad;
    }
   }

   return $description;
 }

Feel free to change the variables:

$tLimit is how many letters you want to allow it.

$break is where you want it to cut off, right now it is set to cut off at the next space. You can have it interrupt words if you like by putting $break=""

$pad is what you want it to show after it cuts off the text.

If you really want no description to show at all Then I recommend still doing something similar to the original script.

function getDescriptionLength($description, $tLimit="20")
{
  if(strlen($string) <= $tlimit) return $string;

  else {
    $description = NULL;
  }

  return $description;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top