This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
/
wc-mnm-container-step.php
292 lines (232 loc) · 13.2 KB
/
wc-mnm-container-step.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
<?php
/**
* Plugin Name: WooCommerce Mix and Match - Container Step
* Plugin URI: https://github.com/kathyisawesome/wc-mnm-container-step
* Version: 2.0.0
* Description: Require container size to be in quantity mnultiples, ie: 12,16,20,etc.
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com/
* Developer: Kathy Darling
* Developer URI: http://kathyisawesome.com/
* Text Domain: wc-mnm-container-step
* Domain Path: /languages
*
* GitHub Plugin URI: kathyisawesome/wc-mnm-grouped
* GitHub Plugin URI: https://github.com/kathyisawesome/wc-mnm-grouped
* Release Asset: true
*
* Copyright: © 2020 Kathy Darling
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* The Main WC_MNM_Container_Step class
**/
if ( ! class_exists( 'WC_MNM_Container_Step' ) ) :
class WC_MNM_Container_Step {
/**
* constants
*/
const VERSION = '2.0.0';
/**
* WC_MNM_Container_Step Constructor
*
* @access public
* @return WC_MNM_Container_Step
*/
public static function init() {
// Quietly quit if MNM is not active.
if ( ! function_exists( 'wc_mix_and_match' ) ) {
return false;
}
// Load translation files.
add_action( 'init', array( __CLASS__, 'load_plugin_textdomain' ) );
// Add extra meta.
add_action( 'wc_mnm_admin_product_options', array( __CLASS__, 'container_size_options') , 15, 2 );
add_action( 'woocommerce_admin_process_product_object', array( __CLASS__, 'process_meta' ), 20 );
// Register Scripts.
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_scripts' ) );
add_filter( 'wc_mnm_add_to_cart_script_parameters', array( __CLASS__, 'script_parameters' ) );
add_filter( 'wc_mnm_container_data_attributes', array( __CLASS__, 'add_data_attributes' ), 10, 2 );
// Display Scripts.
add_action( 'woocommerce_mix-and-match_add_to_cart', array( __CLASS__, 'load_scripts' ) );
// QuickView support.
add_action( 'wc_quick_view_enqueue_scripts', array( __CLASS__, 'load_scripts' ) );
// Add to cart validation.
add_filter( 'wc_mnm_add_to_cart_container_validation', array( __CLASS__, 'validation' ), 10, 3 );
}
/*-----------------------------------------------------------------------------------*/
/* Localization */
/*-----------------------------------------------------------------------------------*/
/**
* Make the plugin translation ready
*
* @return void
*/
public static function load_plugin_textdomain() {
load_plugin_textdomain( 'wc-mnm-container-step' , false , dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/*-----------------------------------------------------------------------------------*/
/* Admin */
/*-----------------------------------------------------------------------------------*/
/**
* Adds the container max weight option writepanel options.
*
* @param int $post_id
* @param WC_Product_Mix_and_Match $mnm_product_object
*/
public static function container_size_options( $post_id, $mnm_product_object ) {
woocommerce_wp_text_input( array(
'id' => '_mnm_container_step',
'label' => esc_html__( 'Container Size Step', 'wc-mnm-container-step' ),
'desc_tip' => true,
'description' => esc_html__( 'Force customers to purchase quantities in multiples. Ignored if min and max sizes are the same.', 'wc-mnm-container-step' ),
'type' => 'number',
'data_type' => 'decimal',
'value' => $mnm_product_object->get_meta( '_mnm_container_step', true, 'edit' ),
'desc_tip' => true,
) );
}
/**
* Saves the new meta field.
*
* @param WC_Product_Mix_and_Match $mnm_product_object
*/
public static function process_meta( $product ) {
if ( $product->is_type( 'mix-and-match' ) ) {
if( ! empty( $_POST[ '_mnm_container_step' ] ) ) {
$product->update_meta_data( '_mnm_container_step', intval( wc_clean( wp_unslash( $_POST[ '_mnm_container_step' ] ) ) ) );
} else {
$product->delete_meta_data( '_mnm_container_step' );
}
}
}
/*-----------------------------------------------------------------------------------*/
/* Cart Functions */
/*-----------------------------------------------------------------------------------*/
/**
* Server-side validation
*
* @param bool $is_valid
* @param obj WC_Product_Mix_and_Match $product
* @param obj WC_Mix_and_Match_Stock_Manager $mnm_stock
* @return bool
*/
public static function validation( $valid, $product, $mnm_stock ) {
if( $product->get_meta( '_mnm_container_step', true ) && $product->get_min_container_size() !== $product->get_max_container_size() ) {
$total_qty = $mnm_stock->get_total_quantity();
$step = $product->get_meta( '_mnm_container_step', true );
// Validate the step modulus.
if ( 0 !== $total_qty % $step ) {
$error_message = sprintf(
esc_html__( '"%1$s" is incorrectly configured. The total quantity of selected products must be a multiple of %2$d.', 'wc-mnm-container-step' ),
$product->get_title(),
$step
);
wc_add_notice( $error_message, 'error' );
$valid = false;
}
$valid = true;
}
return $valid;
}
/*-----------------------------------------------------------------------------------*/
/* Scripts and Styles */
/*-----------------------------------------------------------------------------------*/
/**
* Script parameters
*
* @param array $params
* @return array
*/
public static function script_parameters( $params ) {
$new_params = array(
// translators : %v is the current quantity message. %min is the script placeholder for min quantity. %max is script placeholder for max quantity.
'i18n_step_min_qty_error_singular' => _x( '%v Please select at least %min item (in multiples of %step) to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the script placeholder for min quantity. %max is script placeholder for max quantity.
'i18n_step_min_qty_error' => _x( '%v Please select at least %min items (in multiples of %step) to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the script placeholder for min quantity. %max is script placeholder for max quantity.
'i18n_step_max_qty_error_singular' => _x( '%v Please select fewer than %max item (in multiples of %step) to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the script placeholder for min quantity. %max is script placeholder for max quantity.
'i18n_step_max_qty_error' => _x( '%v Please select fewer than %max items (in multiples of %step) to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the script placeholder for min quantity. %max is script placeholder for max quantity.
'i18n_step_min_max_qty_error' => esc_html_x( '%v Please select between %min and %max items (in multiples of %step) to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the script placeholder for min quantity. %max is script placeholder for max quantity.
'i18n_step_min_or_max_error' => esc_html_x( '%v Please choose either %min or %max items to continue….', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_step_valid_message' => _x( '%v You may select any multiple of %step items or add to cart to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_step_valid_min_max_message' => _x( '%v You may select between %min and %max items (in multiples of %step) or add to cart to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_step_valid_max_message' => _x( '%v You may select fewer items (in multiples of %step) or add to cart to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_step_valid_max_no_min_message' => _x( '%v You may select up to %max items (in multiples of %step) or add to cart to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_step_valid_min_message' => _x( '%v You may select more items (in multiples of %step) or add to cart to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_step_valid_min_or_max_message' => _x( '%v You may select either %min or %max items, or add to cart to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_edit_step_valid_message' => _x( '%v You may select any multiple of %step items or update to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_edit_step_valid_min_max_message' => _x( '%v You may select between %min and %max items (in multiples of %step) or update to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_edit_step_valid_max_message' => _x( '%v You may select fewer items (in multiples of %step) or update to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_edit_step_valid_max_no_min_message' => _x( '%v You may select up to %max items (in multiples of %step) or update to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_edit_step_valid_min_message' => _x( '%v You may select more items (in multiples of %step) or update to continue…', '[Frontend]', 'wc-mnm-container-step' ),
// translators : %v is the current quantity message. %min is the container minimum. %max is the container maximum.
'i18n_edit_step_valid_min_or_max_message' => _x( '%v You may select either %min or %max items, or update to continue…', '[Frontend]', 'wc-mnm-container-step' ),
);
return array_merge( $params, $new_params );
}
/**
* Register scripts
*
* @return void
*/
public static function register_scripts() {
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_register_script( 'wc-add-to-cart-mnm-step-validation', plugins_url( 'assets/js/frontend/wc-add-to-cart-mnm-step-validation' . $suffix . '.js', __FILE__ ), array( 'wc-add-to-cart-mnm' ), self::VERSION, true );
}
/**
* Product-specific data attributes.
*
* @param array $params
* @param obj WC_Mix_and_Match_Product
* @return array
*/
public static function add_data_attributes( $params, $product ) {
if( self::validate_by_step( $product ) ) {
$new_params = array(
'container_step' => $product->get_meta( '_mnm_container_step', true )
);
$params = array_merge( $params, $new_params );
}
return $params;
}
/**
* Load the script anywhere the MNN add to cart button is displayed
* @return void
*/
public static function load_scripts() {
global $product;
if ( self::validate_by_step( $product ) ) {
wp_enqueue_script( 'wc-add-to-cart-mnm-step-validation' );
}
}
/*-----------------------------------------------------------------------------------*/
/* Helpers */
/*-----------------------------------------------------------------------------------*/
/**
* Does this product validate by weight.
* @param WC_Product
* @return bool
*/
public static function validate_by_step( $product ) {
return $product instanceOf WC_Product_Mix_and_Match && intval( $product->get_meta( '_mnm_container_step', true ) ) > 0 && $product->get_min_container_size() !== $product->get_max_container_size();
}
} //end class: do not remove or there will be no more guacamole for you
endif; // end class_exists check
// Launch the whole plugin.
add_action( 'plugins_loaded', array( 'WC_MNM_Container_Step', 'init' ), 20 );