PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / includes / core / classes / discount / class-wpfnl-discount.php

class-wpfnl-discount.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at includes/core/classes/discount/class-wpfnl-discount.php

99 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }