在 Woocommerce 中,如果价格高于 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;
}
如果产品价格高于 59,如何在产品价格前添加 (GRATIS!)
文本?
最佳答案
更新:您应该尝试以下重新访问的函数代码:
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 single product pages only you will replace this line:
if( $price > 15 ){
By this:
if( $price > 15 && is_product() ){
For single product pages and archives pages you will replace this line:
if( $price > 15 ){
By this:
if( $price > 15 && ( is_product() || is_shop() || is_product_category() || is_product_tag() ){
关于php - 如果产品价格高于 Woocommerce 中的特定金额,请在产品价格前添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48552886/