Search code examples
phpwordpresswoocommercemetadataemail-notifications

Display custom fields on specific WooCommerce email notification


On my WooCommerce based site, I recently added some code to display the shipping methods and prices for each order on the "Edit Order" page. Now, I would like to try and add those same fields to the "New Order" email template that gets sent to the admin. This is what I've got so far:

// Capture the available shipping methods, and costs: 
function action_woocommerce_checkout_update_order_meta( $order_id ) {
    // Get shipping packages
    $packages = WC()->shipping()->get_packages();
    
    // Set array
    $rate_labels = array();
    $rate_costs = array();
    
    // Loop through packages
    foreach ( $packages as $key => $package ) {
        // Loop through package rates
        foreach( $package['rates'] as $rate_id => $rate ) {
            // Push to array
            $rate_labels[] = $rate->get_label();
            $rate_costs[] = $rate->get_cost();
        }
    }

    // NOT empty
    if ( ! empty ( $rate_labels ) ) {
        // Update post meta
        update_post_meta( $order_id, '_available_shipping_methods', $rate_labels );
        update_post_meta( $order_id, '_available_shipping_method_cost', $rate_costs );
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 ); 

// Make it display on the edit order page:
function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
    // Get meta
    $rate_labels = $order->get_meta( '_available_shipping_methods' );
    $rate_costs = $order->get_meta( '_available_shipping_method_cost' );
    
    $methods = array ( $rate_labels, $rate_costs );
    
    // True
    if ( $rate_labels ) {
        // Loop
        echo '<p><strong>Shipping Methods: </strong>';
        foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {
             echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';
        }
    }
    
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );

Adding on to that, this is what I have been trying to get working, with no luck so far:

// Add it to the new order email template 
add_filter( 'woocommerce_new_order', 'custom_woocommerce_email_order_meta_fields', 10, 3 );

function custom_woocommerce_email_order_meta_fields( $rate_labels, $sent_to_admin, $order ) {
    
    $rate_labels = $order->get_meta( '_available_shipping_methods' );
    $rate_costs = $order->get_meta( '_available_shipping_method_cost' );
    
    $methods = array ( $rate_labels, $rate_costs );
    
    if ( $rate_labels ) {
        // Loop
        echo '<p><strong>Shipping Methods: </strong>';
        foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {
             echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';
        }
    }
}

Solution

  • Use instead the following for example:

    add_action( 'woocommerce_email_after_order_table', 'wc_email_new_order_custom_meta_data', 10, 4);
    function wc_email_new_order_custom_meta_data( $order, $sent_to_admin, $plain_text, $email ){
        // On "new order" email notifications
        if ( 'new_order' === $email->id ){
            $rate_labels = $order->get_meta( '_available_shipping_methods' );
            $rate_costs = $order->get_meta( '_available_shipping_method_cost' );
            
            $methods = array ( $rate_labels, $rate_costs );
            
            if ( $rate_labels ) {
                // Loop
                echo '<p><strong>Shipping Methods: </strong>';
                foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {
                     echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';
                }
            }
        }
    }
    

    It should work.