Sync Field Max to WooCommerce Quantity

Sync a field’s max to the max of the WooCommerce product’s quantity input. Works with Variable Products.

Instructions

  1. Install our free Gravity Forms Code Chest plugin.
     Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/
    
    1. Copy and paste the snippet into the “JavaScript” setting under the Code Chest form settings subview.
    2. Add “sync-max-to-quantity” to the Custom CSS Class setting of the fields you want to sync the max.

Code

Filename: gspc-sync-field-max-to-wc-quantity.js

/**
 * Gravity Shop // Product Configurator // Sync Field Max to WooCommerce Quantity
 *
 * Sync a field's max to the max of the WooCommerce product's quantity input. Works with Variable Products.
 *
 * Instructions:
 *     1. Install our free Gravity Forms Code Chest plugin.
 *         Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/
 *     2. Copy and paste the snippet into the "JavaScript" setting under the Code Chest form settings subview.
 *     3. Add "sync-max-to-quantity" to the Custom CSS Class setting of the fields you want to sync the max.
 */
var $form = jQuery('#gform_GFFORMID');
var $quantity = jQuery('.input-text.qty');
var quantityMax = $quantity.attr('max');

var getSyncedInputs = function() {
    return $form.find('.sync-max-to-quantity input');
}

// Create MutationObserver on $quantity's max attribute.
var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        if (mutation.attributeName === 'max') {
            quantityMax = $quantity.attr('max');

            getSyncedInputs()
                .attr('max', quantityMax)
                .each(function() {
                    var $this = jQuery(this);
                    if ($this.val() > quantityMax) {
                        $this.val(quantityMax);
                    }
              });
        }
    });
});

observer.observe($quantity[0], {
    attributes: true,
    attributeFilter: ['max']
});

// Listen to input events on the synced inputs to ensure they don't exceed the max.
getSyncedInputs().on('input', function() {
    var $this = jQuery(this);
    if ($this.val() > quantityMax) {
        $this.val(quantityMax);
    }
});

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.