在特定的 WooCommerce 电子邮件通知上显示自定义字段

Display custom fields on specific WooCommerce email notification

在我基于 WooCommerce 的网站上,我最近添加了一些代码以在“编辑订单”页面上显示每个订单的运输方式和价格。现在,我想尝试将这些相同的字段添加到发送给管理员的“新订单”电子邮件模板。这是我到目前为止得到的:

// 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 );

除此之外,这就是我一直在尝试的工作,但到目前为止运气不佳:

// 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>';
        }
    }
}

例如,请改用以下内容:

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>';
            }
        }
    }
}

应该可以。