adding new calculations for every 4 posters purchased for the poster order form
authorAnthony Talarico <talarico@gaslightmedia.com>
Tue, 25 Apr 2017 14:21:36 +0000 (10:21 -0400)
committerAnthony Talarico <talarico@gaslightmedia.com>
Tue, 25 Apr 2017 14:21:36 +0000 (10:21 -0400)
using javascript and php to set the gravity pdf form entry to give a $10 discount for every
4 posters purchased from the poster order form in gravity forms

functions.php
js/app.js
js/custom/pageSetup.js

index 7263e88..04cc3e5 100644 (file)
@@ -366,17 +366,33 @@ function change_post_content( $form ) {
             $populated_fields[$key] = array('price'=>$_POST[$key], 'qty'=>$_POST[$value]);
         } 
     }
-
+    // loop through the populated fields to calc the qty
+    $quantity = 0;
     foreach($populated_fields as $price=>$info){
-        if( $info['qty'] > 1 ){
-            $prod_price = $info['qty'] * 10 - 10;
-            $prod_price = $prod_price / $info['qty'];
-            $_POST[$price] = $prod_price;
-            return;
-        } else {
-             $_POST[$price] = '$0.00';
-            return;
-        }
+        $quantity += $info['qty'];
     }
+    
+    // logic to determine the discount rate, every 4 gets $10 off
+    if($quantity % 4 === 0){
+       $discount_qty = $quantity / 4;
+       $discount_price = $discount_qty * 10;
+       $price_total = ($quantity * 10) - $discount_price;
+
+    } else if( $quantity > 4 && $quantity % 4 !== 0){
+       $discount_qty = floor($quantity / 4);
+       $discount_price = $discount_qty * 10;
+       $price_total = ($quantity * 10) - $discount_price;
+     
+    } else if( $quantity < 4){
+       $price_total = $quantity * 10;
+    }
+    
+    $new_price = $price_total / $quantity;
+    
+    // set the newly calulated price for pdf form
+    foreach($populated_fields as $price=>$info){
+        $_POST[$price] = $new_price;
+    }
+    
 }
 ?>
\ No newline at end of file
index f9f933a..2c2caeb 100644 (file)
--- a/js/app.js
+++ b/js/app.js
@@ -77,10 +77,16 @@ $(document).ready(function () {
             }
         }
 
-       if(qty_total > 4){
-           price_total = (qty_total - 4) * 10 + 30;
-       } else if( qty_total === 4){
-           price_total = 30;
+       if(qty_total % 4 === 0){
+           var discount_qty = qty_total / 4;
+           var discount_price = discount_qty * 10;
+           price_total = (qty_total * 10) - discount_price;
+
+       } else if( qty_total > 4 && qty_total % 4 !== 0){
+           var discount_qty = Math.floor(qty_total / 4);
+           var discount_price = discount_qty * 10;
+           price_total = (qty_total * 10) - discount_price;
+     
        } else if( qty_total < 4){
            price_total = qty_total * 10;
        }
index 7173ad7..5028757 100644 (file)
@@ -68,10 +68,16 @@ $(document).ready(function () {
             }
         }
 
-       if(qty_total > 4){
-           price_total = (qty_total - 4) * 10 + 30;
-       } else if( qty_total === 4){
-           price_total = 30;
+       if(qty_total % 4 === 0){
+           var discount_qty = qty_total / 4;
+           var discount_price = discount_qty * 10;
+           price_total = (qty_total * 10) - discount_price;
+
+       } else if( qty_total > 4 && qty_total % 4 !== 0){
+           var discount_qty = Math.floor(qty_total / 4);
+           var discount_price = discount_qty * 10;
+           price_total = (qty_total * 10) - discount_price;
+     
        } else if( qty_total < 4){
            price_total = qty_total * 10;
        }