谷歌商店显示价格PHP,php – 显示Woocommerce产品的折扣价格和百分比

您的代码有点过时,因为woocommerce版本3作为产品对象属性无法直接访问.相反,你应该使用可用的WC_Product方法.

要格式化价格,您将使用wc_price()专用格式化功能.

现在你可以拥有(3种可能性):

1)节省价格:

add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );

function change_displayed_sale_price_html( $price, $product ) {

// Only on sale products on frontend and excluding min/max price on variable products

if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){

// Get product prices

$regular_price = (float) $product->get_regular_price(); // Regular price

$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

// "Saving price" calculation and formatting

$saving_price = wc_price( $regular_price - $sale_price );

// Append to the formated html price

$price .= sprintf( __('

Save: %s

', 'woocommerce' ), $saving_price );

}

return $price;

}

2)储蓄百分比:

add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );

function change_displayed_sale_price_html( $price, $product ) {

// Only on sale products on frontend and excluding min/max price on variable products

if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){

// Get product prices

$regular_price = (float) $product->get_regular_price(); // Regular price

$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

// "Saving Percentage" calculation and formatting

$precision = 1; // Max number of decimals

$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';

// Append to the formated html price

$price .= sprintf( __('

Save: %s

', 'woocommerce' ), $saving_percentage );

}

return $price;

}

3他们两个(折扣价格和百分比):

add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );

function change_displayed_sale_price_html( $price, $product ) {

// Only on sale products on frontend and excluding min/max price on variable products

if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){

// Get product prices

$regular_price = (float) $product->get_regular_price(); // Regular price

$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

// "Saving price" calculation and formatting

$saving_price = wc_price( $regular_price - $sale_price );

// "Saving Percentage" calculation and formatting

$precision = 1; // Max number of decimals

$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';

// Append to the formated html price

$price .= sprintf( __('

Save: %s (%s)

', 'woocommerce' ), $saving_price, $saving_percentage );

}

return $price;

}

代码位于活动子主题(或活动主题)的function.php文件中.

经过测试和工作.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值