How to Add Decimal Quantity to WooCommerce

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;
}

// Add step value to the quantity field

add_filter('woocommerce_quantity_input_step', 'allowDecimal');
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;
}

Read Also: Best Free Plugins for WooCommerce

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!