将订单总重量添加到 WooCommerce 新订单电子邮件通知 [英] Add the order total weight to WooCommerce new order email notification

查看:49
本文介绍了将订单总重量添加到 WooCommerce 新订单电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 WooCommerce新订单"电子邮件通知中显示订单的总重量(针对管理员)?

Is it possible to show the total weight for an order in the WooCommerce "New order" email notification (for admins)?

推荐答案

这是挂在 woocommerce_email_after_order_table 动作钩子中的自定义函数,它将在新订单"电子邮件通知中显示总权重

Here is that custom function hooked in woocommerce_email_after_order_table action hook, that will show on "New order" email notification the total weight

add_action('woocommerce_email_after_order_table','show_total_weight', 10, 4);
function show_total_weight( $order, $sent_to_admin, $plain_text, $email ){

    if ( 'new_order' != $email->id ) return;

    $total_weight = 0;

    foreach( $order->get_items() as $item_id => $product_item ){
        $quantity = $product_item->get_quantity(); // get quantity
        $product = $product_item->get_product(); // get the WC_Product object
        $product_weight = $product->get_weight(); // get the product weight
        // Add the line item weight to the total weight calculation
        $total_weight += floatval( $product_weight * $quantity );
    }

    // Styles
    $style1 = 'style="width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif; color: #737373; border: 1px solid #e4e4e4; border-top:0;"';
    $style2 = '  style="text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;border-top:0;"';
    $style3 = ' style="text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;border-top:0;"';

    // Output
    echo "<table class='td' cellspacing='0' cellpadding='6' $style1><tr><th $style2>" . __( 'Total weight: ', 'woocommerce' ) . "</th><td $style3>" . $total_weight . " kg</td></tr></table>";
}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

这是经过代码测试的,仅适用于 WooCommerce 3+.

This is code tested and works only in WooCommerce 3+.

你会得到(示例截图):

You will get that (example screenshot):

这篇关于将订单总重量添加到 WooCommerce 新订单电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆