【问题标题】:Display calculated total volume in Woocommerce Order received totals在 Woocommerce 订单收到的总数中显示计算的总交易量
【发布时间】:2018-09-27 07:35:27
【问题描述】:

我得到了with some code 的热心帮助,帮助我添加了附加到每个产品的自定义字段的组合值——在这种情况下,整个订单的体积以 m3 为单位。

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

【问题讨论】:

    标签: php wordpress woocommerce custom-fields orders


    【解决方案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 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      相关资源
      最近更新 更多