JackPDLTD
JackPDLTD

Reputation: 89

Order item names repeating beneath table in emails in WooCommerce

I have a function that's hooked to woocommerce_order_item_name for the purpose of adding a notice under item names for dropshipping items and backordered items:

add_filter( 'woocommerce_order_item_name', 'custom_order_item_notices', 10, 2 );
function custom_order_item_notices( $item_name, $item ) {
    if ( is_admin() )
        return;
    $echoed = false;

    if ( is_cart() || is_checkout() || is_wc_endpoint_url( 'view-order' ) ) {

        $backorder_qty = 0;
        $product = $item->get_product();

        $DID = strtoupper(get_post_meta($item['product_id'] , 'direct-dispatch', true));
        if ($DID) {
            switch ($DID) {
                case 'BEE':
                    echo $item_name . '<small style="display: block;color: grey;">These items will be 
sent directly from the supplier to your specified delivery address. Please allow 3-5 working days for 
delivery. <a target="_blank" href="/delivery/#direct-despatch">Click here</a> for more info.</small>';
                    $echoed = true;
                    break;
            }
        }

        if (get_post_meta($item->get_order_id(), 'split-order-type', true) == 'BKO') {
            echo $item_name . '<small style="display: block;color: grey;">More stock is on it’s way! Please allow 5-10 working days for delivery. <a target="_blank" href="/delivery/#backorders">Click here</a> for more info.</small>';
            $echoed = true;
        }
    }

    if (!$echoed) echo $item_name;
}

I'm getting an issue I can't explain in all the e-mail templates that display order items.

The issue being that the order item names repeat again beneath the order table.

Please see below image of output:

email output

There's obviously something wrong with my function to be causing this, but after a few hours trying to suss this out, I still can't even figure out where the output is coming from in the first place so any help or insights would be greatly appreciated.

Upvotes: 3

Views: 96

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

You use echo versus return, I believe this will solve your issue

function custom_order_item_notices( $item_name, $item ) {
    if ( is_admin() )
        return;

    if ( is_cart() || is_checkout() || is_wc_endpoint_url( 'view-order' ) ) {

        $backorder_qty = 0;
        $product = $item->get_product();

        $DID = strtoupper( get_post_meta($item['product_id'] , 'direct-dispatch', true) );

        $DID = 'BEE';

        if ( $DID ) {
            switch ( $DID ) {
                case 'BEE':
                    $item_name .= '<small style="display: block;color: grey;">These items will be sent directly from the supplier to your specified delivery address. Please allow 3-5 working days for delivery. <a target="_blank" href="/delivery/#direct-despatch">Click here</a> for more info.</small>';
                    break;
            }
        }

        if ( get_post_meta( $item->get_order_id(), 'split-order-type', true) == 'BKO' ) {
            $item_name .= '<small style="display: block;color: grey;">More stock is on it’s way! Please allow 5-10 working days for delivery. <a target="_blank" href="/delivery/#backorders">Click here</a> for more info.</small>';
        }
    }

    return $item_name;
}
add_filter( 'woocommerce_order_item_name', 'custom_order_item_notices', 10, 2 );

Upvotes: 2

Related Questions