使用Woocommerce 3中的自定义文本替换产品零显示价格

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

我正在寻找一种方法来替换WooCommerce(版本3.3.x)产品的€0,00价格与文本,如'基于合同'。最好在网上商店和电子邮件中可见。由于最近WooCommerce的变化,现有的片段不再起作用,例如:

add_filter('woocommerce_get_price_html', 'changeFreePriceNotice', 10, 2);

function changeFreePriceNotice($price, $product) {
    if ( $price == wc_price( 0.00 ) )
        return 'FREE';
    else
        return $price;
}

要么

<?php
// Do not copy the opening <?php tag above
// Customize the Free! price output
add_filter( 'woocommerce_variable_free_price_html','wsm_custom_free_price' );
add_filter( 'woocommerce_free_price_html', 'wsm_custom_free_price' );
add_filter( 'woocommerce_variation_free_price_html', 'wsm_custom_free_price' );
function wsm_custom_free_price( $price ) {
    // Edit content between single quotes below to desired output
    return 'Prepaid Inventory';
}

要么

function my_wc_custom_get_price_html( $price, $product ) {
    if ( $product->get_price() == 0 ) {
        if ( $product->is_on_sale() && $product->get_regular_price() ) {
            $regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) );

            $price = wc_format_price_range( $regular_price, __( 'Free!', 'woocommerce' ) );
        } else {
            $price = '<span class="amount">' . __( 'Free!', 'woocommerce' ) . '</span>';
        }
    }

    return $price;
}

add_filter( 'woocommerce_get_price_html', 'my_wc_custom_get_price_html', 10, 2 );
php wordpress woocommerce product price
1个回答
3
投票

这是在Woocommerce 3+上进行此操作的正确方法(在WooCommerce 3.3.x和3.5+上测试过):

// Product displayed prices
add_filter( 'woocommerce_get_price_html', 'free_price_custom_label', 20, 2 );
function free_price_custom_label( $price, $product ) {
    if ( is_shop() || is_product_category() || is_product_tag() || is_product() ) {
        // HERE your custom free price label
        $free_label = '<span class="amount">' . __('Based on contract') . '</span>';

        if( $product->is_type('variable') )
        {
            $price_min  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price('min') ) );
            $price_max  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price('max') ) );

            if( $price_min != $price_max ){
                if( $price_min == 0 && $price_max > 0 )
                    $price = wc_price( $price_max );
                elseif( $price_min > 0 && $price_max == 0 )
                    $price = wc_price( $price_min );
                else
                    $price = wc_format_price_range( $price_min, $price_max );
            } else {
                if( $price_min > 0 )
                    $price = wc_price( $price_min);
                else
                    $price = $free_label;
            }
        }
        elseif( $product->is_on_sale() )
        {
            $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
            $sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
            if( $sale_price > 0 )
                $price = wc_format_sale_price( $regular_price, $sale_price );
            else
                $price = $free_label;
        }
        else
        {
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) );
            if( $active_price > 0 )
                $price = wc_price($active_price);
            else
                $price = $free_label;
        }
    }
    return $price;
}

// Product variation displayed prices
add_filter( 'woocommerce_variable_price_html', 'free_variation_price_custom_label', 20, 2 );
function free_variation_price_custom_label( $price, $product ) {
    // HERE your custom free price label
    $free_label = '<span class="amount">' . __('Based on contract') . '</span>';

    if( $product->is_on_sale() )
    {
        $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
        $sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
        if( $sale_price > 0 )
            $price = wc_format_sale_price( $regular_price, $sale_price );
        else
            $price = $free_label;
    }
    else
    {
        $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) );
        if( $active_price > 0 )
            $price = wc_price($active_price);
        else
            $price = $free_label;
    }
    return $price;
}

// Cart items displayed prices and line item subtotal
add_filter( 'woocommerce_cart_item_subtotal', 'free_cart_item_price_custom_label', 20, 3 );
add_filter( 'woocommerce_cart_item_price', 'free_cart_item_price_custom_label', 20, 3 );
function free_cart_item_price_custom_label( $price, $cart_item, $cart_item_key ) {
    // HERE your custom free price label
    $free_label = '<span class="amount">' . __('Based on contract') . '</span>';

    if( $cart_item['data']->get_price() > 0 )
        return $price;
    else
        return $free_label;
}

// Order items displayed prices (and also email notifications)
add_filter( 'woocommerce_order_formatted_line_subtotal', 'free_order_item_price_custom_label', 20, 3 );
function free_order_item_price_custom_label( $subtotal, $item, $order ) {
    // HERE your custom free price label
    $free_label = '<span class="amount">' . __('Based on contract') . '</span>';

    if( $order->get_line_subtotal( $item ) > 0 )
        return $subtotal;
    else
        return $free_label;
}

此代码位于活动子主题(或主题)的function.php文件中。经过测试和工作。

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