Add opt-in decimal quantity support - #270
Open
sultann wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Supersedes #261 (closed). Adds optional decimal quantity support to the plugin using WooCommerce's official
woocommerce_stock_amountfilter extension point. Feature is off by default so existing installs are unaffected.How it works
When the "Allow decimal quantities" setting is enabled:
Plugin::bootstrap()removes WC's defaultintvalfilter onwoocommerce_stock_amountand registersfloatvalinstead. WC then funnels every quantity (add-to-cart, cart updates, checkout, order items, emails, REST) through floatval — no$_POSThijacking, no static state, no reentrancy bugs.Cart.phpbranch onwc_is_stock_amount_integer()(WC 10.1+, with afunction_existsfallback that assumes integer mode on older WC).functions.phpcentralize the branching:wcmmq_is_integer_qty(),wcmmq_format_qty(),wcmmq_is_valid_step().step="any"so decimals can be entered when the feature is on.Why not "just widen casts to float"
Two traps the previous PR (#261) fell into:
intvalfilter wins by default.add_filter('woocommerce_stock_amount', 'floatval')alone runs after WC'sintval, which has already truncated the decimal. Data loss. Mustremove_filterfirst.absint(),%(PHP forces int operands), andnumber_format()without precision all silently strip or round decimals. Withstep=0.5andmax_qtyset,absint(0.5)is0, and$max % 0fatal-errors on PHP 8+. These needed direct replacement withwc_is_stock_amount_integer()branches.Pro plugin coordination
Pro plugin PR submitted in parallel: https://github.com/pluginever/wc-min-max-quantities-pro/pull/new/feature/decimal-quantity. These must merge and ship together — the free plugin widens limit storage to float, and Pro's
Cart.phpwas casting back to int via thewc_min_max_quantities_product_limitsfilter. Shipping only one would break the other.Files changed
includes/Plugin.phpremove_filter(intval) + add_filter(floatval)inbootstrap()includes/functions.php(float)includes/Cart.phpabsint()/%/number_format()with helper calls at 20+ sitesincludes/Admin/Settings.phpwcmmq_decimal_quantitiescheckbox + warning copy +step="any"on qty fieldsincludes/Admin/MetaBoxes.phpstep="any"on product step fieldTest plan
Setting OFF (default) — must behave identically to v2.2.9
wc_is_stock_amount_integer()returnstrueSetting ON
inputmodebecomesdecimal(WC handles this automatically)fmod+ epsilon)fmod(3.6, 0.3)case, precision tolerance workswcmmq_format_qty(), shows "2.5"WC version fallback
function_exists('wc_is_stock_amount_integer')to false (simulate WC < 10.1) → helpers fall back to integer mode, no fatalsPro plugin coordination