我用的是 ¨将订单总重量添加到WooCommerce的新订单邮件通知中。¨ 在我的function.php中添加了显示邮件底部权重的回答代码。
代码工作正常。但我需要的转换是在吨 而不是磅。
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 );
}
$total_weight = $total_weight * 0.0005;
// 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>";
}