SHARE
TWEET

Gravity forms - better inventory

a guest Oct 27th, 2012 201 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: My required plugins
  4. Plugin URI: http://mysite.com/
  5. Description: functions that I want active on all themes
  6. Author: My name
  7. Version: 1.0.0
  8. Author URI: http://mysite.com
  9. */
  10.  
  11. /**
  12. * Better Inventory with Gravity Forms / Limit by Sum of Field Values
  13. * http://gravitywiz.com/2012/05/19/simple-ticket-inventory-with-gravity-forms/
  14. */
  15.  
  16. class GWLimitBySum {
  17.    
  18.     private $_args;
  19.    
  20.     function __construct($args) {
  21.        
  22.         $this->_args = wp_parse_args($args, array(
  23.             'form_id' => false,
  24.             'field_id' => false,
  25.             'limit' => 20,
  26.             'limit_message' => __('Sorry, this item is sold out.'),
  27.             'validation_message' => __('You ordered %1$s of this item. There are only %2$s of this item left.'),
  28.             'approved_payments_only' => false,
  29.             'hide_form' => false
  30.             ));
  31.        
  32.         $this->_args['input_id'] = $this->_args['field_id'];
  33.         extract($this->_args);
  34.        
  35.         add_filter("gform_pre_render_$form_id", array(&$this, 'limit_by_field_values'));
  36.         add_filter("gform_validation_$form_id", array(&$this, 'limit_by_field_values_validation'));
  37.        
  38.         if($approved_payments_only)
  39.             add_filter('gwlimitbysum_query', array(&$this, 'limit_by_approved_only'));
  40.        
  41.     }
  42.    
  43.     public function limit_by_field_values($form) {
  44.        
  45.         $sum = self::get_field_values_sum($form['id'], $this->_args['input_id']);
  46.         if($sum < $this->_args['limit'])
  47.             return $form;
  48.        
  49.         if($this->_args['hide_form']) {
  50.             add_filter('gform_get_form_filter', create_function('', 'return "' . $this->_args['limit_message'] . '";'));
  51.         } else {
  52.             add_filter('gform_field_input', array(&$this, 'hide_field'), 10, 2);
  53.         }
  54.          
  55.         return $form;
  56.     }
  57.  
  58.     public function limit_by_field_values_validation($validation_result) {
  59.        
  60.         extract($this->_args);
  61.        
  62.         $form = $validation_result['form'];
  63.         $exceeded_limit = false;
  64.        
  65.         foreach($form['fields'] as &$field) {
  66.            
  67.             if($field['id'] != intval($input_id))
  68.                 continue;
  69.            
  70.             $requested_value = rgpost("input_" . str_replace('.', '_', $input_id));
  71.             $field_sum = self::get_field_values_sum($form['id'], $input_id);
  72.            
  73.             if($field_sum + $requested_value <= $limit)
  74.                 continue;
  75.                
  76.             $exceeded_limit = true;
  77.             $number_left = $limit - $field_sum >= 0 ? $limit - $field_sum : 0;
  78.            
  79.             $field['failed_validation'] = true;
  80.             $field['validation_message'] = sprintf($validation_message, $requested_value, $number_left);
  81.            
  82.         }
  83.        
  84.         $validation_result['form'] = $form;
  85.         $validation_result['is_valid'] = !$validation_result['is_valid'] ? false : !$exceeded_limit;
  86.        
  87.         return $validation_result;
  88.     }
  89.  
  90.     public function hide_field($field_content, $field) {
  91.        
  92.         if($field['id'] == intval($this->_args['input_id']))
  93.             return "<div class=\"ginput_container\">{$this->_args['limit_message']}</div>";
  94.        
  95.         return $field_content;
  96.     }
  97.    
  98.     public static function get_field_values_sum($form_id, $input_id) {
  99.         global $wpdb;
  100.        
  101.         $query = apply_filters('gwlimitbysum_query', array(
  102.             'select' => 'SELECT sum(value)',
  103.             'from' => "FROM {$wpdb->prefix}rg_lead_detail ld",
  104.             'where' => $wpdb->prepare("WHERE ld.form_id = %d AND CAST(ld.field_number as unsigned) = %d", $form_id, $input_id)
  105.             ));
  106.        
  107.         $sql = implode(' ', $query);
  108.         return $wpdb->get_var($sql);
  109.     }
  110.    
  111.     public static function limit_by_approved_only($query) {
  112.         $query['from'] .= ' INNER JOIN wp_rg_lead l ON l.id = ld.lead_id';
  113.         $query['where'] .= ' AND l.payment_status = \'Approved\'';
  114.         return $query;
  115.     }
  116.    
  117. }
  118.  
  119. new GWLimitBySum(array(
  120.     'form_id' => 113,
  121.     'field_id' => 13.3,
  122.     'limit' => 8,
  123.     'limit_message' => 'Sorry, there are no more tickets!',
  124.     'validation_message' => 'You ordered %1$s tickets. There are only %2$s tickets left.',
  125.     'approved_payments_only' => false,
  126.     'hide_form' => false
  127.     ));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top