Gousia
Gousia

Reputation: 63

Remove the shipping row from the order table in Woocommerce email notifications

In WooCommerce I am trying to remove the shipping row from the order table in email notifications.

enter image description here

Upvotes: 1

Views: 2095

Answers (2)

Omar Shishani
Omar Shishani

Reputation: 507

Expanding on @LoicTheAztec and updating for 2022:

Replace <tfoot> in "email-order-details.php" WooCommerce template with code below. Main changes are:

  1. Adding a key name to the foreach loop, key_total
  2. Adding a conditional to ignore printing the row if key_total === 'shipping'
<tfoot>
            <?php
            $item_totals = $order->get_order_item_totals();

            if ( $item_totals ) {
                $i = 0;
                // Add "$key_total" to be able to grab the key name
                foreach ( $item_totals as $key_total => $total ) {
                    $i++;
                    // Add conditional to only print row if row key is not "shipping" 
                    if( $key_total !== 'shipping' ):
                        ?>
                        <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
                    endif;
                }
            }
            
            if ( $order->get_customer_note() ) {
                ?>
                <tr>
                    <th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Note:', 'woocommerce' ); ?></th>
                    <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo wp_kses_post( nl2br( wptexturize( $order->get_customer_note() ) ) ); ?></td>
                </tr>
                <?php
            }
            ?>
        </tfoot>

We can see an example of how the key name is laid out in the Order Details object. Image below comes from printing $order->get_order_item_totals(); to error_log

print_r to error_log of $order->get_order_item_totals();

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254373

Overriding woocommerce templates via the theme, to remove the shipping row in email notifications can be done easily adding few code to emails/email-order-details.php template.

Here is an extract starting at line 51. So you will replace all the code beetwen html opening <tfoot> tag and closing</tfoot> tag with the following code:

        <tfoot>
            <?php
                if ( $totals = $order->get_order_item_totals() ) {
                    $i = 0;
                    foreach ( $totals as $key_total => $total ) {
                        $i++;
                        if( $key_total != 'shipping' ):
                        ?><tr>
                            <th class="td" scope="row" colspan="2" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['label']; ?></th>
                            <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['value']; ?></td>
                        </tr><?php
                        endif;
                    }
                }
                if ( $order->get_customer_note() ) {
                    ?><tr>
                        <th class="td" scope="row" colspan="2" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Note:', 'woocommerce' ); ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>;"><?php echo wptexturize( $order->get_customer_note() ); ?></td>
                    </tr><?php
                }
            ?>
        </tfoot>

This is tested and works. So you will get something like (Without the shipping row):

enter image description here

Upvotes: 2

Related Questions