php - 以吨为单位的订单总重量转换为 WooCommerce 新订单电子邮件通知
问题描述
我正在使用¨将订单总重量添加到 WooCommerce 新订单电子邮件通知中¨答案代码以在添加到我的 functions.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>";
}
推荐阅读
- html - 将 z-index 应用于图像
- r - 文件中的错误(描述 = xlsxFile):无效的“描述”参数
- python - 将包含多个对象的pandas对象转换为单个pandas数据框的过程是什么
- java - Android滑动删除recyclerView项目
- docker - 如何在 HAproxy 上启用 CORS?
- ruby-on-rails - 日期解析字符串类型日期并查询rails activerecord
- ag-grid - agGrid 我应该如何在 setRowData() 或 applyTransaction() 上处理不同类别的对象?
- heap - 在二进制最大堆中,父节点不是比其子节点大吗?
- laravel - 在 laravel 笔记工作中路由 /author/{username}
- php - Laravel 打印 HTML 字段属性错误