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 getSyncedInputs = function() {
    return $form.find('.sync-max-to-quantity input');
}

$form.on('found_variation', (event, variation) => {
  var maxQty = variation.max_qty;
  getSyncedInputs()
    .attr('max', maxQty)
    .each(function () {
      var $this = jQuery(this);
      if ($this.val() > maxQty) {
        $this.val(maxQty);
      }
    });
});

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.