From: Anthony Talarico Date: Tue, 25 Apr 2017 14:21:36 +0000 (-0400) Subject: adding new calculations for every 4 posters purchased for the poster order form X-Git-Tag: v1.0.13^2 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=9e7bb7b4b12dbfe758772efb79c39c5106ca2e50;p=WP-Themes%2Fmackinacbridge.git adding new calculations for every 4 posters purchased for the poster order form 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 --- diff --git a/functions.php b/functions.php index 7263e88..04cc3e5 100644 --- a/functions.php +++ b/functions.php @@ -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 diff --git a/js/app.js b/js/app.js index f9f933a..2c2caeb 100644 --- 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; } diff --git a/js/custom/pageSetup.js b/js/custom/pageSetup.js index 7173ad7..5028757 100644 --- a/js/custom/pageSetup.js +++ b/js/custom/pageSetup.js @@ -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; }