首页 > 解决方案 > woocommerce 闪购 - 警告除以零

问题描述

由于更新 woocommerce 我收到警告:使用以下函数除以零错误。

我不明白为什么这个错误之前没有出现,目前即使关闭 wp debug 仍然显示错误。

function 6516_product_sale_flash( $output, $post, $product ) {
    global $product;
    if($product->is_on_sale()) {
        if($product->is_type( 'variable' ) )
        {
            $regular_price = $product->get_variation_regular_price();
            $sale_price = $product->get_variation_price();
        } else {
            $regular_price = $product->get_regular_price();
            $sale_price = $product->get_sale_price();
        }
        $percent_off = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
        return '<span class="onsale">' . 'Save<br>' . round($percent_off) . '%</span>';
    }
}

关于为什么现在这是一个问题的任何想法?

谢谢

标签: phpwordpressfunctionwoocommercewarnings

解决方案


设法解决了这个问题。

使用非销售的复合产品时,正常价格为零。所以我用一个解决了问题的 if 语句包裹了出错的行;

function 6516_product_sale_flash( $output, $post, $product ) {
    global $product;
    if($product->is_on_sale()) {
        if($product->is_type( 'variable' ) )
        {
            $regular_price = $product->get_variation_regular_price();
            $sale_price = $product->get_variation_price();
        } else {
            $regular_price = $product->get_regular_price();
            $sale_price = $product->get_sale_price();
        }

        if ($regular_price > 0) {

        $percent_off = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
        return '<span class="onsale">' . 'Save<br>' . round($percent_off) . '%</span>';

        }

        else {
            return null;
        }
    }
} 

推荐阅读