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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
<?php
namespace Objectiv\Plugins\Checkout\Model;
class Bump { protected $id; protected $title; protected $display_for; protected $products; protected $categories; protected $any_product = false; protected $location; protected $discount_type; protected $offer_product; protected $offer_discount; protected $offer_language; protected $offer_description; protected $upsell = false;
public function __construct() {}
/** * @param int $id * @throws \Exception */ public function load( int $id ): bool { $bump = get_post( $id );
if ( empty( $bump ) ) { return false; }
$this->id = $id; $this->title = $bump->post_title; $this->display_for = get_post_meta( $this->id, 'cfw_ob_display_for', true ); $this->products = get_post_meta( $this->id, 'cfw_ob_products', true ); $this->categories = get_post_meta( $this->id, 'cfw_ob_categories', true ); $this->any_product = get_post_meta( $this->id, 'cfw_ob_any_product', true ) === 'yes'; $this->location = get_post_meta( $this->id, 'cfw_ob_display_location', true ); $this->discount_type = get_post_meta( $this->id, 'cfw_ob_discount_type', true ); $this->offer_product = get_post_meta( $this->id, 'cfw_ob_offer_product', true ); $this->offer_discount = get_post_meta( $this->id, 'cfw_ob_offer_discount', true ); $this->offer_language = get_post_meta( $this->id, 'cfw_ob_offer_language', true ); $this->offer_description = get_post_meta( $this->id, 'cfw_ob_offer_description', true ); $this->upsell = get_post_meta( $this->id, 'cfw_ob_upsell', true ) === 'yes';
return true; }
/** * @return string */ public function get_title(): string { return $this->title; }
/** * @return bool */ public function is_displayable(): bool { $offer_product = $this->get_offer_product();
// Is the bump in stock? if ( ! $this->can_offer_product_be_added_to_the_cart() ) { return false; }
// Is it in the cart already? if ( $this->quantity_of_product_in_cart( $this->offer_product ) ) { return false; }
// Is it a valid upsell (setup correctly) and are there enough units of the offer product to match the cart product? // TODO: Technically this disallows upsell offer products that are backordered. Bug or feature? YOU DECIDE. if ( $this->is_valid_upsell() && $offer_product->get_manage_stock() && $this->quantity_of_normal_product_in_cart( array_values( $this->products )[0] ) > $offer_product->get_stock_quantity() ) { return false; }
// If by this point we passed all checks and the product is set to display on all products, we can display if ( 'all_products' === $this->display_for ) { return true; }
if ( 'specific_products' === $this->display_for ) { $matching_products_in_cart = 0;
// Count matching products in the cart foreach ( $this->products as $product ) { if ( $this->quantity_of_normal_product_in_cart( (int) $product ) ) { $matching_products_in_cart++; } }
// If all products must match and we have fewer products in the cart than in our matching list, return false if ( ! $this->any_product && count( $this->products ) > $matching_products_in_cart ) { return false; }
// If we get here, matching rule is set to any product, so we can // use the number of matching products to determine if we have a match return boolval( $matching_products_in_cart ); }
if ( 'specific_categories' === $this->display_for ) { // Count matching products in the cart foreach ( $this->categories as $category ) { if ( $this->quantity_of_normal_cart_items_in_category( $category ) ) { return true; } } }
return false; }
/** * @return bool */ public function is_cart_bump_valid(): bool { $offer_product = $this->get_offer_product();
// Is it a valid upsell (setup correctly) and are there enough units of the offer product to match the cart product? if ( $this->is_valid_upsell() && $this->quantity_of_product_in_cart( array_values( $this->products )[0] ) > $offer_product->get_stock_quantity() ) { return false; }
// If the bump is valid for all products, make sure we have at least one other product in the cart if ( 'all_products' === $this->display_for && WC()->cart->get_cart_contents_count() > $this->quantity_of_product_in_cart( $this->offer_product ) ) { return true; }
if ( 'specific_products' === $this->display_for ) { $matching_products_in_cart = 0;
// Count matching products in the cart foreach ( $this->products as $product ) { if ( $this->quantity_of_product_in_cart( (int) $product ) ) { $matching_products_in_cart++; } }
// If all products must match and we have fewer products in the cart than in our matching list, return false if ( ! $this->any_product && count( $this->products ) > $matching_products_in_cart ) { return false; }
// If we get here, matching rule is set to any product, so we can // use the number of matching products to determine if we have a match return boolval( $matching_products_in_cart ); }
if ( 'specific_categories' === $this->display_for ) { // Count matching products in the cart foreach ( $this->categories as $category ) { if ( $this->quantity_of_normal_cart_items_in_category( $category ) ) { return true; } } }
return false; }
/** * @return bool */ public function is_valid_upsell(): bool { return $this->upsell && 'specific_products' === $this->display_for && count( $this->products ) === 1; }
public function can_offer_product_be_added_to_the_cart(): bool { $product = $this->get_offer_product();
return $product && $product->is_purchasable() && ( $product->is_in_stock() || $product->backorders_allowed() ); }
/** * @param int $needle_product_id * @return int */ public function quantity_of_product_in_cart( int $needle_product_id ): int { $needle_product = wc_get_product( $needle_product_id );
if ( ! $needle_product ) { return 0; }
foreach ( WC()->cart->get_cart() as $cart_item ) { $cart_item_variation_id = ! empty( $cart_item['variation_id'] ) ? $cart_item['variation_id'] : 0; $cart_item_parent_id = $cart_item_variation_id ? wp_get_post_parent_id( $cart_item_variation_id ) : 0; $possible_ids = array( $cart_item_parent_id, $cart_item_variation_id, $cart_item['product_id'] ); $in_cart = in_array( $needle_product_id, $possible_ids, true );
if ( $in_cart ) { return $cart_item['quantity']; } }
return 0; }
/** * @param int $needle_product_id * @return int */ public function quantity_of_normal_product_in_cart( int $needle_product_id ): int { $needle_product = wc_get_product( $needle_product_id );
if ( ! $needle_product ) { return 0; }
foreach ( WC()->cart->get_cart() as $cart_item ) { if ( isset( $cart_item['_cfw_order_bump_id'] ) ) { continue; }
$cart_item_variation_id = ! empty( $cart_item['variation_id'] ) ? $cart_item['variation_id'] : 0; $cart_item_parent_id = $cart_item_variation_id ? wp_get_post_parent_id( $cart_item_variation_id ) : 0; $possible_ids = array( $cart_item_parent_id, $cart_item_variation_id, $cart_item['product_id'] ); $in_cart = in_array( $needle_product_id, $possible_ids, true );
if ( $in_cart ) { return $cart_item['quantity']; } }
return 0; }
/** * @param string $needle_category_slug * @return int */ public function quantity_of_normal_cart_items_in_category( string $needle_category_slug ): int { $needle_category = get_term_by( 'slug', $needle_category_slug, 'product_cat' );
if ( ! $needle_category ) { return 0; }
$found = 0;
foreach ( WC()->cart->get_cart() as $cart_item ) { if ( isset( $cart_item['_cfw_order_bump_id'] ) ) { continue; }
$cart_item_terms = wp_get_post_terms( $cart_item['product_id'], 'product_cat' );
/** @var \WP_Term $cart_item_term */ foreach ( $cart_item_terms as $cart_item_term ) { if ( $cart_item_term->slug === $needle_category_slug ) { $found++; } } }
return $found; }
/** * @return int */ public function get_id(): int { return $this->id; }
/** * @return mixed */ public function get_offer_language() { return $this->offer_language; }
/** * @return mixed */ public function get_offer_description() { return $this->offer_description; }
/** * @return false|\WC_Product|null */ public function get_offer_product() { return wc_get_product( $this->offer_product ); }
/** * @return string */ public function get_offer_product_price(): string { $product = $this->get_offer_product(); $price = wc_get_price_to_display( $product, array( 'price' => $product->get_price( 'view' ) ) ); $sale_price = wc_get_price_to_display( $product, array( 'price' => $this->get_offer_product_sale_price() ) ); $sale_price_formatted = wc_price( $sale_price );
if ( $price > $sale_price ) { return wc_format_sale_price( $price, $sale_price ); }
return $sale_price_formatted; }
/** * @return float|int */ public function get_offer_product_sale_price() { $product = $this->get_offer_product();
$discount_type = $this->discount_type; $discount = $this->offer_discount; $price = $product->get_price();
$discount_value = 'percent' === $discount_type ? ( $price / 100 ) * $discount : $discount;
return $price - $discount_value; }
public function get_display_location(): string { return $this->location ?? 'below_cart_items'; }
/** * Get Displayed On Purchases Count * * The number of times this bump was displayed and a purchase was subsequently made. * * @return integer */ private function get_displayed_on_purchases_count(): int { return intval( get_post_meta( $this->id, 'times_bump_displayed_on_purchases', true ) ); }
/** * Get Purchase Count * * The number of times this bump was added to the cart and purchased. * * @return integer */ private function get_purchase_count(): int { return intval( get_post_meta( $this->id, 'times_bump_purchased', true ) ); }
public function increment_displayed_on_purchases_count() { update_post_meta( $this->id, 'times_bump_displayed_on_purchases', $this->get_displayed_on_purchases_count() + 1 ); }
public function increment_purchased_count() { update_post_meta( $this->id, 'times_bump_purchased', $this->get_purchase_count() + 1 ); }
public function update_conversion_rate() { $purchase_count = $this->get_purchase_count(); $displayed_count = $this->get_displayed_on_purchases_count(); $not_calculable = min( $purchase_count, $displayed_count ) < 1;
$value = $not_calculable ? 0 : round( $purchase_count / $displayed_count * 100, 2 );
update_post_meta( $this->id, 'conversion_rate', $value ); }
public function get_conversion_rate() { $value = get_post_meta( $this->id, 'conversion_rate', true );
return '' === $value ? '--' : floatval( $value ) . '%'; }
/** * @return array */ public function get_products(): array { return (array) $this->products; }
/** * @throws \Exception */ static public function get( int $post_id ) { $self = new self();
return $self->load( $post_id ) ? $self : false; }
static public function get_post_type(): string { return 'cfw_order_bumps'; }
static public function init( $parent_menu_slug ) { $post_type = self::get_post_type();
add_action( 'init', function() use ( $post_type, $parent_menu_slug ) { register_post_type( $post_type, array( 'labels' => array( 'name' => cfw__( 'Order Bumps', 'checkout-wc' ), 'singular_name' => cfw__( 'Order Bump', 'checkout-wc' ), 'add_new' => cfw__( 'Add New', 'checkout-wc' ), 'add_new_item' => cfw__( 'Add New Order Bump', 'checkout-wc' ), 'edit_item' => cfw__( 'Edit Order Bump', 'checkout-wc' ), 'new_item' => cfw__( 'New Order Bump', 'checkout-wc' ), 'view_item' => cfw__( 'View Order Bump', 'checkout-wc' ), 'search_items' => cfw__( 'Find Order Bump', 'checkout-wc' ), 'not_found' => cfw__( 'No order bumps were found.', 'checkout-wc' ), 'not_found_in_trash' => cfw__( 'Not found in trash', 'checkout-wc' ), 'menu_name' => cfw__( 'Order Bumps', 'checkout-wc' ), ), 'public' => false, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => $parent_menu_slug, 'query_var' => false, 'rewrite' => false, 'capability_type' => 'post', 'has_archive' => false, 'hierarchical' => false, 'supports' => array( 'title' ), ) ); } );
add_filter( "manage_{$post_type}_posts_columns", function( $columns ) { $date = array_pop( $columns );
$columns['conversion_rate'] = 'Conversion Rate' . wc_help_tip( 'Conversion Rate tracks how often a bump is added to an actual completed purchase. If 20 orders are placed and a bump was displayed on 10 of those orders and the bump was purchased 5 times, the conversion rate is 50%.' ); $columns['date'] = $date;
return $columns; } );
add_action( "manage_{$post_type}_posts_custom_column", function( $column, $post_id ) { if ( 'conversion_rate' === $column ) { $bump = self::get( $post_id ); echo $bump->get_conversion_rate(); } return; }, 10, 2 ); }
/** * @return Bump[] * @throws \Exception */ static public function get_all(): array { $posts = get_posts( array( 'post_type' => self::get_post_type(), 'numberposts' => -1, 'suppress_filters' => true, ) );
$bumps = array();
foreach ( $posts as $post ) { $bumps[] = self::get( $post->ID ); }
return array_filter( $bumps ); } }
|