繁体   English   中英

将订单总重量添加到WooCommerce新订单电子邮件通知中

[英]Add the order total weight to WooCommerce new order email notification

是否可以在WooCommerce“新订单”电子邮件通知中显示订单的总重量(适用于管理员)?

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

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+中有效。

您将得到(示例屏幕截图):

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM