Today we are going to talk about adding decimal quantity in WooCommerce. You have to paste the following function in your active/child theme’s function.php file.
// Add min value
add_filter('woocommerce_quantity_input_min', 'decimal');
function decimal($val) {
return 0.1;
}
function decimal($val) {
return 0.1;
}
// Add step value to the quantity field
add_filter('woocommerce_quantity_input_step', 'allowDecimal');
function allowDecimal($val) {
return 0.1;
}
function allowDecimal($val) {
return 0.1;
}
// Removes the WooCommerce filter
remove_filter('woocommerce_stock_amount', 'intval');
// Add a filter
add_filter('woocommerce_stock_amount', 'floatval');
// Add unit price fix
add_filter('woocommerce_order_amount_item_total', 'unit_price', 10, 5);
function unit_price($price, $order, $item, $incTax = false, $round = true) {
$qty = (!empty($item['qty']) && $item['qty'] != 0) ? $item['qty'] : 1;
if($incTax) {
$price = ($item['line_total'] + $item['line_tax']) / $qty;
} else {
$price = $item['line_total'] / $qty;
}
$price = $round ? round( $price, 2 ) : $price;
return $price;
}
function unit_price($price, $order, $item, $incTax = false, $round = true) {
$qty = (!empty($item['qty']) && $item['qty'] != 0) ? $item['qty'] : 1;
if($incTax) {
$price = ($item['line_total'] + $item['line_tax']) / $qty;
} else {
$price = $item['line_total'] / $qty;
}
$price = $round ? round( $price, 2 ) : $price;
return $price;
}
Read Also: Best Free Plugins for WooCommerce