Skip to content

Instantly share code, notes, and snippets.

@artikus11
Last active January 12, 2022 12:08
Show Gist options
  • Save artikus11/1a69e41b07a6c9bf7df3f8a9120f2c05 to your computer and use it in GitHub Desktop.
Save artikus11/1a69e41b07a6c9bf7df3f8a9120f2c05 to your computer and use it in GitHub Desktop.
Расчет объема заказа в кубометрах
/**
* Расчет объема заказа в кубических метрах
*
* @return float|int
*
* @author Unknown
* @verphp 7.4
*/
public static function get_cart_volume() {
// Инициализайия переменных
$volume = $rate = 0;
// Получение единиц измерения из Woocommerce
$dimension_unit = get_option( 'woocommerce_dimension_unit' );
// Пересчет единиц изменр ия в кубометры
switch ( $dimension_unit ) {
case 'mm':
$rate = 10 ** 9;
break;
case 'cm':
$rate = 10 ** 6;
break;
case 'm':
$rate = 1;
break;
}
if ( $rate === 0 ) {
return 0;
}
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
$qty = $cart_item['quantity'];
$length = $product->get_length() ? : 0;
$width = $product->get_width() ? : 0;
$height = $product->get_height() ? : 0;
// Расчет общего объема заказов
$volume += $length * $width * $height * $qty;
}
return round( (float) $volume / $rate, 4, PHP_ROUND_HALF_UP );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment