| 1 |
<?php |
| 2 |
/** |
| 3 |
* This class is responsible for all discount calculation |
| 4 |
* |
| 5 |
* @package WPFunnels\Discount |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Discount; |
| 8 |
|
| 9 |
class WpfnlDiscount{ |
| 10 |
|
| 11 |
|
| 12 |
/** |
| 13 |
* Calculate price after discount |
| 14 |
* |
| 15 |
* @param $discount_type Discount type can be percentage and flat amount. |
| 16 |
* @param $discount_value Discount amount. |
| 17 |
* @param $price Product price where you want to add discount |
| 18 |
* |
| 19 |
* @since 2.7.6 |
| 20 |
* @return Number |
| 21 |
*/ |
| 22 |
public function calculate_discount( $discount_type, $discount_value, $price ) { |
| 23 |
$custom_price = $price; |
| 24 |
if (!empty($discount_type)) { |
| 25 |
$price = (float)$price; |
| 26 |
$discount_value = (float)$discount_value; |
| 27 |
if( is_numeric($price) && is_numeric($discount_value) ){ |
| 28 |
if ('discount-percentage' === $discount_type) { |
| 29 |
if ( $discount_value > 0 && $discount_value <= 100) { |
| 30 |
$custom_price = $price - (($price * $discount_value) / 100); |
| 31 |
$custom_price = number_format((float)$custom_price,2, '.', ''); |
| 32 |
} |
| 33 |
} elseif ('discount-price' === $discount_type) { |
| 34 |
if ($discount_value > 0 && $price >= $discount_value ) { |
| 35 |
$custom_price = $price - $discount_value; |
| 36 |
$custom_price = number_format($custom_price, 2); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
return $custom_price; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Get discount amount |
| 47 |
* |
| 48 |
* @param $discount_type Discount type can be percentage and flat amount. |
| 49 |
* @param $discount_value Discount amount. |
| 50 |
* @param $product_price Product price where you want to add discount |
| 51 |
* |
| 52 |
* @since 2.7.6 |
| 53 |
* @return Mix Number or Boolean |
| 54 |
*/ |
| 55 |
public static function get_discount_amount( $discount_type, $discount_value, $price ) { |
| 56 |
$discount = false; |
| 57 |
if (!empty($discount_type) && 'original' !== $discount_type ) { |
| 58 |
$price = (float)$price; |
| 59 |
$discount_value = (float)$discount_value; |
| 60 |
if( is_numeric($price) && is_numeric($discount_value) ){ |
| 61 |
if ('discount-percentage' === $discount_type) { |
| 62 |
if ( $discount_value > 0 && $discount_value <= 100) { |
| 63 |
$discount = (($price * $discount_value) / 100); |
| 64 |
} |
| 65 |
} elseif ('discount-price' === $discount_type) { |
| 66 |
if ($discount_value > 0 && $price >= $discount_value ) { |
| 67 |
$discount = $discount_value; |
| 68 |
} |
| 69 |
} |
| 70 |
if( $discount ){ |
| 71 |
$discount = number_format($discount, 2); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
return $discount; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Get discount settings by step id from postmeta |
| 81 |
* |
| 82 |
* @param $step_id Step ID. |
| 83 |
* |
| 84 |
* @since 2.7.6 |
| 85 |
* @return Mix Array or boolean |
| 86 |
*/ |
| 87 |
public function get_discount_settings( $step_id ){ |
| 88 |
$discount = false; |
| 89 |
if( $step_id ){ |
| 90 |
$step_type = get_post_meta( $step_id, '_step_type', true ); |
| 91 |
if( 'checkout' === $step_type ){ |
| 92 |
$discount = get_post_meta( $step_id, '_wpfnl_checkout_discount_main_product', true ); |
| 93 |
}elseif( 'upsell' === $step_type || 'downsell' === $step_type ){ |
| 94 |
$discount = get_post_meta( $step_id, '_wpfnl_'.$step_type.'_discount', true ); |
| 95 |
} |
| 96 |
} |
| 97 |
return $discount; |
| 98 |
} |
| 99 |
} |