Search code examples
phpwordpresswoocommerceorders

Remove "shipping subtotal" from Woocommerce's new order emails


I have tried unsetting the code but then it removes totals too. I want to leave totals and remove shipping totals.

<?php
    if ( $totals = $order->get_order_item_totals() ) {
        $i = 0;
        foreach ( $totals as $total ) {
            $i++;
            ?><tr>
                <th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
                <td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
            </tr><?php
        }
    }
?>

I can't seem to find a solution for this. Any suggestions?

Thanks.


Solution

  • To remove shipping subtotal line try this code instead:

    <?php
    
    if ( $totals = $order->get_order_item_totals() ) {
        $i = 0;
        foreach ( $totals as $total_key => $total ) { // Changed HERE
            $i++;
            if( 'shipping' != $total_key ){ // Added HERE
            ?><tr>
                <th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
                <td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
            </tr><?php
            } // Added HERE
        }
    }
    
    ?>
    

    Or you can use unset() php function:

    <?php
    
    if ( $totals = $order->get_order_item_totals() ) {
        unset(totals['shipping']) // HERE
        $i = 0;
        foreach ( $totals as $total_key => $total ) { 
            $i++;
            ?><tr>
                <th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
                <td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
            </tr><?php
    }
    
    ?>