Display total cart shipping volume value in Woocommerce

淺唱寂寞╮ 提交于 2019-12-30 12:59:26

问题


I use woocommerce for wholesale customers who order containers of furniture - normally 40 foot containers with a volume of 68 cubic meters.

Is there a way I can show somewhere on the website - maybe in the header area a box with showing the total m3 of products in their basket? I need to show the client when they reach 68m3 so they know they have filled a container.

And is there a way to flash up a message if a client tries to submit an order less than 68m3 indicating to them that they still have room left in their container?

Any help is appreciated.


回答1:


You can try something like this:

<?php
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    $cart_prods_m3 = array();

        //LOOP ALL THE PRODUCTS IN THE CART
        foreach($items as $item => $values) { 
            $_product =  wc_get_product( $values['data']->get_id());
            //GET GET PRODUCT M3 
            $prod_m3 = $_product->get_length() * 
                       $_product->get_width() * 
                       $_product->get_height();
            //MULTIPLY BY THE CART ITEM QUANTITY
            //DIVIDE BY 1000000 (ONE MILLION) IF ENTERING THE SIZE IN CENTIMETERS
            $prod_m3 = ($prod_m3 * $values['quantity']) / 1000000;
            //PUSH RESULT TO ARRAY
            array_push($cart_prods_m3, $prod_m3);
        } 

    echo "Total of M3 in the cart: " . array_sum($cart_prods_m3);
?>

See WC() docs: https://docs.woocommerce.com/document/class-reference/




回答2:


Here is a function that will get automatically the dimension unit set in Woocommerce and that will do the total cart volume calculation:

function get_cart_volume(){
    // Initializing variables
    $volume = $rate = 0;

    // Get the dimetion unit set in Woocommerce
    $dimension_unit = get_option( 'woocommerce_dimension_unit' );

    // Calculate the rate to be applied for volume in m3
    if ( $dimension_unit == 'mm' ) {
        $rate = pow(10, 9);
    } elseif ( $dimension_unit == 'cm' ) {
        $rate = pow(10, 6);
    } elseif ( $dimension_unit == 'm' ) {
        $rate = 1;
    }

    if( $rate == 0 ) return false; // Exit

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item) { 
        // Get an instance of the WC_Product object and cart quantity
        $product = $cart_item['data'];
        $qty     = $cart_item['quantity'];

        // Get product dimensions  
        $length = $product->get_length();
        $width  = $product->get_width();
        $height = $product->get_height();

        // Calculations a item level
        $volume += $length * $width * $height * $qty;
    } 
    return $volume / $rate;
}

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

Example usage output:

echo __('Cart volume') . ': ' . get_cart_volume() . ' m3';


来源:https://stackoverflow.com/questions/49229642/display-total-cart-shipping-volume-value-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!