我想在产品标题下显示销售百分比.这是我在functions.php页面的函数,但我得到NAN % Off

    // Function to calculate and display sale percentage.
function display_sale_percentage() {
    global $product;

    if ( $product->is_on_sale() ) {
        // Get regular and sale prices
        $regular_price = $product->get_regular_price();
        $sale_price = $product->get_sale_price();

        // Calculate discount percentage
        $discount_percentage = round( ( ($regular_price - $sale_price) / $regular_price ) * 100 );

        // Display the percentage
        echo '<span class="sale-percentage">' . $discount_percentage . '% off</span>';
    }
}

// Hook the function to a suitable location in your product archive template.
// For example, you can hook it into the product loop.
add_action( 'woocommerce_before_shop_loop_item_title', 'display_sale_percentage', 25 );

推荐答案

您的代码需要处理可变产品及其变化价格.

对于可变产品,将显示以下代码

  • 折扣百分比范围,如果有至少2个不同折扣百分比的销售变化,
  • 一个唯一的折扣百分比,如果销售变动折扣百分比

对于正在销售的简单产品,将显示折扣百分比.

// Utility function: Get an array of discount percentages from the variations
function get_variations_discount_percentage( $product ) {
    $prices = $product->get_variation_prices( true );
    $percentages = array();

    foreach( $prices['regular_price']  as $key_id => $price ) {
        if( $price != $prices['price'][$key_id] ) {
            $reg_price  = floatval($price);
            $sale_price = floatval($prices['price'][$key_id]);
            $percentages[] = round( ($reg_price - $sale_price) / $reg_price * 100);
        }
    }
    return array_unique($percentages);
}

// Displays On Sale product discount percentage
add_action( 'woocommerce_before_shop_loop_item_title', 'display_products_on_sale_percentage', 25 );
function display_products_on_sale_percentage() {
    global $product;

    if ( ! $product->is_on_sale() ) return; // Only "On sale" products

    if ( $product->is_type('variable') ) {
        $percentages = get_variations_discount_percentage( $product );

        if ( count($percentages) > 1 ) {
            // Display the discount percentages range
            printf( '<span class="sale-percentage">From %d%% to %d%% off</span>', min($percentages), max($percentages) );
        } elseif ( count($percentages) === 1 ) {
            // Display the discount percentage
            printf( '<span class="sale-percentage">%d%% off</span>', current($percentages) );
        }
    } else {
        $reg_price  = (float) $product->get_regular_price();
        $sale_price = (float) $product->get_sale_price();

        // Display the discount percentage
        printf( '<span class="sale-percentage">%d%% off</span>',
            round( ($reg_price - $sale_price) / $reg_price * 100) );
    }
}

代码将在子主题的functions.php文件中(或插件中).测试和工作.

Php相关问答推荐

在PHP中替换数组中的文本并同时使用其他函数

允许Laravel路由指定`withTrashed`,尽管有显式的模型绑定

Yahoo Finance API文件_GET_CONTENTS 429请求太多

如何减少Laravel分页中的链接数

如何显示每个分类的当前术语的帖子中包含的前20个标签,不包括标签分类

Sylius php -如何将购物车传递到小枝模板(Sylius模板事件)

添加并显示单价字段到WooCommerce产品变化

如何不重新查询模型A-&>;相关模型B-&>模型

使用php ZipArhive类将Zip压缩文件分成多个部分

即使在WooCommerce购物车中添加了两次产品,也要将所有项目设置为空白行

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

在具有Angular 的 Laravel 应用程序中,不显示图像

使用帖子 ID 更新 wp 帖子

PHP 解析 XML 以获取多个深度的所有字符串值

将产品数量、名称和结帐链接添加到 WooCommerce 添加到购物车消息

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

如何在 Laravel 中修改嵌套数组的键?

仅允许减少 WooCommerce 中已完成订单的库存数量

为什么可以从 PHP 类访问 PHP Trait 的私有成员?

PHP - 如何获取发送到脚本的查询参数