WooCommerce产品简短说明中的自动文本 [英] Automatic text in short description of WooCommerce products
问题描述
我正在尝试在WooCommerce文章的描述中创建一个自动文本,并将文章仅在商店中可用".
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?
谢谢.
推荐答案
因此您可以通过以下方式使用它:
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…
您可以在活动主题style.css
文件中针对类选择器.product-message
设置样式,例如:
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.
这篇关于WooCommerce产品简短说明中的自动文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!