1
votes

I have included product short description in my home and category pages by adding the following code to my child theme functions.php

add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_excerpt', 5);

Now I would like to limit the characters of product short description in the home and category pages.

Any help please??

More code and clarifications: My add_action is added to the following file woocommerce/includes/wc-template-hooks.php and here are the Product Loop Items.

add_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );

add_action( 'woocommerce_before_subcategory', 'woocommerce_template_loop_category_link_open', 10 );
add_action( 'woocommerce_shop_loop_subcategory_title', 'woocommerce_template_loop_category_title', 10 );
add_action( 'woocommerce_after_subcategory', 'woocommerce_template_loop_category_link_close', 10 );

add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );

everything is printed here: Mytheme/woocommerce/single-product.php and the code is

   <div class="container-inner">
        <?php do_action( 'woocommerce_before_shop_loop_item' ); ?>
        <div class="image-block">       
        <a href="<?php the_permalink(); ?>">        
            <?php
                /**
                 * woocommerce_before_shop_loop_item_title hook
                 *
                 * @hooked woocommerce_show_product_loop_sale_flash - 10
                 * @hooked woocommerce_template_loop_product_thumbnail - 10
                 */
                do_action( 'woocommerce_before_shop_loop_item_title' );
            ?></a>          
            <div class="product-block-hover"></div>

            </div>
<a href="<?php the_permalink(); ?>"><h3 class="product-name"><?php     the_title(); ?></h3></a>
            <?php
                /**
                 * woocommerce_after_shop_loop_item_title hook
                 *
                 * @hooked woocommerce_template_loop_rating - 5
                 * @hooked woocommerce_template_loop_price - 10 
                 */
                 do_action( 'woocommerce_after_shop_loop_item_title' );
            ?>                                      

        <?php do_action( 'woocommerce_after_shop_loop_item' ); ?>
        </div>              
4
Can you post more code so I can help you? Show us how do you print themSkatox
Hi @Skatox, I have added more code. Thanks for your help.salimottmani
Hi @Skatox I have added the code that you provided me with in the functions.php of my child theme right before the last php closing tag and it is not working. I also tried to replace woocommerce_short_description with woocommerce_template_single_excerpt() and it is not working too. Any suggestion please?salimottmani
I don't see do_action( 'woocommerce_shop_loop_item_title' ); in your template fileSkatox

4 Answers

1
votes

You can edit the lenght with the woocommerce_short_description filter, do something like this:

add_filter('woocommerce_short_description','limit_short_descr');

function limit_short_descr($description){
  return ($description > 140) ? substr($description, 0 , 140) : $description;
}

Also you can add a &hellip; after the text to make it look better.

0
votes

Thank you Skatox for your help, I finally sloved this issue. Here is what I did:

In the following file(\wp-contents\plugins\woocommerce\templates\single-product\short-description.php) I added the following code:

<?php
    if ( is_single()):
        echo apply_filters( 'woocommerce_short_description', $post->post_excerpt     );
    else :
        echo substr(apply_filters( 'woocommerce_short_description', $post-    >post_excerpt ),0,140);
    endif;
?> 

With this, I was able to display the characters I needed in my home and category pages and leave product page as it is.

P.S: the best way to get this done if you are facing the same issue, is to create the same file (short-description.php), edit the code and put it in woocommerce folder in your theme so that your changes are not affected by woocommerce update.

0
votes

Instead of editing files directly within the plugin (which is a very bad idea because once update the plugin and all of your changes will be lost!)

create a function and then hook to that filter... something like this...

add_filter('woocommerce_short_description', 'reigel_woocommerce_short_description', 10, 1);
function reigel_woocommerce_short_description($post_excerpt){
    if (!is_product()) {
        $pieces = explode(" ", $post_excerpt);
        $post_excerpt = implode(" ", array_splice($pieces, 0, 10));

    }
    return $post_excerpt;
}

paste this in your functions.php file of your theme.

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.

and use this line where you want to display product description -

<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?>

use this code to change Limit on the Home and Category Page not Product-detailed Page.

0
votes

I think this the better way. It will limit the short description to 85 characters, Makes your archive page look cleaner:

function customize_shop_loop_excerpt() {
function woocommerce_shop_loop_excerpt_custom() {
<p>
<?php echo preg_replace('/\s+?(\S+)?$/', '', substr(get_the_excerpt(), 0, 85)) . '...'; ?>
</p>
<?php
}
remove_action ('product_box_after', 'woocommerce_shop_loop_excerpt', 20);
add_action ('product_box_after', 'woocommerce_shop_loop_excerpt_custom', 20);
}
add_action ('after_setup_theme', 'customize_shop_loop_excerpt');