Insert a custom total row on cart and checkout totals in Woocommerce

后端 未结 1 459
北海茫月
北海茫月 2020-12-19 22:39

In woocommerce, I have successfully managed to add a custom field to a Woocommerce simple product and display that value on the additional info tab on a product page.

<
相关标签:
1条回答
  • 2020-12-19 23:19

    The following will display the total cart volume in cart and checkout page:

    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>';
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    0 讨论(0)
提交回复
热议问题