显示收到的Woocommerce订单总计的计算总量

时间:2018-04-17 10:59:48

标签: php wordpress woocommerce custom-fields orders

我得到了很好的帮助with some code,它有助于添加附加到每个产品的自定义字段的组合值 - 在这种情况下,整个订单的数量以m3为单位。

我想在thankyou页面上的产品列表下方的表格中显示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>';
  }
 }

1 个答案:

答案 0 :(得分:1)

以下是在<收到的订单(谢谢你)和”查看订单中显示总量的方法em>“(我的帐户)页面(在前端):

// 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文件。经过测试和工作。

kqueue()