5

I'm using the following code on my WordPress site to shorten my description excerpt on WooCommerce and it works fine if I input my characters for 14 or less. As soon as I enter more than 14 characters it shows the full short description.

add_action( 'woocommerce_after_shop_loop_item_title', 'lk_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk_woocommerce_product_excerpt'))
{
    function lk_woocommerce_product_excerpt()
    {
        $content_length = 14;
        global $post;
        $content = $post->post_excerpt;
        $wordarray = explode(' ', $content, $content_length + 1);
        if(count($wordarray) > $content_length) :
            array_pop($wordarray);
            array_push($wordarray, '...');
            $content = implode(' ', $wordarray);
            $content = force_balance_tags($content);
            $content = substr($content, 0, 14);

        endif;
        echo "<span class='excerpt'><p>$content...</p></span>";
    }
}

Any help would be appreciated.

Thank you.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
user3612498
  • 147
  • 1
  • 4
  • 14

2 Answers2

3

Your code is counting letters with white spaces, instead the code below is counting words without white spaces. Please See this live php file in action (here the result of your code on a string containing 25 words and mine too). Then this code is working correctly as you wish:

add_action( 'woocommerce_after_shop_loop_item_title', 'shorten_product_excerpt', 35 );
function shorten_product_excerpt()
{
    global $post;
    $limit = 14;
    $text = $post->post_excerpt;
    if (str_word_count($text, 0) > $limit) {
        $arr = str_word_count($text, 2);
        $pos = array_keys($arr);
        $text = substr($text, 0, $pos[$limit]) . '...';
        // $text = force_balance_tags($text); // may be you dont need this…
    }
    echo '<span class="excerpt"><p>' . $text . '</p></span>';
}

Or you can use the function from the thread below, with yours this way:

if (!function_exists('lk_limit_text'))
{
    function lk_limit_text($text, $limit) {
        if (str_word_count($text, 0) > $limit) {
            $words = str_word_count($text, 2);
            $pos = array_keys($words);
            $text = substr($text, 0, $pos[$limit]) . '...';
        }
        return $text;
    }
}

add_action( 'woocommerce_after_shop_loop_item_title', 'lk_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk_woocommerce_product_excerpt'))
{
    function lk_woocommerce_product_excerpt()
    {
        global $post;
        $content = $post->post_excerpt;
        // $content = force_balance_tags($content); // may be you dont need this…
        echo '<span class="excerpt"><p>' . lk_limit_text( $content, 14 ) . '</p></span>';
    }
}

This should work…

This code is based on this thread: How can I truncate a string to the first 20 words in PHP?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for the code LoicTheAztec! Unfortunately this doesn't work at all to shorten the description length. Do you have any other ideas why the original code wouldn't work past 14 characters? I tried with different WordPress themes, but that made no difference. – user3612498 Jul 02 '16 at 20:29
  • For some reason I thought your original code was counting characters, not words. So it does work! Thank you so much! – user3612498 Jul 03 '16 at 03:29
  • Just a quick follow up question. Is there a simple way to change the code so it uses the WooCommerce full description instead of the short one? – user3612498 Jul 03 '16 at 17:13
  • 2
    @user3612498 Yes of course, you can replace `$post->post_excerpt;` by `$post->post_content;`… – LoicTheAztec Jul 03 '16 at 17:23
  • Thanks @LoicTheAztec you're the best! – user3612498 Jul 03 '16 at 22:54
1

You can Limit WooCommerce product description

use this code -

add_filter('woocommerce_short_description', 'limit_product_short_description', 10, 1);

function limit_product_short_description($post_excerpt)
 {
    if (!is_product()) 
    {
         $pieces = explode(" ", $post_excerpt);
         $post_excerpt = implode(" ", array_splice($pieces, 0, 14));
     }
   return $post_excerpt;
 }

explode breaks the original string into an array of words, array_splice lets you get certain ranges of those words, and then implode combines the ranges back together into single strings.

Use this code to change Limit on the Shop Page not Product-detailed Page.

If you want to change on both pages remove if (!is_product()) condition.

Swapnali
  • 1,141
  • 9
  • 13