3

I have added 3 custom fees in my order table but in the emails sent to admin/client, I want to hide one of these custom fee.

See screen shot :

enter image description here

Actually, I added the custom fees with this snippet:

add_action( 'woocommerce_cart_calculate_fees','customfee1' );
 
function customfee1() {
    $soustotalavecliv = (WC()->cart->get_cart_contents_total()+WC()->cart->get_shipping_total());
    WC()->cart->add_fee( 'TOTAL', $soustotalavecliv );
}

add_action( 'woocommerce_cart_calculate_fees','customfee2' );
 
function customfee2() {
    $retraitsubt = (WC()->cart->get_cart_contents_total()+WC()->cart->get_shipping_total());
    WC()->cart->add_fee( 'A retirer', $retraitsubt*-1 );
}

add_action( 'woocommerce_cart_calculate_fees','customfee3' );
 
function customfee3() {
    $total_minus_100 = (WC()->cart->get_cart_contents_total()+WC()->cart->get_shipping_total()) /2;
    WC()->cart->add_fee( 'Acompte versé par CB', $total_minus_100 * -1 );
}

But in all the template emails, I want to hide the "customfee2". I managed to hide this with a CSS code. But the following code works only in the NEW ORDER EMAIL sent to admin and not the other emails.

function ymcode_hide_customfee2_row_with_css()
{
    ?>
    <style>
          tfoot tr:nth-child(5) {
             display: none !important;
      }
    </style>
    <?php
}

add_action('woocommerce_email_header', 'ymcode_hide_customfee2_row_with_css', 10, 0);

Any advice?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50

1 Answers1

2

First of all, I modified your existing code. This because:

  • It is not necessary to add the same code several times, since everything can happen once.
  • The use of WC()->cart is not necessary, as $cart is passed by default to the callback function.

So just use:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    // Getters
    $contents_total = $cart->get_cart_contents_total();
    $shipping_total = $cart->get_shipping_total();
    $total = $contents_total + $shipping_total;

    // 1
    // Add fee
    $cart->add_fee( __( 'TOTAL', 'woocommerce' ), $total );

    // 2
    // Calculate
    $retraitsubt = $total *-1;
    // Add fee
    $cart->add_fee( __( 'A retirer', 'woocommerce' ), $retraitsubt );

    // 3
    // Calculate
    $total_minus_100 = ( $total / 2 ) * -1;
    // Add fee
    $cart->add_fee( __( 'Acompte versé par CB', 'woocommerce' ), $total_minus_100 );
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );

To answer your question:

You can use the woocommerce_email_styles filter hook, which will allow you to add CSS to the emails versus having to overwrite the template files

So you get:

// Add CSS to email
function filter_woocommerce_email_styles( $css, $email ) {
    $extra_css = 'tfoot tr:nth-child(5) { display: none !important; }';
    
    return $css . $extra_css;
}
add_filter( 'woocommerce_email_styles', 'filter_woocommerce_email_styles', 10, 2 );

Note: Because the above answer via the filter hook is quite 'tricky' because no CSS class are assigned to the <tr> tag in the emails, overwriting the /emails/email-order-details.php file may be a 'safer' option.

  • This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-details.php.

Replace line 65 - 76 @version 3.7.0

if ( $item_totals ) {
    $i = 0;
    foreach ( $item_totals as $total ) {
        $i++;
        ?>
        <tr>
            <th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['label'] ); ?></th>
            <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['value'] ); ?></td>
        </tr>
        <?php
    }
}

With

if ( $item_totals ) {
    $i = 0;
    foreach ( $item_totals as $total ) {
        $i++;
        
        // Make sure this is correct!
        $label = 'A retirer:';
        
        // Check if the string is not equal to the label, and if not, display the table row
        if ( $total['label'] != $label ) {
        ?>
        <tr>
            <th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['label'] ); ?></th>
            <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['value'] ); ?></td>
        </tr>
        <?php
        }
    }
}
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50