Albert Alarcón Albert Alarcón - 11 months ago 87
PHP Question

Auto text woocommerce_short_description hook

I am trying to create an automatic text in the description of the WooCommerce articles and put "article only available in the store."

I thought about putting it inside a function like this:

add_filter ('woocommerce_short_description', 'in_single_product', 10, 2);

function in_single_product () {
echo '<p> my-text-here </ p>';

But the text does not appear in the front-end. It only appears if I put the text on the back-end.

Does anyone know why this happens?


Use a conditional for single product pages and return the function variable instead of echoing it.

This will replace the post excerpt in single product pages:

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

    if ( is_single( $product->id ) )
        $post_excerpt = '<p class="some-class">' . __( "my-text-here", "your_theme_slug" ) . '</ p>';

    return $post_excerpt;

Is better to ad your text in a gettex for translation (replace "your_theme_slug" by the slug of your theme...

Reference: How to use different short description in shop page and in product page in woocommerce