| 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 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Checks if the discount is time-bound for a specific step. |
| 103 |
* |
| 104 |
* @param int $step_id The ID of the step. |
| 105 |
* @return bool True if the discount is time-bound, false otherwise. |
| 106 |
* @since 3.1.0 |
| 107 |
*/ |
| 108 |
public function maybe_time_bound_discount( $step_id ){ |
| 109 |
if( !$step_id ){ |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
$discount = $this->get_time_bound_discount_settings( $step_id ); |
| 114 |
if( !$discount || !isset( $discount['isEnabled'] ) || 'yes' !== $discount['isEnabled'] ){ |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Retrieves the time-bound discount settings for a specific step. |
| 124 |
* |
| 125 |
* @param int $step_id The ID of the step. |
| 126 |
* @return array The time-bound discount settings for the specified step. |
| 127 |
* |
| 128 |
* @since 3.1.0 |
| 129 |
*/ |
| 130 |
public function get_time_bound_discount_settings( $step_id ){ |
| 131 |
if( !$step_id ){ |
| 132 |
return false; |
| 133 |
} |
| 134 |
return get_post_meta( $step_id, '_wpfnl_time_bound_discount_settings', true ); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Validates the discount time for a given step ID. |
| 140 |
* |
| 141 |
* @param int $step_id The ID of the step to validate the discount time for. |
| 142 |
* @return bool True if the discount is valid, false otherwise. |
| 143 |
* |
| 144 |
* @since 3.1.0 |
| 145 |
*/ |
| 146 |
public function maybe_validate_discount_time( $step_id ){ |
| 147 |
if( !$step_id ){ |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
$discount = $this->get_time_bound_discount_settings( $step_id ); |
| 152 |
if( !$discount || !isset( $discount['isEnabled'] ) || 'yes' !== $discount['isEnabled'] ){ |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
$from_date = date('Y-m-d', strtotime($discount['fromDate'])); |
| 157 |
$to_date = date('Y-m-d', strtotime($discount['toDate'])); |
| 158 |
|
| 159 |
$date_created = new \DateTime(); |
| 160 |
$current_date = $date_created->format( "Y-m-d" ); |
| 161 |
|
| 162 |
if( $current_date >= $from_date && $current_date <= $to_date ){ |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
return false; |
| 167 |
} |
| 168 |
} |