The case for decimal quantities #
By default, WooCommerce only allow whole numbers for the quantity fields. However, the use of decimal quantities in WooCommerce can be beneficial in various scenarios:
- Products Sold by Weight or Length: If you sell products that are measured by weight or length, such as fruits, vegetables, fabric, or liquids, decimal quantities allow customers to purchase precise amounts. For example, a customer may want to buy 0.5 kg of apples or 1.75 meters of fabric. Enabling decimal quantities allows for accurate purchasing based on weight or length.
- Flexible Measurement Units: Decimal quantities are useful for products that are measured using non-integer units, such as liquids, powders, or granules. For example, if you sell spices by weight or liquids by volume, decimal quantities allow customers to purchase precise amounts, such as 0.5 kg of spices or 1.75 liters of liquid.
- International Units: Decimal quantities are commonly used for products with international measurement units. For example, if you sell products in a country that uses decimal-based units, such as kilograms, liters, or meters, decimal quantities allow for accurate and precise purchasing based on local measurement standards.
- Customizable Products: Decimal quantities can be helpful for products that customers can customize or configure. For instance, if you offer products with multiple options or variations, such as custom fabric by the yard or custom paint by the liter, decimal quantities allow customers to specify exact measurements for their customized products.
- User Convenience: Enabling decimal quantities can enhance the user experience for customers. It allows them to purchase products in the exact quantity they need, without being limited to whole numbers. This can be particularly useful for customers who require precise quantities for their specific needs.
Enabling decimal quantities for product stock #
WooCommerce does not have a built-in setting to enable decimal quantities. However, you can achieve this functionality through the use of third-party plugins or custom code.
Here are some possible ways to enable decimal quantities for products in WooCommerce:
- Use a WooCommerce plugin: There are several third-party plugins available in the WooCommerce plugin marketplace that add decimal quantity functionality to your store. You can search for plugins using keywords such as “decimal quantities” or “decimal quantities for WooCommerce” to find suitable options.
- Implement custom code: If you are comfortable with coding, you can implement custom code to enable decimal quantities for products in WooCommerce. Please note that custom code implementation requires careful consideration and testing to ensure compatibility with your theme and other plugins, and it’s recommended to backup your site before making any changes.
It’s important to note that enabling decimal quantities may have implications for inventory management, shipping calculations, and other aspects of your WooCommerce store. Therefore, thorough testing and consideration of the implications of decimal quantities is recommended before implementing this functionality on a live store.
Enabling decimal quantities with Fluid Checkout PRO optimized cart and checkout pages #
Once you have enabled decimal quantities on WooCommerce, you might need to configure Fluid Checkout PRO to allow decimal quantities on the optimized cart page and on the checkout page if the feature to allow customers to edit cart quantities at checkout is enabled.
To configure decimal quantities for the cart quantities on Fluid Checkout PRO, you need to add the following code snippets to your website, adapting it to the correct number steps for the increase “+” and decrease “-” buttons and the number of decimal places you use for quantities on your website.
If you are unsure about how to add the code snippet to your website, check our article:
How to safely add code snippets to your WooCommerce website
Change number of decimal places for quantity fields
/**
* Change quantity fields expected decimal places for the number spinner script.
*/
function fluidcheckout_change_cart_items_quantity_field_decimal_places( $decimal_places, $cart_item, $cart_item_key, $product ) {
return 1;
}
add_filter( 'fc_pro_cart_items_quantity_decimal_places', 'fluidcheckout_change_cart_items_quantity_field_decimal_places', 300, 4 );
Change quantity fields increase/decrease step
This code snippet will only change the quantity step attribute on the cart and checkout pages. You might not need to use if your custom code or the plugin you use for enabling decimal quantities already sets this attribute for other pages on your website.
/**
* Change quantity fields increase/decrease step amount on cart and checkout pages.
*/
function fluidcheckout_maybe_change_cart_items_quantity_step( $step, $product ) {
// Bail if not on cart or checkout pages
if ( ! class_exists( 'FluidCheckout_Steps' ) || ( ! FluidCheckout_Steps::instance()->is_cart_page_or_fragment() && ! FluidCheckout_Steps::instance()->is_checkout_page_or_fragment() ) ) { return $step; }
return .5;
}
add_filter( 'woocommerce_quantity_input_step', 'fluidcheckout_maybe_change_cart_items_quantity_step', 300, 2 );
Allow decimal values for stock quantities
Your custom code or the plugin you use for enabling decimal quantites probably already replaces the stock quantity filter hooks to use a data type that allows decimal values, but here is the code snippet to do that, for the sake of completeness:
/*
* Replace conversion of stock quantity from integer to float values
*/
remove_filter( 'woocommerce_stock_amount', 'intval', 10 );
add_filter( 'woocommerce_stock_amount', 'floatval', 10 );