Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce Measurement Price Calculator: Redefine the quantity input's pattern to include floating point values to fix the "Please match the requested format" HTML validation error that occurs with the Enfold theme. Please review our guide for adding custom code to WordPress Sites here: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
<?php // only copy this line if needed
/**
* Filter the quantity input's pattern attribute to fix the "Please
* match the requested format" HTML validation error that occurs when using
* WooCommerce Measurement Price Calculator with certain premium themes
*
* @props to mensmaximus {@link https://github.com/mensmaximus} for the
* proposed solution
*
* @param string $pattern the value of the input element's pattern attribute
* @return string the updated pattern to allow floats
*/
function sv_wc_mpc_fix_quantity_input_pattern_premium_themes( $pattern ) {
if ( has_filter( 'woocommerce_stock_amount', 'floatval' ) ) {
$pattern = '^\d*(\.\d*)?$';
}
return $pattern;
}
add_filter( 'woocommerce_quantity_input_pattern', 'sv_wc_mpc_fix_quantity_input_pattern_premium_themes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment