skip to Main Content

I have a problem with Woocommerce to find a good solution, I am looking to have my line breaks in this product extract space. I would also like to be able to use lists.
Despite my search in the plugin, I can’t find any strip_tags that could be the cause of removing them, and also overriding a filter, I have tested many codes by going through Google without success.

This solution not working for me.
woocommerce product excerpt description (short)

enter image description here
enter image description here
Thank you

2

Answers


  1. Chosen as BEST ANSWER

    I just went into the template editor and replaced the product extract with the description, I'll see if it's possible to go through an ACF block. I'm not yet very familiar with the template editor


  2. The short description is controlled by the short description input field for each product. So you can make your changes there for each product which require some manual work.

    However, you can automatically convert each sentence in the short description into a list item. One way you can achieve this by adding a function to split the description at each period (".") symbol, and wrap each resulting sentence in <li> tags.

    The below code will:

    1. Remove wpautop filter: removes the automatic paragraph tags added by WordPress.
    2. Split the short description into sentences at each period (.) symbol. It is important that you add the period symbol at the end of each sentence, as this is what determines where the list tag ends.
    3. Wrap each sentence in <li> tags.
    4. Combine all list items into a single unordered list (<ul>).

    /* Automatically convert each sentence in the short description into a list item */
    // Remove the wpautop filter for the short description
    remove_filter('woocommerce_short_description', 'wpautop');
    
    // Add the custom filter to apply the function to the short description
    add_filter('woocommerce_short_description', 'custom_list_item_converter_short_description', 10, 1);
    function custom_list_item_converter_short_description($the_excerpt) {
        // Split the excerpt into sentences at each period symbol and filter out empty elements
        $sentences = array_filter(array_map('trim', explode('.', $the_excerpt)));
    
        // Wrap each sentence in <li> tags and combine them into a single string with <ul> tags
        $formatted_excerpt = '<ul><li>' . implode('.</li><li>', $sentences) . '.</li></ul>';
    
        // Return the formatted excerpt
        return $formatted_excerpt;
    }
    

    I have tested this code and it works on my end. You should add it in the functions.php file of your active child theme.

    enter image description here

    Removing this code will revert back to normal settings.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search