Display total cart shipping volume value in Woocommerce

前端 未结 2 1681
遥遥无期
遥遥无期 2021-01-15 08:19

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

2条回答
  •  醉梦人生
    2021-01-15 08:45

    You can try something like this:

    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/

提交回复
热议问题