在 Woocommerce 订单接收总数中显示计算的总量
Display calculated total volume in Woocommerce Order received totals
我得到了 的帮助,帮助我添加了附加到每个产品的自定义字段的组合值 - 在本例中是整个订单的体积,以立方米为单位。
我想在感谢页面的产品列表下方 table 中显示 m3 音量 - 有谁知道我应该使用的挂钩。下面是在购物车和结帐页面上显示我的总数量的代码。
add_action( 'woocommerce_cart_totals_before_shipping',
'display_cart_volume_total', 20 );
add_action( 'woocommerce_review_order_before_shipping',
'display_cart_volume_total', 20 );
function display_cart_volume_total() {
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'],
'_item_volume', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
if( $total_volume > 0 ){
// The Output
echo ' <tr class="cart-total-volume">
<th>' . __( "Total Shipping Volume", "woocommerce" ) . '</th>
<td data-title="total-volume">' . number_format($total_volume, 2) .
' m3</td>
</tr>';
}
}
这是在“已收到订单”中显示总成交量的方法(谢谢) 和“查看订单”(我的帐户) 页面(在前端):
// Front: Display Total Volume in "Order received" (thankyou) and "View Order" (my account) pages
add_action( 'woocommerce_order_items_table', 'display_order_volume_total', 20 );
function display_order_volume_total() {
global $wp;
// Only in thankyou "Order-received" and My account "Order view" pages
if( is_wc_endpoint_url( 'order-received' ))
$endpoint = 'order-received';
elseif( is_wc_endpoint_url( 'view-order' ))
$endpoint = 'view-order';
else
return; // Exit
$order_id = absint( $wp->query_vars[$endpoint] ); // Get Order ID
$order_id > 0 ? $order = wc_get_order($order_id) : exit(); // Get the WC_Order object
$total_volume = 0;
echo '</tbody><tbody>';
// Loop through cart items and calculate total volume
foreach( $order->get_items() as $item ){
$product_volume = (float) get_post_meta( $item->get_product_id(), '_item_volume', true );
$total_volume += $product_volume * $item->get_quantity();
}
if( $total_volume > 0 ){
// The Output
echo '<tr>
<th scope="row">' . __( "Total Shipping Volume", "woocommerce" ) . '</th>
<td data-title="total-volume">' . number_format($total_volume, 2) . ' m3</td>
</tr>';
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
我得到了
我想在感谢页面的产品列表下方 table 中显示 m3 音量 - 有谁知道我应该使用的挂钩。下面是在购物车和结帐页面上显示我的总数量的代码。
add_action( 'woocommerce_cart_totals_before_shipping',
'display_cart_volume_total', 20 );
add_action( 'woocommerce_review_order_before_shipping',
'display_cart_volume_total', 20 );
function display_cart_volume_total() {
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'],
'_item_volume', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
if( $total_volume > 0 ){
// The Output
echo ' <tr class="cart-total-volume">
<th>' . __( "Total Shipping Volume", "woocommerce" ) . '</th>
<td data-title="total-volume">' . number_format($total_volume, 2) .
' m3</td>
</tr>';
}
}
这是在“已收到订单”中显示总成交量的方法(谢谢) 和“查看订单”(我的帐户) 页面(在前端):
// Front: Display Total Volume in "Order received" (thankyou) and "View Order" (my account) pages
add_action( 'woocommerce_order_items_table', 'display_order_volume_total', 20 );
function display_order_volume_total() {
global $wp;
// Only in thankyou "Order-received" and My account "Order view" pages
if( is_wc_endpoint_url( 'order-received' ))
$endpoint = 'order-received';
elseif( is_wc_endpoint_url( 'view-order' ))
$endpoint = 'view-order';
else
return; // Exit
$order_id = absint( $wp->query_vars[$endpoint] ); // Get Order ID
$order_id > 0 ? $order = wc_get_order($order_id) : exit(); // Get the WC_Order object
$total_volume = 0;
echo '</tbody><tbody>';
// Loop through cart items and calculate total volume
foreach( $order->get_items() as $item ){
$product_volume = (float) get_post_meta( $item->get_product_id(), '_item_volume', true );
$total_volume += $product_volume * $item->get_quantity();
}
if( $total_volume > 0 ){
// The Output
echo '<tr>
<th scope="row">' . __( "Total Shipping Volume", "woocommerce" ) . '</th>
<td data-title="total-volume">' . number_format($total_volume, 2) . ' m3</td>
</tr>';
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。