Display custom checkout field value in WooCommerce orders and email notifications with 100% discount coupon

I have the following code which adds a custom field to checkout for a specific product, adds it to the new order page, send’s it in the new order email and shows on the thank you for your order page.

// Utility function: Delivery partner options (array)
function get_delivery_partner_options() {
    return array(
        ''  => __('---Please select your Delivery Partner', 'woocommerce'),
        '1' => __('Learners University College (LUC), Dubai', 'woocommerce' ),
        '2' => __('Elite Training Services (ECS), KSA ', 'woocommerce' ),
        '3' => __('MELI, KSA', 'woocommerce' ),
        '4' => __('KLD Management Training, Dubai', 'woocommerce' ),
    );
}

// Display "Delivery Partner" custom checkout field 
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field', 10, 1 );
function my_custom_checkout_field( $checkout ) {
    
    $targeted_product_id = 6245; //Global Membership ID

    foreach( WC()->cart->get_cart() as $item ){
        if ( ! in_array($targeted_product_id, [$item['product_id'], $item['variation_id']]) ) {
            return; // Exit if there are other products in cart than the targeted one.
        }
    }

    echo '<div id="my_custom_checkout_field">
    <h2>' . __('Please select your Delivery Partner', 'woocommerce') . '</h2>';

    woocommerce_form_field( 'delivery_partner', array(
        'type'       => 'select',
        'required'   => 'true',
        'options'    => get_delivery_partner_options(),
        'class'      => array('my-field-class form-row-wide'),
        'label'      => __('Delivery Partner', 'woocommerce'),
    ), $checkout->get_value( 'delivery_partner' ));

    echo '</div>';
}

// Validate delivery partner field
add_action( 'woocommerce_checkout_process', 'custom_checkout_field_validation' );
function custom_checkout_field_validation() {
    $field_key = 'delivery_partner';
    if ( isset($_POST[$field_key]) && empty($_POST[$field_key]) ) {
        wc_add_notice( __('<strong>'.__('Delivery Partner', 'woocommerce').'</strong> is a required field.', 'woocommerce'),'error' );
    }
}

// Save the selected delivery partner as order custom metadata:
add_action( 'woocommerce_checkout_create_order', 'save_selected_delivery_partner' );
function save_selected_delivery_partner( $order ) {
    $field_key = 'delivery_partner';
    $options   = get_delivery_partner_options();
    if ( isset($_POST[$field_key]) && ! empty($_POST[$field_key]) ) {
        $order->update_meta_data( 'delivery_partner', esc_attr($options[$field_key]) );
    } 
}

// Display the custom field value on admin order pages after billing adress:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_selected_delivery_partner_in_admin_orders', 10, 1 );
function display_selected_delivery_partner_in_admin_orders( $order ) {
    if ( $delivery_partner = $order->get_meta('delivery_partner') ) {
        echo '<p><strong>'.__('Delivery Partner', 'woocommerce').':</strong> ' . $delivery_partner . '</p>'; 
    }
}

// Display on customer orders and email notifications
add_filter( 'woocommerce_get_order_item_totals', 'display_delivery_on_order_item_totals', 10, 3 );
function display_delivery_on_order_item_totals( $total_rows, $order, $tax_display ) {
    if ( $delivery_partner = $order->get_meta('delivery_partner') ) {
        $new_total_rows = [];

        // Loop through order total rows
        foreach( $total_rows as $key => $values ) {
            // Inserting before payment method
            if( $key === 'payment_method' ) {
                $new_total_rows['gift_order'] = array(
                    'label' =>__('Delivery Partner', 'woocommerce') . ':',
                    'value' => $delivery_partner,
                );
            }
            $new_total_rows[$key] = $values;
        }
        return $new_total_rows;
    }
    return $total_rows;
}

Everything seems to be working fine apart from: if I use a 100% discounted coupon it doesn’t show up on the thank you for your order screen / email.

Also how can I add multiple product ID’s to have the same checkout field?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật