wc_get_quantity_input_args()
Get the quantity input args.
Note, when autocomplete is enabled in firefox, it will overwrite actual value with what user entered last. So we default to off. See @link https://github.com/woocommerce/woocommerce/issues/30733.
Hooks from the function
Returns
Array.
Usage
wc_get_quantity_input_args( $args, $product );
- $args(array) (required)
- The arguments.
- $product(WC_Product|null)
- The product.
Default:null
wc_get_quantity_input_args() wc get quantity input args code WC 10.4.3
function wc_get_quantity_input_args( $args, $product = null ) {
// phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment, WooCommerce.Commenting.CommentHooks.HookCommentWrongStyle
$defaults = array(
'input_id' => uniqid( 'quantity_' ),
'input_name' => 'quantity',
'classes' => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),
'pattern' => apply_filters( 'woocommerce_quantity_input_pattern', wc_is_stock_amount_integer() ? '[0-9]*' : '' ),
'inputmode' => apply_filters( 'woocommerce_quantity_input_inputmode', wc_is_stock_amount_integer() ? 'numeric' : 'decimal' ),
'placeholder' => apply_filters( 'woocommerce_quantity_input_placeholder', '', $product ),
'autocomplete' => apply_filters( 'woocommerce_quantity_input_autocomplete', 'off', $product ),
'readonly' => false,
);
if ( $product ) {
$defaults['min_value'] = $product->get_min_purchase_quantity();
$defaults['max_value'] = $product->get_max_purchase_quantity();
$defaults['step'] = $product->get_purchase_quantity_step();
$defaults['product_name'] = $product->get_title();
} else {
$defaults['min_value'] = apply_filters( 'woocommerce_quantity_input_min', 1, $product );
$defaults['max_value'] = apply_filters( 'woocommerce_quantity_input_max', -1, $product );
$defaults['step'] = apply_filters( 'woocommerce_quantity_input_step', 1, $product );
$defaults['product_name'] = '';
}
// phpcs:enable WooCommerce.Commenting.CommentHooks.MissingHookComment, WooCommerce.Commenting.CommentHooks.HookCommentWrongStyle
/**
* Filters all quantity input args.
*
* @since 2.5.0
* @param array $args The arguments.
* @param \WC_Product|null $product The product.
*
* @return array
*/
$args = apply_filters( 'woocommerce_quantity_input_args', wp_parse_args( $args, $defaults ), $product );
// Apply correction to min/max args - min cannot be lower than 0.
$args['min_value'] = max( $args['min_value'], 0 );
$args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : '';
// Max cannot be lower than min if defined.
if ( '' !== $args['max_value'] && $args['max_value'] < $args['min_value'] ) {
$args['max_value'] = $args['min_value'];
}
// Default value should be the min value unless defined.
$args['input_value'] = isset( $args['input_value'] ) ? $args['input_value'] : $defaults['min_value'];
/**
* The input type attribute will generally be 'number' unless the quantity cannot be changed, in which case
* it will be set to 'hidden'. An exception is made for non-hidden readonly inputs: in this case we set the
* type to 'text' (this prevents most browsers from rendering increment/decrement arrows, which are useless
* and/or confusing in this context).
*/
$type = $args['min_value'] > 0 && $args['min_value'] === $args['max_value'] ? 'hidden' : 'number';
$type = $args['readonly'] && 'hidden' !== $type ? 'text' : $type;
/**
* Controls the quantity input's type attribute.
*
* @since 7.4.0
*
* @param string $type A valid input type attribute value, usually 'number' or 'hidden'.
*/
$args['type'] = apply_filters( 'woocommerce_quantity_input_type', $type );
return $args;
}