首页 > 解决方案 > WooCommerce 购物车小计:显示“免费”而不是“0,00”

问题描述

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

我知道,Single Produkt 本身有一个片段,但是即使在结帐/购物车页面上的小计/总计计算中,是否有可能更改价格标签?

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;
}

标签: phpwordpresswoocommercecarthook-woocommerce

解决方案


尝试使用以下在产品价格为零时应显示“免费”的内容:

// 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 文件中。它应该有效。


推荐阅读