11

I'm trying to inject some text in my description ending. Is it possible with filter?

Or do i need to do this via child theme? Been trying to find the hook for description but can only find one for short description. Example:

This is a description.

Just some sample text to fill the description out.

What i want is to inject "This is the last line in the description" So the hole description would look like this.

This is a description.

Just some sample text to fill the description out.

This is the last line in the description

The code i have for injecting text before short description is this:

add_filter( 'woocommerce_short_description', 'single_product_short_descriptions', 10, 1 );
function single_product_short_descriptions( $post_excerpt ){
    global $product;

    if ( is_single( $product->id ) )
        $post_excerpt = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>' . $post_excerpt;

    return $post_excerpt;
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Oscar vs
  • 376
  • 2
  • 4
  • 17

1 Answers1

15

You can use this custom function hooked in the_content filter hook this way:

add_filter( 'the_content', 'customizing_woocommerce_description' );
function customizing_woocommerce_description( $content ) {

    // Only for single product pages (woocommerce)
    if ( is_product() ) {

        // The custom content
        $custom_content = '<p class="custom-content">' . __("This is the last line in the description", "woocommerce").'</p>';

        // Inserting the custom content at the end
        $content .= $custom_content;
    }
    return $content;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


Addition - Force product description when is empty (if you want this custom text to be displayed):

add_filter( 'woocommerce_product_tabs', 'force_description_product_tabs' );
function force_description_product_tabs( $tabs ) {

    $tabs['description'] = array(
        'title'    => __( 'Description', 'woocommerce' ),
        'priority' => 10,
        'callback' => 'woocommerce_product_description_tab',
    );

    return $tabs;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Question __("This is the last line in the description", "woocommerce") What does this do? I noticed that it worked without adding this as well. Is it some kind of safety to use ,"woocommerce" or why is it used when injecting text. – Oscar vs Aug 09 '17 at 13:24
  • @Oscarvs This is the text that you are injecting at the end of your product description – LoicTheAztec Aug 09 '17 at 13:26
  • I know, its working fine thank you so much. But i can't understand whats the difference is between. $custom_content ='

    ' . __("This is the last line in the description", "woocommerce").'

    '; And $custom_content ='

    This is the last line in the description

    '; Both works and i would love to read more about what the difference is to learn.
    – Oscar vs Aug 09 '17 at 13:30
  • @Oscarvs the first one is to get it ready for translation (in multi language sites or foreign language sites) … Look for Wordpress gettex – LoicTheAztec Aug 09 '17 at 13:51
  • Ahh perfect thank you so much for this very good information! – Oscar vs Aug 09 '17 at 14:13