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

3
在WooCommerce“新订单”电子邮件通知(管理员使用)中显示订单的总重量是否可能?
1个回答

7

下面是钩入到 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+中经过测试并且有效。

您将获得以下内容(示例截图):

enter image description here


有人能告诉我如何更改这个设置,使得邮件发送给客户时包含重量信息,而不是发送给管理员的邮件,并且使用购物车重量而不是产品重量?我可以使用 $woocommerce->cart->cart_contents_weight . get_option('woocommerce_weight_unit') 吗? - Lyall
@Lyall 将 if ( 'new_order' != $email->id ) return; 更改为 if ( 'new_order' == $email->id ) return; - LoicTheAztec
1
谢谢 - 我刚刚发现了你关于查找电子邮件ID并更改它的另一个答案,这让我成功了,我需要使用customer_processing_order来满足我的需求。再次感谢! - Lyall
请问您能告诉我如何更改以使用$woocommerce->cart->cart_total重量(或$woocommerce->cart->cart_contents_weight),而不是存储的产品重量吗?谢谢。 - Lyall
2
global $woocommerce$woocommerce->cart 已经过时了... 请使用 WC()->cart->get_cart_contents_weight()... 可以在 WC_Cart 类可用方法 中查看。 - LoicTheAztec

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接