我使用下面的代码来显示节省的金额,其中的百分比略低于WooCommerce中简单产品和可变产品的价格.但它显示了正确的格式,恰好低于图中的价格.但在样品产品页面上,它显示的只是瓷砖,我多次更改了优先级,但没有达到预期的效果.

以下是示例产品的URL sample produt

变量产品的URL如下所示 varriable product

enter image description here

enter image description here

    // 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;
}

推荐答案

更新:

当您使用Astra主题时,请try 以下重新访问的代码:

// Inline CSS in the header for single product pages
add_action( 'wp_head', 'simple_product_saving_amount_css', 999 );
function simple_product_saving_amount_css() {
    if ( is_product() ) { ?>
    <style>#saving_total_price {font-size:12px;text-align:center;width:40%;background:lightgoldenrodyellow;margin-bottom:1em;}</style>
    <?php }
}

// For simple products
add_action( 'woocommerce_get_price_html', 'simple_product_saving_amount', 10, 2 );
function simple_product_saving_amount( $price_html, $product ) {
    global $woocommerce_loop;

    if( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) && $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 );

        $price_html .= sprintf('<p id="saving_total_price">%s: %s (%s)</p>', __("You Save"), wc_price($saved_amount), $percentage.'%' );
    }
    return $price_html;
}

// For product variations (on variable products)
add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 10, 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'] .= sprintf('<p id="saving_total_price">%s: %s (%s)</p>', 
            __("You Save"), wc_price($saved_amount), $percentage.'%' );
    }
    return $data;
}

经过测试,效果良好.

您将得到如下内容:

enter image description here

Php相关问答推荐

谷歌云SQL / mysql谁是对的?

使用WooCommerce本机产品短码时不考虑价格筛选(_SHORT CODE)

WordPress:将自定义单选输入字段添加到用户配置文件

将带有值的属性添加到WooCommerce产品,保留现有属性

在WooCommerce中禁用客户用户角色的纳税计算

获取并判断WooCommerce用户订阅结束日期

按与WooCommerce管理顺序列表中的特定字符串不匹配的特定元值筛选订单

更改WooCommerce checkout 中的错误消息以获得不可用的送货方式

隐藏WooCommerce管理子菜单;主页;来自store 经理

Shopware 6 插件:如何删除布尔配置并将其迁移到多选配置

如何判断php中wordpress路径之外的文件夹是否存在?

为什么 php 只能在我的 Nginx Web 服务器的某些目录中工作?

使用 phpseclib 验证 RSA-PSS

根据所选付款方式启用/禁用 WooCommerce 所需的 checkout 字段

$_SERVER 超全局变量在 PHP 中是否保证可写?

如何在 laravel 中的同一个 slug 上显示不同的内容?

ACF 更新字段功能不更新 WordPress 中的任何数据

批量作业(job)提交错误无法处理所有文档,uris 似乎正确?

Laravel 8:会话不适用

Laravel 模型将日期保存为空