店铺页面显示单价,产品页面显示批发价格

问题描述 投票:0回答:1

在 Woocommerce 中,我已为我的产品设置了批发价。我试图仅在商店页面中显示产品的单价,这应该是批发价除以 6。

有什么线索吗?

php wordpress woocommerce product product-price
1个回答
0
投票

您将保留产品的批发价(在单个产品页面中),并且在商店页面上,以下功能将显示单价(批发价除以6),可选地在前面加上“

(unit)
”等文本:

add_filter( 'woocommerce_get_price_html', 'unit_product_price_on_archives', 10, 2 );
function unit_product_price_on_archives( $price, $product ) {
    if ( is_shop() || is_product_category() || is_product_tag() ) {
        $unit_divider = 6;
        $suffix = ' '. __('(unit)', 'woocommerce');

        if( $product->is_type('variable') )
        {
            $unit_price_min  = $product->get_variation_price('min') / $unit_divider;
            $unit_price_fmin = wc_get_price_to_display( $product, array( 'price' => $unit_price_min ) );
            $unit_price_max  = $product->get_variation_price('max') / $unit_divider;
            $unit_price_fmax = wc_get_price_to_display( $product, array( 'price' => $unit_price_max ) );

            if( $unit_price_min != $unit_price_max )
                $price = wc_format_price_range( $unit_price_fmin, $unit_price_fmax ) . $suffix;
            else
                $price = wc_price($unit_price_fmin) . $suffix;
            if( $unit_price_max == 0 )
                $price = '';
        }
        elseif( $product->is_type('grouped') )
        {
            $tax_mode     = get_option( 'woocommerce_tax_display_shop' );
            $children     = array_map( 'wc_get_product', $product->get_children() );
            $child_prices = array();
            foreach ( $children as $child ){
                if ( '' !== $child->get_price() ) {
                    $child_prices[] = 'incl' === $tax_mode ? wc_get_price_including_tax( $child ) : wc_get_price_excluding_tax( $child );
                }
            }
            if ( ! empty( $child_prices ) ) {
                $min_price = min( $child_prices );
                $max_price = max( $child_prices );
            } else {
                $min_price = $max_price = '';
            }
            if ( '' !== $min_price ) {
                $min_price /= $unit_divider;
                $max_price /= $unit_divider;
                $price = $min_price !== $max_price ? wc_format_price_range( $min_price, $max_price ) : wc_price( $min_price );
            } else {
                $price = '';
            }
        }
        elseif( $product->is_on_sale() )
        {
            $regular_price_unit = $product->get_regular_price() / $unit_divider;
            $regular_price_unit = wc_get_price_to_display( $product, array( 'price' => $regular_price_unit ) );
            $sale_price_unit = $product->get_sale_price() / $unit_divider;
            $sale_price_unit = wc_get_price_to_display( $product, array( 'price' => $sale_price_unit ) );

            $price = wc_format_sale_price( $regular_price_unit, $sale_price_unit ) . $suffix;
        }
        else
        {
            $unit_price = $product->get_price() / $unit_divider;
            $unit_price = wc_get_price_to_display( $product, array( 'price' => $unit_price ) );

            $price = wc_price($unit_price) . $suffix;
        }
    }
    return $price;
}

代码位于活动子主题(或主题)的 function.php 文件中。

已测试且有效。

© www.soinside.com 2019 - 2024. All rights reserved.