Hi,
WooCommerce uses a default stock quantity of 1 as the minimum because it typically deals with whole, discrete products. However, you can change this behavior by using a filter to set the minimum stock quantity to 0.1. Here’s how you can do it:
- Access Your Theme’s
functions.php File:
Log in to your WordPress admin panel, and navigate to “Appearance” > “Theme Editor.” In the theme editor, look for your theme’s functions.php file, typically located under the theme’s root directory.
- Add Custom Code:
Inside the functions.php file, add the following code:
add_filter('woocommerce_product_is_in_stock', 'change_minimum_stock_quantity', 10, 2);
function change_minimum_stock_quantity($is_in_stock, $product) {
// Set your desired minimum stock quantity here (e.g., 0.1).
$minimum_stock_quantity = 0.1;
// Check if the product's stock quantity is less than the minimum.
if ($product->get_stock_quantity() < $minimum_stock_quantity) {
$is_in_stock = true;
}
return $is_in_stock;
}
In this code, the woocommerce_product_is_in_stock filter is used to check if a product is in stock. If a product’s stock quantity is less than your specified $minimum_stock_quantity, it will be considered in stock.
- Save Changes:
After adding the code, save the functions.php file.
- Test:
You can now set your product stock to decimal values like 0.8, and it should be considered in stock if it’s greater than or equal to 0.1.
Please note that modifying your theme files involves working with code, so make sure to create a backup of your website before making any changes. If your theme receives updates, you may need to reapply this change. Consider using a child theme to maintain customizations through theme updates.
Hey @owadud655
I tried out your code but it doesn’t work for some reason. I also tried to turn off all other plugins to see if they are interrupting this code but I still can’t set default minimum stock value to 0.1.
add_filter('woocommerce_get_stock_quantity', 'change_woocommerce_stock_minimum');
function change_woocommerce_stock_minimum($quantity, $product) {
// Change the minimum stock limit to 0.1
return max($quantity, 0.1);
}
Try this one.
This will give me critical error. However I see your idea but I haven’t found the right solution to set min default product quantity to 0.1 instead of 1. @owadud655
Hi @kevinin2d,
I have found-out another way for you.
Use this plugin, it’s support decimal value.
Link: https://wordpress.org/plugins/product-quantity-for-woocommerce/
Let me know if this works for you.
Tried this plugin out. I can purchase decimal products with it but all products under 1 are still out of stock.
Hi @kevinin2d
While you’ve managed to set the step to 0.1 with a plugin, WooCommerce still treats any value less than 1 as out of stock. This is not something that can be changed with a simple setting in WooCommerce.
However, it may be possible to achieve this using a plugin, like Smart Product Quantity or FooSales for WooCommerce plugin. They allow you to sell in decimal quantities.
If you were looking to do it via code, it looks like there’s a solution for that here:
https://stackoverflow.com/questions/27417718/woocommerce-decimal-quantity/33764898
I hope this clears up any confusion. If you have any other questions, feel free to ask!