Automatic text in short description of WooCommerce products

非 Y 不嫁゛ 提交于 2019-12-19 04:34:07

问题


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.


回答1:


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;

    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    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;

    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    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.




回答2:


I update to say I found a solution to my problem:

I created a "shipping class" in "products" with the name "article only available in the store" and the slug "productshop".

Then in (mytheme)/woocommerce/single-product/meta.php I have included:

<?php
$clase=$product->get_shipping_class();
if ($clase=="productshop") {
if (get_locale()=='en_US') {echo 'Product only available in store';}
else {echo 'Producte només disponible a la botiga';}
}?>

Then I have only to select the shipping of product into the method.

And that's it!

Thanks for your answers



来源:https://stackoverflow.com/questions/38098914/automatic-text-in-short-description-of-woocommerce-products

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!