我正在尝试在WooCommerce文章的描述中创建一个自动文本,并放置"文章仅在商店中可用"。
我想把它放在一个函数里,像这样:
add_filter ('woocommerce_short_description', 'in_single_product', 10, 2);
function in_single_product () {
echo '<p> article only available in the store. </ p>';
}
但是这取代了已经写在产品简短描述中的文本。如果我没有放文字,什么也不会显示。
是否可以将代码文本"文章仅在商店中提供"而不包含产品的简短描述?
谢谢。
所以你可以这样使用:
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;
}
正常情况下,此代码将覆盖单个产品页面中现有的简短描述文本,如果该简短描述存在…
(更新)——与你的评论
如果你想在不覆盖摘录(简短描述)的情况下显示它,你可以这样添加它:
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;
}
所以你会在之前收到你的信息,之后(如果有简短的描述)简短的描述…
你可以在你的活动主题style.css
文件的类选择器.product-message
,例如:
.product-message {
background-color:#eee;
border: solid 1px #666;
padding: 10px;
}
你需要写你自己的样式规则来得到你想要的
我更新说我找到了解决我的问题的方法:
我在"products"中创建了一个"shipping class",命名为"article only in the store",并添加了一个"productshop"。
然后在(mytheme)/woocommerce/single-product/meta.php中我包含了:
<?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';}
}?>
那么我只需要选择将产品装运到方法中。
就是这样!
感谢您的回答