-
Notifications
You must be signed in to change notification settings - Fork 18
/
quantites-and-units.php
executable file
·342 lines (271 loc) · 9.37 KB
/
quantites-and-units.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/*
Plugin Name: Quantities and Units for WooCommerce
Plugin URI: https://wordpress.org/plugins/quantities-and-units-for-woocommerce/
Description: Easily require your customers to buy a minimum / maximum / incremental amount of products to continue with their checkout.
Version: 1.0.13
Author: Nicholas Verwymeren
Author URI: https://www.nickv.codes
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WC_Quantities_and_Units' ) ) :
class WC_Quantities_and_Units {
public $wc_version;
/**
* @var WooCommerce Supplier instance
* @since 2.1
*/
protected static $_instance = null;
/**
* Main Incremental Product Quantities Instance
*
* @return WooCommerce Supplier - Main instance
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __construct() {
// Activation / Deactivation Hooks
register_activation_hook( __FILE__, array( $this, 'activation_hook' ) );
register_activation_hook( __FILE__, array( $this, 'update_rules_with_roles' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivation_hook' ) );
// Include Required Files
require_once( 'includes/wcqu-functions.php' );
require_once( 'includes/class-wcqu-filters.php' );
require_once( 'includes/class-wcqu-actions.php' );
require_once( 'includes/class-wcqu-product-meta-box.php' );
require_once( 'includes/class-wcqu-post-type.php' );
require_once( 'includes/class-wcqu-validations.php' );
require_once( 'includes/class-wcqu-advanced-rules.php' );
require_once( 'includes/class-wcqu-units-box.php' );
require_once( 'includes/class-wcqu-product-unit.php' );
// Add Scripts and styles
add_action( 'wp_enqueue_scripts', array( $this, 'input_value_validation' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
add_action( 'admin_init', array( $this, 'quantity_styles' ) );
// Set WC Version Number
add_action( 'init', array( $this, 'get_wc_version' ) );
// Control Admin Notices
// add_action( 'admin_notices', array( $this, 'thumbnail_plugin_notice' ) );
add_action( 'admin_init', array( $this, 'thumbnail_plugin_notice_ignore' ) );
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
}
public function plugins_loaded() {
// Remove intval restriction, we'll allow decimals
remove_filter( 'woocommerce_stock_amount', 'intval' );
// allow decimal floats
add_filter( 'woocommerce_stock_amount', 'floatval' );
}
/*
* Adds default option values
*/
public function activation_hook() {
$options = get_option( 'ipq_options' );
$defaults = array (
'ipq_site_rule_active' => '',
'ipq_site_min' => '',
'ipq_site_max' => '',
'ipq_site_step' => '',
'ipq_site_rule_active' => '',
'ipq_show_qty_note' => '',
'ipq_qty_text' => 'Minimum Qty: %MIN%',
'ipq_show_qty_note_pos' => 'below',
'ipq_qty_class' => ''
);
// If no options set the defaults
if ( $options == false ) {
add_option( 'ipq_options', $defaults, '', false );
// Otherwise check that all option are set
} else {
$needs_update = FALSE;
// Check and assign each unset value
foreach ( $defaults as $key => $value ) {
if ( !isset( $options[$key] ) ) {
$options[$key] = $value;
$needs_update = TRUE;
}
}
// If values are missing update the options
if ( $needs_update === TRUE ) {
update_option( 'ipq_options', $options );
}
}
}
/*
* 'Checks' all user roles for pre-2.1 rules
*
* @status To be depricated in version 2.3
*/
public function update_rules_with_roles() {
// Construct default roles list to apply
global $wp_roles;
$roles = $wp_roles->get_names();
$applied_roles = array();
foreach ( $roles as $slug => $name ) {
array_push( $applied_roles, $slug );
}
$args = array (
'posts_per_page' => -1,
'post_type' => 'quantity-rule',
'post_status' => 'publish',
);
$rules = get_posts( $args );
// Loop through rules
foreach ( $rules as $rule ) {
// If their rule value is false, apply all roles
$roles = get_post_meta( $rule->ID, '_roles', true );
if ( $roles == false ) {
update_post_meta( $rule->ID, '_roles', $applied_roles, false );
}
}
}
/*
* Remove thumbnail plugin notice meta value
*/
public function deactivation_hook() {
$args = array(
'meta_key' => 'wpbo_thumbnail_input_notice',
'meta_value' => 'true',
);
$admins = get_users( $args );
foreach ( $admins as $admin ) {
delete_user_meta( $admin->ID, 'wpbo_thumbnail_input_notice' );
}
}
public function enqueue_styles() {
wp_enqueue_style(
'wcqu_quantity_styles',
plugins_url( '/assets/css/styles.css', __FILE__ )
);
}
/*
* Include JS to round any value that isn't a multiple of the
* step up.
*/
public function input_value_validation() {
global $post, $woocommerce;
// Only display script if we are on a single product or cart page
if ( is_object( $post ) and $post->post_type == 'product' or is_cart() ) {
wp_enqueue_script(
'ipq_validation',
plugins_url( '/assets/js/ipq_input_value_validation.js', __FILE__ ),
array( 'jquery' )
);
// Only localize parameters for variable products
if ( ! is_cart() ) {
// Get the product
$pro = get_product( $post );
// Check if variable
if ( $pro->product_type == 'variable' ) {
// See what rules are being applied
$rule_result = wcqu_get_applied_rule( $pro );
// If the rule result is inactive, we're done
if ( $rule_result == 'inactive' or $rule_result == null ) {
return;
// Get values for Override, Sitewide and Rule Controlled Products
} else {
$values = wcqu_get_value_from_rule( 'all', $pro, $rule_result );
}
// Check if the product is out of stock
$stock = $pro->get_stock_quantity();
// Check if the product is under stock management and out of stock
if ( strlen( $stock ) != 0 and $stock <= 0 ) {
if ( $values['min_oos'] != '' ) {
$values['min_value'] = $values['min_oos'];
}
if ( $values['max_oos'] != '' ) {
$values['max_value'] = $values['max_oos'];
}
}
// Output admin-ajax.php URL with sma eprotocol as current page
$params = array (
'min' => $values['min_value'],
'max' => $values['max_value'],
'step' => $values['step']
);
wp_localize_script( 'ipq_validation', 'ipq_validation', $params );
}
}
}
}
/*
* Include Styles
*/
public function quantity_styles() {
if ( is_admin() ) {
wp_enqueue_style(
'wcqu_admin_quantity_styles',
plugins_url( '/assets/css/admin-styles.css', __FILE__ )
);
wp_enqueue_script(
'wcqu_admin_script',
plugins_url( '/assets/js/ipq_admin_script.js', __FILE__ ),
array( 'jquery' )
);
}
}
/*
* Set what version of WooCommerce the user has installed
*/
public function get_wc_version() {
if ( ! function_exists( 'get_plugins' ) )
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$plugin_folder = get_plugins( '/' . 'woocommerce' );
$plugin_file = 'woocommerce.php';
if ( isset( $plugin_folder[$plugin_file]['Version'] ) ) {
$this->wc_version = $plugin_folder[$plugin_file]['Version'];
} else {
$this->wc_version = NULL;
}
}
/*
* General Admin Notice to Encourage users to download thumbnail input as well
*/
public function thumbnail_plugin_notice() {
global $current_user;
$user_id = $current_user->ID;
// Check if Thumbnail Plugin is activated
if ( !in_array( 'woocommerce-thumbnail-input-quantities/woocommerce-thumbnail-input-quantity.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// Check if User has Dismissed this message already
if ( ! get_user_meta( $user_id, 'wpbo_thumbnail_input_notice' ) ) {
echo '<div class="updated">
<p><strong>Notice:</strong> It is highly recommended you install and activate the <a href="http://wordpress.org/plugins/woocommerce-thumbnail-input-quantities/" target="_blank">WooCommerce Thumbnail Input Quantites</a> plugin to display input boxes on products thumbnails. <a href="';
// Echo the current url
echo site_url() . $_SERVER['REQUEST_URI'];
// Echo notice variable as nth get variable with &
if ( strpos( $_SERVER['REQUEST_URI'] , '?' ) !== false ) {
echo '&wpbo_thumbnail_plugin_dismiss=0';
// Echo notice variable as first get variable with ?
} else {
echo '?wpbo_thumbnail_plugin_dismiss=0';
}
echo '">Dismiss Notice</a></p></div>';
}
}
}
/*
* Make Admin Notice Dismissable
*/
public function thumbnail_plugin_notice_ignore() {
global $current_user;
$user_id = $current_user->ID;
if ( isset($_GET['wpbo_thumbnail_plugin_dismiss']) && '0' == $_GET['wpbo_thumbnail_plugin_dismiss'] ) {
add_user_meta($user_id, 'wpbo_thumbnail_input_notice', 'true', true);
}
}
}
endif;
/**
* Returns the main instance of WCS to prevent the need to use globals.
*
* @since 1.0
* @return WCS_Supplier
*/
function wc_quantities_and_units() {
return WC_Quantities_and_Units::instance();
}
// Global for backwards compatibility.
$GLOBALS['WC_Quantities_and_Units'] = wc_quantities_and_units();