在 Woocommerce 中我尝试添加文本before价格如果高于 59 欧元
我尝试了以下代码(以及其他代码),但它们不起作用:
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message( $price ) {
if ($price>='59'){
$new_price = $price .__('(GRATIS!)');
}
return $new_price;
}
我该怎么做才能添加(GRATIS!)
如果产品价格高于 59,请在产品价格前显示文字?
Updated:您应该尝试以下重新访问的函数代码:
add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
// Only on frontend and excluding min/max prices on variable products
if( is_admin() || $product->is_type('variable') )
return $price_html;
// Get product price
$price = (float) $product->get_price(); // Regular price
if( $price > 15 )
$price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;
return $price_html;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。
经过测试并有效。
For 仅单个产品页面您将替换这一行:
if( $price > 15 ){
By this:
if( $price > 15 && is_product() ){
For 单一产品页面和档案页面您将替换这一行:
if( $price > 15 ){
By this:
if( $price > 15 && ( is_product() || is_shop() || is_product_category() || is_product_tag() ){
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)