<?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);
?>
2条答案
按热度按时间sy5wg1nm1#
这里是一个函数,将自动获得Woocommerce中设置的尺寸单位,并将进行总购物车体积计算:
ijxebb2r2#
你可以试试这样的方法:
参见WC()文件:https://docs.woocommerce.com/document/class-reference/