Albert Alarcón Albert Alarcón - 1 year ago 131
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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download