【问题标题】:Display on shop pages the unit price and the wholesale price on product pages在商店页面上显示产品页面上的单价和批发价
【发布时间】:2018-02-23 09:20:15
【问题描述】:

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

有什么线索吗?

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    您将保留您的产品批发价格(在单个产品页面中),并且在商店页面上,以下功能将显示单价(批发价格除以 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 文件中。

    经过测试并且有效。

    【讨论】:

    • 非常感谢,你太棒了!因为我需要它用于特定的产品类别(葡萄酒),所以我将它嵌套在 if( has_term( 'vins', 'product_cat') ) 上,效果很好。
    猜你喜欢
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多