Add snippet to adjust the quantity input values
This commit is contained in:
parent
c14d6d918e
commit
f7babfbdd2
|
@ -2,9 +2,11 @@
|
|||
|
||||
Various code snippets you can add to your site to enable custom functionality:
|
||||
|
||||
- [Add a message above the login / register form](./before-login--register-form.md)
|
||||
- [Add a currency and symbol](./add-a-currency-symbol.md)
|
||||
- [Change number of related products output](./number-of-products-per-row.md)
|
||||
- [Rename a country](./rename-a-country.md)
|
||||
- [Change a currency symbol](./change-a-currency-symbol.md)
|
||||
- [Unhook and remove WooCommerce emails](./unhook--remove-woocommerce-emails.md)
|
||||
- [Add a message above the login / register form](./before-login--register-form.md)
|
||||
- [Add a currency and symbol](./add-a-currency-symbol.md)
|
||||
- [Change number of related products output](./number-of-products-per-row.md)
|
||||
- [Rename a country](./rename-a-country.md)
|
||||
- [Change a currency symbol](./change-a-currency-symbol.md)
|
||||
- [Unhook and remove WooCommerce emails](./unhook--remove-woocommerce-emails.md)
|
||||
- [Unhook and remove WooCommerce emails](./unhook--remove-woocommerce-emails.md);
|
||||
- [Adjust the quantity input values](./adjust-quantity-input-values.md)
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
# Adjust the quantity input values
|
||||
|
||||
> This is a **Developer level** doc. If you are unfamiliar with code and resolving potential conflicts, select a [WooExpert or Developer](https://woocommerce.com/customizations/) for assistance. We are unable to provide support for customizations under our [Support Policy](http://www.woocommerce.com/support-policy/).
|
||||
|
||||
Set the starting value, maximum value, minimum value, and increment amount for quantity input fields on product pages.
|
||||
|
||||
Add this code to your child theme’s `functions.php` file or via a plugin that allows custom functions to be added, such as the [Code snippets](https://wordpress.org/plugins/code-snippets/) plugin. Avoid adding custom code directly to your parent theme’s `functions.php` file, as this will be wiped entirely when you update the theme.
|
||||
|
||||
```php
|
||||
if ( ! function_exists( 'YOUR_PREFIX_woocommerce_quantity_input_args' ) ) {
|
||||
/**
|
||||
* Adjust the quantity input values for simple products
|
||||
*/
|
||||
function YOUR_PREFIX_woocommerce_quantity_input_args( $args, $product ) {
|
||||
// Only affect the starting value on product pages, not the cart
|
||||
if ( is_singular( 'product' ) ) {
|
||||
$args['input_value'] = 4;
|
||||
}
|
||||
|
||||
$args['max_value'] = 10; // Maximum value
|
||||
$args['min_value'] = 2; // Minimum value
|
||||
$args['step'] = 2; // Quantity steps
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
add_filter( 'woocommerce_quantity_input_args', 'YOUR_PREFIX_woocommerce_quantity_input_args', 10, 2 );
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'YOUR_PREFIX_woocommerce_available_variation' ) ) {
|
||||
/**
|
||||
* Adjust the quantity input values for variations
|
||||
*/
|
||||
function YOUR_PREFIX_woocommerce_available_variation( $args ) {
|
||||
$args['max_qty'] = 20; // Maximum value (variations)
|
||||
$args['min_qty'] = 2; // Minimum value (variations)
|
||||
|
||||
// Note: the starting value and step for variations is controlled
|
||||
// from the 'woocommerce_quantity_input_args' filter shown above for
|
||||
// simple products
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
add_filter( 'woocommerce_available_variation', 'YOUR_PREFIX_woocommerce_available_variation' );
|
||||
}
|
||||
```
|
Loading…
Reference in New Issue