在商店页面上显示单价和产品页面上的批发价格

时间:2018-02-22 22:29:52

标签: php wordpress woocommerce product price

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

有任何线索吗?

1 个答案:

答案 0 :(得分: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文件中。

经过测试和工作。