WooCommerce cart subtotal: Display 'free' instead of '0,00'

左心房为你撑大大i 提交于 2021-01-20 13:01:46

问题


i am searching for a solution to display 'free' instead of €0,00 in the WooCommerce Cart/Checkout.

I know, there is a snippet for the Single Produkt itself, but is there a possibillity to change the price label even in the Subtotal/total calulation on the checkout/cart page?

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:


Try to use the following that should display "Free" when the product price is equal to Zero:

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

Code goes in functions.php file of your active child theme (or active theme). It should works.



来源:https://stackoverflow.com/questions/63995559/woocommerce-cart-subtotal-display-free-instead-of-0-00

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!