-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedd-max-discount.php
More file actions
174 lines (144 loc) · 5.15 KB
/
Copy pathedd-max-discount.php
File metadata and controls
174 lines (144 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Plugin Name: Easy Digital Downloads - Maximum Discount Allowed
* Plugin URI: https://wisdomplugin.com
* Description: Apply maximum discount amount to checkout cart in Easy Digital Downloads.
* Author: Brian Batt
* Author URI: https://wisdomplugin.com
* Version: 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Main class.
*/
final class EDD_Max_Discount {
/**
* The single instance of the class.
*/
protected static $_instance = null;
/**
* Main Instance.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor.
*/
public function __construct() {
$this->edd_hooks();
}
/**
* Hook into actions and filters.
*/
public function edd_hooks() {
// Add settings.
add_action( 'edd_add_discount_form_before_products', array( $this, 'edd_add_discount_form_before_products' ) );
add_action( 'edd_edit_discount_form_before_products', array( $this, 'edd_edit_discount_form_before_products' ), 10, 2 );
// Save settings.
add_filter( 'edd_insert_discount', array( $this, 'edd_save_discount' ) );
add_filter( 'edd_update_discount', array( $this, 'edd_save_discount' ) );
// Hooks.
add_filter( 'edd_get_cart_item_discounted_amount', array( $this, 'edd_get_cart_item_discounted_amount' ), 10, 4 );
add_filter( 'edd_get_cart_discount_html', array( $this, 'edd_get_cart_discount_html' ), 10, 4 );
}
/**
* Add maximum discount settings.
*/
public function edd_add_discount_form_before_products() {
?>
<tr>
<th scope="row" valign="top">
<label for="edd-max-discount-amount"><?php _e( 'Maximum discount allowed', 'easy-digital-downloads' ); ?></label>
</th>
<td>
<input type="number" min="0" step="1" max="1000" id="edd-max-discount-amount" name="max_discount_amount" value="" />
<p class="description"><?php _e( 'This is the maximum allowed discount for this code. Enter a flat number between 1 and 1000.', 'easy-digital-downloads' ); ?></p>
</td>
</tr>
<?php
}
/**
* Add max amount setting to edit discount screen.
*/
public function edd_edit_discount_form_before_products( $discount_id, $discount ) {
$max_discount = get_post_meta( $discount_id, '_edd_discount_max_discount', true );
?>
<tr>
<th scope="row" valign="top">
<label for="edd-max-discount-amount"><?php _e( 'Maximum discount allowed', 'easy-digital-downloads' ); ?></label>
</th>
<td>
<input type="number" min="0" step="1" max="1000" id="edd-max-discount-amount" name="max_discount_amount" value="<?php echo esc_attr( $max_discount ); ?>" style="width: 80px;"/>
<p class="description"><?php _e( 'This is the maximum allowed discount for this code. Enter a flat number between 1 and 1000.', 'easy-digital-downloads' ); ?></p>
</td>
</tr>
<?php
}
/**
* Allow EDD to save the maximum discount setting.
*/
public function edd_save_discount( $meta ) {
// Sanitized input - security standards.
$max_discount = isset( $_POST[ 'max_discount_amount' ] ) ? sanitize_text_field( $_POST[ 'max_discount_amount' ] ) : '';
$meta[ 'max_discount' ] = $max_discount;
return $meta;
}
/**
* Get max discounted amount by checking discounts.
*/
public function edd_get_cart_item_discounted_amount( $discounted_price, $discounts, $item, $price ) {
// Loop through discounts.
if ( $discounts ) {
foreach( $discounts as $discount_code ) {
// Get discount meta by code.
$discount = edd_get_discount_by_code( $discount_code );
if ( empty( $discount ) ) {
continue;
}
// If the discount is larger than max discount. Apply max discount amount instead.
$max_amount = get_post_meta( $discount->ID, '_edd_discount_max_discount', true );
if ( $max_amount ) {
$difference = $price - $discounted_price;
if ( $difference > $max_amount ) {
$discounted_price = $price - $max_amount;
}
}
}
}
return $discounted_price;
}
/**
* Change the HTML for max discount - remove % and add a flat discount.
*/
public function edd_get_cart_discount_html( $discount_html, $discount, $rate, $remove_url ) {
$thediscount = edd_get_discount_by_code( $discount );
if ( empty( $thediscount ) ) {
return $discount_html;
}
// Get the max discount amount if available.
$max_amount = get_post_meta( $thediscount->ID, '_edd_discount_max_discount', true );
if ( $max_amount ) {
$rate = edd_currency_filter( edd_format_amount( edd_get_cart_discounted_amount() ) );
$discount_html = '';
$discount_html .= "<span class=\"edd_discount\">\n";
$discount_html .= "<span class=\"edd_discount_rate\">$discount – $rate</span>\n";
$discount_html .= "<a href=\"$remove_url\" data-code=\"$discount\" class=\"edd_discount_remove\"></a>\n";
$discount_html .= "</span>\n";
}
return $discount_html;
}
}
/**
* Main instance.
*/
function edd_max_discount() {
return EDD_Max_Discount::instance();
}
// Global for backwards compatibility.
$GLOBALS[ 'edd_max_discount' ] = edd_max_discount();