php 在WooCommerce 6(非特价徽章)或移动特价徽章中显示价格前的折扣百分比

dluptydi  于 2023-01-19  发布在  PHP
关注(0)|答案(1)|浏览(114)

我不使用“打折徽章”,因为它不显示%折扣在主商店页面上,我需要显示价格前的百分比。我有一个代码从旧WooCommerce(也许)它不工作,它显示0%,请帮助我重写函数。或者显示如何移动“打折徽章”到一行与产品的价格。tnx

add_filter( 'woocommerce_get_price_html', 'ywp_single_price_html', 100, 2 );
function ywp_single_price_html( $price, $product ) {
    $html = $price;

    if ( ! is_admin() ) {
        $discount = '';

        $regular_price = $product->is_type( 'variable' ) ? $product->get_variation_regular_price( 'min', true ) : $product->get_regular_price();
        $sale_price    = $product->is_type( 'variable' ) ? $product->get_variation_sale_price( 'min', true ) : $product->get_sale_price();

        if ( $regular_price && $sale_price ) {
            $html .= ywp_get_sale_percentage( $product );
        }

        if ( $product->is_on_sale() ) {
            $html = str_replace( '<del>', '<del>M.R.P.: ', $html );
            $html = str_replace( '<ins>', '<ins>Price: ', $html );
        }
    }

    return $html;
}

function ywp_get_sale_percentage( $product ) {
    $max_percentage = 0;
    $save_price     = 0;

    if ( $product->is_on_sale() ) {
        if ( ! $product->is_type( 'variable' ) ) {
            $max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
            $save_price     = $product->get_regular_price() - $product->get_sale_price();
        } else {
            foreach ( $product->get_children() as $child_id ) {
                $variation = wc_get_product( $child_id );
                $price     = $variation->get_regular_price();
                $sale      = $variation->get_sale_price();
                $percentage = $price - $sale;

                $percentage = 0;
                if ( $price != 0 && ! empty( $sale ) ) {
                    $percentage = ( $price - $sale ) / $price * 100;
                }
                if ( $percentage > $max_percentage ) {
                    $max_percentage = $percentage;
                }
            }
        }
    }

    $discount = '<span class="discount-percent">(' . round( $max_percentage ) . '%)</span>';

    return $discount;
}

我搜索了这个,我发现了许多代码来实现它,但并不是所有的代码都适合我。我需要为简单产品和可变产品这样做,有人能告诉我如何实现它吗?我需要像这样显示折扣百分比enter image description here

rsaldnfx

rsaldnfx1#

您需要检查是否有有效的销售价格(正常/销售价格的差异)

add_filter( 'woocommerce_get_price_html', 'ywp_single_price_html', 100, 2 );
function ywp_single_price_html( $price, $product ) {
    $html = $price;

    if ( ! is_admin() ) {
        $discount = '';

        $regular_price = $product->is_type( 'variable' ) ? $product->get_variation_regular_price( 'min', true ) : $product->get_regular_price();
        $sale_price    = $product->is_type( 'variable' ) ? $product->get_variation_sale_price( 'min', true ) : $product->get_sale_price();

        if ( ( $regular_price && $sale_price ) and ($regular_price !== $sale_price) ) {
            $html .= ywp_get_sale_percentage( $product );
        }

        if ( $product->is_on_sale() ) {
            $html = str_replace( '<del>', '<del>M.R.P.: ', $html );
            $html = str_replace( '<ins>', '<ins>Price: ', $html );
        }
    }

    return $html;
}

function ywp_get_sale_percentage( $product ) {
    $max_percentage = 0;
    $save_price     = 0;

    if ( $product->is_on_sale() ) {
        if ( ! $product->is_type( 'variable' ) ) {
            $max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
            $save_price     = $product->get_regular_price() - $product->get_sale_price();
        } else {
            foreach ( $product->get_children() as $child_id ) {
                $variation = wc_get_product( $child_id );
                $price     = $variation->get_regular_price();
                $sale      = $variation->get_sale_price();
                $percentage = $price - $sale;

                $percentage = 0;
                if ( $price != 0 && ! empty( $sale ) ) {
                    $percentage = ( $price - $sale ) / $price * 100;
                }
                if ( $percentage > $max_percentage ) {
                    $max_percentage = $percentage;
                }
            }
        }
    }

    $discount = '<span class="discount-percent">(' . round( $max_percentage ) . '%)</span>';

    return $discount;
}

相关问题