在商店页面上显示产品页面上的单价和批发价 [英] Display on shop pages the unit price and the wholesale price on product pages

查看:74
本文介绍了在商店页面上显示产品页面上的单价和批发价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Woocommerce, I have set my products with a wholesale price. I'm trying to display the unit price of the products only in shop pages, which should be the wholesale price divided by 6.

有什么线索吗?

推荐答案

您将保留您的产品批发价格(在单个产品页面中),并且在商店页面上,以下功能将显示单价(批发价格除以 6) 可选地加上(unit)"之类的文字:

You will keep your product wholesale price are they are (in single product pages) and on shop page the following function will display the unit price (the wholesale price divided by 6) prepended optionally with a text like "(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 文件中.

Code goes in function.php file of your active child theme (or theme).

经过测试并有效.

这篇关于在商店页面上显示产品页面上的单价和批发价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆