Albert Alarcón Albert Alarcón - 4 months ago 15
PHP Question

Automatic text in short description of WooCommerce products

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> article only available in the store. </ p>';
}


But this replace the text that is already written in the short description of the product. If I have not put text, nothing appears.

Is possible put the text of the code "article only available in the store" without a short description of the product?

Thank you.

Answer

So you can use it this way:

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">' . __( "article only available in the store.", "woocommerce" ) . '</p>';

    return $post_excerpt;
}

Normally this code will override existing short description text in single product pages, if this short description exist…


(update) - Related to your comment

If you want to display this without overriding the excerpt (short description), you can add it before this way:

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 = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>' . $post_excerpt;

    return $post_excerpt;
}

So you will get your message before, and after (if short description exist) the short description…

You can style it targeting in your active theme style.css file the class selector .product-message, for example this way:

.product-message {
    background-color:#eee;
    border: solid 1px #666;
    padding: 10px;
}

You will need to write your own style rules to get it as you want.