Add custom content to your WooCommerce product description

I am trying to insert some text at the end of the description. Is it possible with a filter?

Or do I need to do this with a child theme? Tried looking for a hook for the description but can only find one for a short description. Example:

This is the description.

Just sample text to fill in the description.

What I want is to enter "This is the last line in the description". Thus, the hole description will look like this.

This is the description.

Just sample text to fill in the description.

This is the last line in the description

Code to enter text before short description:

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;
}

      

+4


source to share


1 answer


You can use this custom function connected to the filter the_content

like this:

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;
}

      

The code is placed in the functions.php file of your active child theme (or active theme). Tested and working.




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

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;
}

      

The code is placed in the function.php file of your active child theme (or active theme). Tested and working.

+5


source







All Articles