WooCommerce购物车小计:显示“免费”而不是“ 0,00”

时间:2020-09-21 15:39:44

标签: php wordpress woocommerce cart hook-woocommerce

我正在寻找一种在WooCommerce购物车/结帐中显示“免费”而不是€0.00的解决方案。

我知道,单一产品本身有一个代码段,但是即使在结帐/购物车页面的小计/小计中,也可以更改价格标签吗?

add_filter( 'woocommerce_get_price_html', 'price_free_zero_empty', 9999, 2 );
   
function price_free_zero_empty( $price, $product ){
    if ( '' === $product->get_price() || 0 == $product->get_price() ) {
        $price = '<span class="woocommerce-Price-amount amount">FREE</span>';
    }  
    return $price;
}

1 个答案:

答案 0 :(得分:1)

尝试使用以下内容,当产品价格等于零时,该内容应显示“免费”:

// Cart and minicart
add_filter( 'woocommerce_cart_item_price', 'change_cart_item_price_html', 10, 3 );
function change_cart_item_price_html( $price_html, $cart_item, $cart_item_key ) {
    if( $cart_item['data']->get_price() == 0 ) {
        return '<span class="woocommerce-Price-amount amount">'.__("FREE", "woocommerce").'</span>';
    }
    return $price_html;
}

// Cart and Checkout
add_filter( 'woocommerce_cart_item_subtotal', 'change_checkout_item_subtotal_html', 10, 3 );
function change_checkout_item_subtotal_html( $subtotal_html, $cart_item, $cart_item_key ) {
    if( $cart_item['data']->get_price() == 0 ) {
        return '<span class="woocommerce-Price-amount amount">'.__("FREE", "woocommerce").'</span>';
    }
    return $subtotal_html;
}

代码进入活动子主题(或活动主题)的functions.php文件中。应该可以。