自定义 WooCommerce 单一产品,但不在其他产品循环上
Customize WooCommerce single products but not on other product loops
我的 woocommerce 价格挂钩中有一些自定义代码,它使网站上的所有地方都发生了变化。我只想让它影响产品页面(目前我们所有的产品都是简单的产品)。
在设计产品系列页面时,下面的代码片段会出现在产品页面上。我尝试应用 if 语句仅在产品页面上对 woocommerce_format_sale_price 进行更改,但这似乎没有用。
有什么建议吗?
function add_discount_woocommerce_format_sale_price( $price, $regular_price, $sale_price ){
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$price = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del><br><span class="plan-atc">*Payment Plan Available at Checkout</span><br> <span class="below-atc">Add to cart today, get ' . $percentage . __( ' off </span>', 'text-domain' );
return $price;
}
add_filter( 'woocommerce_format_sale_price', 'add_discount_woocommerce_format_sale_price', 99, 3 );
谢谢!
您可以使用 is_product()
条件函数,但这还不够,因为单个产品页面上还有其他产品循环,例如追加销售产品、相关产品……
要使您的功能仅对单个产品页面(而不是其他产品循环)有效,请使用以下内容:
add_filter( 'woocommerce_format_sale_price', 'display_percentage_discount_on_sale_price_format', 99, 3 );
function display_percentage_discount_on_sale_price_format( $price, $regular_price, $sale_price ){
global $woocommerce_loop;
// Only on single product pages (and not on other product loops)
if( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) {
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$price = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del><br><span class="plan-atc">*Payment Plan Available at Checkout</span><br> <span class="below-atc">Add to cart today, get ' . $percentage . __( ' off </span>', 'text-domain' );
}
return $price;
}
现在缺少结尾 </span>
。请尝试使用以下重新访问的代码:
add_filter( 'woocommerce_format_sale_price', 'display_percentage_discount_on_sale_price_format', 99, 3 );
function display_percentage_discount_on_sale_price_format( $price, $regular_price, $sale_price ){
global $woocommerce_loop;
// Only on single product pages (and not on other product loops)
if( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) {
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$text_end = sprintf( __("Add to cart today, get %s off", "woocommerce"), $percentage );
$text_prefix = sprintf( ' <br><span class="plan-atc">%s</span> <br><span class="below-atc">%s</span>',
__("Payment Plan Available at Checkout", "woocommerce"), $text_end );
$price_html = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del>' . $text_prefix;
}
return $price_html;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
我的 woocommerce 价格挂钩中有一些自定义代码,它使网站上的所有地方都发生了变化。我只想让它影响产品页面(目前我们所有的产品都是简单的产品)。
在设计产品系列页面时,下面的代码片段会出现在产品页面上。我尝试应用 if 语句仅在产品页面上对 woocommerce_format_sale_price 进行更改,但这似乎没有用。
有什么建议吗?
function add_discount_woocommerce_format_sale_price( $price, $regular_price, $sale_price ){
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$price = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del><br><span class="plan-atc">*Payment Plan Available at Checkout</span><br> <span class="below-atc">Add to cart today, get ' . $percentage . __( ' off </span>', 'text-domain' );
return $price;
}
add_filter( 'woocommerce_format_sale_price', 'add_discount_woocommerce_format_sale_price', 99, 3 );
谢谢!
您可以使用 is_product()
条件函数,但这还不够,因为单个产品页面上还有其他产品循环,例如追加销售产品、相关产品……
要使您的功能仅对单个产品页面(而不是其他产品循环)有效,请使用以下内容:
add_filter( 'woocommerce_format_sale_price', 'display_percentage_discount_on_sale_price_format', 99, 3 );
function display_percentage_discount_on_sale_price_format( $price, $regular_price, $sale_price ){
global $woocommerce_loop;
// Only on single product pages (and not on other product loops)
if( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) {
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$price = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del><br><span class="plan-atc">*Payment Plan Available at Checkout</span><br> <span class="below-atc">Add to cart today, get ' . $percentage . __( ' off </span>', 'text-domain' );
}
return $price;
}
现在缺少结尾 </span>
。请尝试使用以下重新访问的代码:
add_filter( 'woocommerce_format_sale_price', 'display_percentage_discount_on_sale_price_format', 99, 3 );
function display_percentage_discount_on_sale_price_format( $price, $regular_price, $sale_price ){
global $woocommerce_loop;
// Only on single product pages (and not on other product loops)
if( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) {
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$text_end = sprintf( __("Add to cart today, get %s off", "woocommerce"), $percentage );
$text_prefix = sprintf( ' <br><span class="plan-atc">%s</span> <br><span class="below-atc">%s</span>',
__("Payment Plan Available at Checkout", "woocommerce"), $text_end );
$price_html = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del>' . $text_prefix;
}
return $price_html;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。