-
Notifications
You must be signed in to change notification settings - Fork 0
/
edd-prevent-checkout.php
194 lines (156 loc) · 4.47 KB
/
edd-prevent-checkout.php
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/*
Plugin Name: Easy Digital Downloads - Prevent Checkout
Plugin URI: https://easydigitaldownloads.com/downloads/edd-prevent-checkout/
Description: Prevents customers from being able to checkout until a minimum cart total is reached
Version: 1.0.1
Author: Easy Digital Downloads
Author URI: https://easydigitaldownloads.com/
License: GPL-2.0+
License URI: http://www.opensource.org/licenses/gpl-license.php
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'EDD_Prevent_Checkout' ) ) {
class EDD_Prevent_Checkout {
private static $instance;
/**
* Main Instance
*
* Ensures that only one instance exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 1.0
*
*/
public static function instance() {
if ( ! isset ( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Start your engines
*
* @since 1.0
*
* @return void
*/
public function __construct() {
$this->setup_actions();
}
/**
* Setup the default hooks and actions
*
* @since 1.0
*
* @return void
*/
private function setup_actions() {
// text domain
add_action( 'init', array( $this, 'textdomain' ) );
// show error before purchase form
add_action( 'edd_before_purchase_form', array( $this, 'set_error' ) );
// prevent form from being loaded
add_filter( 'edd_can_checkout', array( $this, 'can_checkout' ) );
// add settings
add_filter( 'edd_settings_extensions', array( $this, 'settings' ) );
// sanitize settings
add_filter( 'edd_settings_extensions_sanitize', array( $this, 'sanitize_settings' ) );
do_action( 'edd_pc_setup_actions' );
}
/**
* Internationalization
*
* @since 1.0
*/
function textdomain() {
load_plugin_textdomain( 'edd-prevent-checkout', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Minimum cart amount required to checkout
*
* @since 1.0
*/
function minimum_cart_amount_required() {
global $edd_options;
$minimum_cart_amount = isset( $edd_options['edd_pc_minimum_cart_total'] ) ? $edd_options['edd_pc_minimum_cart_total'] : '';
return $minimum_cart_amount;
}
/**
* Set error message
*
* @since 1.0
*/
function set_error() {
$cart_amount = edd_get_cart_total();
$formatted_minimum_cart_amount = edd_currency_filter( edd_format_amount( $this->minimum_cart_amount_required() ) );
if ( $cart_amount < $this->minimum_cart_amount_required() ) {
edd_set_error( 'total_not_reached', apply_filters( 'edd_pc_error_message', sprintf( __( 'The cart\'s total must be at least %s to complete this purchase', 'edd-prevent-checkout' ), $formatted_minimum_cart_amount ) ) );
}
else {
edd_unset_error( 'total_not_reached' );
}
edd_print_errors();
}
/**
* Can checkout?
* Prevents the form from being displayed at all until the minimum cart total has been reached
*
* @since 1.0
*/
function can_checkout( $can_checkout ) {
$cart_amount = edd_get_cart_total();
// if the cart amount is less than the minimum cart amount required we don't let the customer check out
if ( $cart_amount < $this->minimum_cart_amount_required() ) {
$can_checkout = false;
return $can_checkout;
}
return $can_checkout;
}
/**
* Settings
*
* @since 1.0
*/
function settings( $settings ) {
$edd_pc_settings = array(
array(
'id' => 'edd_pc_header',
'name' => '<strong>' . __( 'Prevent Checkout', 'edd-prevent-checkout' ) . '</strong>',
'type' => 'header'
),
array(
'id' => 'edd_pc_minimum_cart_total',
'name' => __( 'Minimum Cart Total', 'edd-prevent-checkout' ),
'desc' => __( 'The minimum cart total before checkout is allowed. eg 10, or 10.00', 'edd-prevent-checkout' ),
'type' => 'text',
'std' => ''
),
);
return array_merge( $settings, $edd_pc_settings );
}
/**
* Sanitize settings
*
* @since 1.0
*/
function sanitize_settings( $input ) {
// only allow number, eg 10 or 10.00
$input['edd_pc_minimum_cart_total'] = is_numeric( $input['edd_pc_minimum_cart_total'] ) ? $input['edd_pc_minimum_cart_total'] : '';
return $input;
}
}
}
/**
* Get everything running
*
* @since 1.0
*
* @access private
* @return void
*/
function edd_prevent_checkout_load() {
$edd_prevent_checkout = new EDD_Prevent_Checkout();
}
add_action( 'plugins_loaded', 'edd_prevent_checkout_load' );