3

I am developing a booking system in which the client wants only to take a USD 50 deposit and to negotiate the remaining amount separately. In order to achieve this I have used the following code to update the total price to USD 50 and to show the remaining price.

function prefix_add_discount_line( $cart ) {
  $deposit = 50;    
  $remaining = $cart->subtotal - 50;
  $cart->add_fee( __( 'Amount Remaining', 'remaining' ) , -$remaining); 
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' ); 

In the order emails the remaining amount is displayed with a minus(-) symbol. Please let me know how to remove the minus symbol in the woocommerce order emails

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Disura
  • 48
  • 5

2 Answers2

2

To make all negative fees amounts to be displayed as a positive amount on WooCommerce Orders totals rows, use the following:

add_filter( 'woocommerce_get_order_item_totals', 'custom_order_total_line_html', 10, 3 );
function custom_order_total_line_html( $total_rows, $order, $tax_display ){
    // Loop through WooCommerce orders total rows
    foreach ( $total_rows as $key_row => $row_values ) {
        // Target only "fee" rows
        if ( strpos($key_row, 'fee_') !== false ) {
            $total_rows[$key_row]['value'] = str_replace('-', '', $row_values['value']);
        }
    }
    return $total_rows;
}

Now to make that only for WooCommerce email notifications, use instead this:

add_filter( 'woocommerce_get_order_item_totals', 'custom_order_total_line_html', 10, 3 );
function custom_order_total_line_html( $total_rows, $order, $tax_display ){
    // Only on emails
    if ( ! is_wc_endpoint_url() ) {
        // Loop through WooCommerce orders total rows
        foreach ( $total_rows as $key_row => $row_values ) {
            // Target only "fee" rows
            if ( strpos($key_row, 'fee_') !== false ) {
                $total_rows[$key_row]['value'] = str_replace('-', '', $row_values['value']);
            }
        }
    }
    return $total_rows;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0

The other answer didn't work for me.

add_filter( 'woocommerce_cart_totals_fee_html', 'remove_minus_sign_from_wc_fee', 10, 2 );
function remove_minus_sign_from_wc_fee( $fee_html, $fee ) {
    if ( 0 > $fee->amount ) {
        $fee_html = str_replace( '-', '', $fee_html );
    }
    return $fee_html;
}
Vijay Hardaha
  • 2,411
  • 1
  • 7
  • 16