我使用下面的代码来显示节省的金额,其中的百分比略低于WooCommerce中简单产品和可变产品的价格.但它显示了正确的格式,恰好低于图中的价格.但在样品产品页面上,它显示的只是瓷砖,我多次更改了优先级,但没有达到预期的效果.
以下是示例产品的URL sample produt个
变量产品的URL如下所示 varriable product个
// For simple products
add_action( 'woocommerce_single_product_summary', 'simple_product_saving_amount', 9 );
function simple_product_saving_amount() {
global $product;
if( $product->is_type('simple') && $product->is_on_sale() ) {
$regular_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_regular_price() ) );
$active_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_sale_price() ) );
$saved_amount = $regular_price - $active_price;
$percentage = round( $saved_amount / $regular_price * 100 );
echo '<p id="saving_total_price" style="
font-size: 12px;
text-align: center;
width: 40%;
background: lightgoldenrodyellow;
margin-bottom: 1em;
">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';
}
}
// For product variations (on variable products)
add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 12, 3 );
function variable_product_saving_amount( $data, $product, $variation ) {
if( $variation->is_on_sale() ) {
$saved_amount = $data['display_regular_price'] - $data['display_price'];
$percentage = round( $saved_amount / $data['display_regular_price'] * 100 );
$data['price_html'] .= '<p id="saving_total_price" style="
font-size: 12px;
text-align: center;
width: 40%;
background: lightgoldenrodyellow;
margin-bottom: 1em;
;
">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';
}
return $data;
}