woocommerce flash sale - деление предупреждений на ноль - PullRequest
0 голосов
/ 01 июня 2018

После обновления woocommerce я получаю предупреждение: ошибка деления на ноль с помощью функции ниже.

Я не понимаю, почему эта ошибка не появлялась раньше, в настоящее время даже при отключенной отладке wp ошибка все еще отображается.

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

есть идеи, почему это стало проблемой?

Спасибо

1 Ответ

0 голосов
/ 01 июня 2018

Удалось поработать над этим.

обычная цена была равна нулю при использовании составного продукта, который не был в продаже.поэтому я обернул строку, в которой произошел сбой, оператором 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;
        }
    }
} 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...