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
|
<?php
namespace Objectiv\Plugins\Checkout\Features; use Objectiv\Plugins\Checkout\Managers\SettingsManager; use Objectiv\Plugins\Checkout\Model\Item; use WC_Frontend_Scripts; use WC_Shipping_Free_Shipping; use WC_Shipping_Zones;
class SideCart extends FeaturesAbstract { protected $itemJustAddedToCart = false;
public function init() { parent::init();
add_action( 'cfw_do_plugin_activation', array( $this, 'run_on_plugin_activation' ) ); }
protected function run_if_cfw_is_enabled() { // Prevent redirecting after add to cart when side cart is on add_filter( 'pre_option_woocommerce_cart_redirect_after_add', function() { return 'no'; } ); add_filter( 'woocommerce_add_to_cart_redirect', '__return_false' );
add_action( 'woocommerce_add_to_cart', array( $this, 'detect_item_just_added_to_cart' ) ); add_action( 'wp', array( $this, 'run_sidecart' ) ); }
function tryToAvoidCompatibilityNightmares() { add_action( 'wp_enqueue_scripts', array( $this, 'make_sure_cart_fragments_script_is_enqueued'), 100 * 1000 ); add_action( 'wp_print_scripts', array( WC_Frontend_Scripts::class, 'localize_printed_scripts' ), 5 ); add_action( 'wp_print_footer_scripts', array( WC_Frontend_Scripts::class, 'localize_printed_scripts' ), 5 ); }
function run_sidecart() { add_filter( 'cfw_breadcrumbs', array( $this, 'remove_cart_breadcrumb' ) ); add_filter( 'cfw_show_return_to_cart_link', '__return_false' );
if ( is_cfw_page() ) { return; }
$this->tryToAvoidCompatibilityNightmares(); add_action( 'cfw_after_side_cart_header', array( $this, 'maybe_output_shipping_progress_bar' ) ); add_action( 'cfw_before_cart_item_subtotal', array( $this, 'add_delete_button' ), 10, 1 ); add_action( 'wp_footer', array( $this, 'output_side_cart_and_overlay_markup' ), 10000 ); add_filter( 'woocommerce_add_to_cart_fragments', array( $this, 'add_side_cart_to_fragments' ) ); add_filter( 'cfw_event_data', array( $this, 'add_localized_settings' ) ); add_action( 'cfw_custom_css_properties', array( $this, 'add_custom_css_property' ) );
// Turn off empty cart notice remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
// Turn off "Item removed. Undo?" notices add_filter( 'woocommerce_cart_item_removed_notice_type', '__return_null' );
add_filter( 'pre_option_woocommerce_enable_ajax_add_to_cart', function( $result ) { if ( SettingsManager::instance()->get_setting( 'enable_side_cart' ) === 'yes' && SettingsManager::instance()->get_setting( 'enable_ajax_add_to_cart' ) === 'yes' ) { $result = 'yes'; }
return $result; }, 10, 1 );
// Output custom styles add_action( 'wp_head', function() { do_action( 'cfw_custom_styles' ); } ); }
function detect_item_just_added_to_cart() { $this->itemJustAddedToCart = true; }
function make_sure_cart_fragments_script_is_enqueued() { WC_Frontend_Scripts::load_scripts(); wp_enqueue_script( 'wc-cart-fragments' ); }
/** * @param Item $item */ function add_delete_button( Item $item ) { if ( defined( 'WOOCOMMERCE_CHECKOUT' ) ) { return; }
$_product = $item->get_product();
echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 'woocommerce_cart_item_remove_link', sprintf( '<a href="%s" class="cfw-remove-item-button" aria-label="%s" data-product_id="%s" data-product_sku="%s">✗</a>', esc_url( wc_get_cart_remove_url( $item->get_item_key() ) ), cfw_esc_html__( 'Remove this item', 'woocommerce' ), esc_attr( $_product->get_id() ), esc_attr( $_product->get_sku() ) ), $item->get_item_key() ); }
function output_side_cart_and_overlay_markup() { ?> <div id="cfw-side-cart-overlay"></div> <div class="checkoutwc" id="cfw-side-cart"><div class="uninitialized" id="cfw-side-cart-inner"></div></div>
<?php if ( SettingsManager::instance()->get_setting( 'enable_floating_cart_button') === 'yes' ): ?> <a class="cfw-side-cart-floating-button cfw-side-cart-open-trigger" aria-label="<?php echo esc_attr( cfw__( 'View cart', 'woocommerce' ) ); ?>"> <?php echo self::get_cart_icon(); ?> </a> <?php endif; ?> <?php }
function get_side_cart_fragment() { ob_start(); ?> <div id="cfw-side-cart-inner"> <div class="cfw-side-cart-contents-header"> <span class="cfw-side-cart-close-btn"> <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M17 8L21 12M21 12L17 16M21 12L3 12" stroke="#111827" stroke-linecap="round" stroke-linejoin="round"/> </svg> </span>
<?php echo self::get_cart_icon(); ?> <?php do_action( 'cfw_after_side_cart_header' ); ?> </div>
<div class="cfw-side-cart-contents"> <form id="cfw-side-cart-form"> <?php if ( WC()->cart->is_empty() ) { do_action( 'woocommerce_cart_is_empty' );
echo '<h2>' . __( 'Your Cart is Empty', 'checkout-wc' ) . '</h2>';
do_action( 'cfw_side_cart_is_empty' ); } else { echo cfw_get_item_summary_table( WC()->cart ); } ?> </form> </div>
<div class="cfw-side-cart-contents-footer"> <?php if ( ! WC()->cart->is_empty() ) : ?> <div class="cfw-side-cart-totals"> <table class="cfw-module"> <tr class="cart-subtotal"> <th><?php cfw_e( 'Subtotal', 'woocommerce' ); ?></th> <td><?php wc_cart_totals_subtotal_html(); ?></td> </tr> </table> </div>
<div class="wc-proceed-to-checkout"> <?php wc_get_pay_buttons(); ?>
<a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="cfw-primary-btn cfw-side-cart-checkout-btn"> <?php cfw_esc_html_e( 'Checkout', 'woocommerce' ); ?> </a>
<a href="#" class="cfw-side-cart-close-trigger cfw-secondary-btn"> <?php cfw_esc_html_e( 'Continue shopping', 'woocommerce' ); ?> </a> </div> <?php else : ?> <div class="wc-proceed-to-checkout"> <?php wc_get_pay_buttons(); ?>
<a href="javascript:" class="cfw-side-cart-close-btn cfw-primary-btn"> <?php cfw_esc_html_e( 'Continue shopping', 'woocommerce' ); ?> </a> </div> <?php endif; ?> </div> </div> <?php return ob_get_clean(); }
/** * @param array|null $data * @return bool */ function does_cart_qualifies_for_free_shipping( array $data = null ): bool { $data = $data ?? $this->get_free_shipping_data();
if ( empty( $data ) ) { return false; }
return (bool) $data['has_free_shipping']; }
/** * @param array|null $data * @return int|null */ function get_remaining_amount_to_qualify_for_free_shipping( array $data = null ): ?int { $data = $data ?? $this->get_free_shipping_data();
if ( empty( $data ) ) { return null; }
return round( floatval( $data['amount_remaining'] ), 0 ); }
/** * @param array|null $data * @return int */ function get_fill_percentage( array $data = null ): int { $data = $data ?? $this->get_free_shipping_data();
if ( empty( $data ) ) { return 0; }
return intval( $data['fill_percentage'] ); }
/** * @return array */ function get_free_shipping_data(): array { $data = array();
$has_free_shipping = false; $threshold = SettingsManager::instance()->get_setting( 'side_cart_free_shipping_threshold' ); $amount_remaining = null; $fill_percentage = null; $subtotal = round( WC()->cart->get_subtotal() + WC()->cart->get_subtotal_tax(), 0 );
if ( ! empty( $threshold ) && is_numeric( $threshold ) ) { return array( 'has_free_shipping' => ( $subtotal >= $threshold ), 'amount_remaining' => $subtotal >= $threshold ? 0 : $threshold - $subtotal, 'fill_percentage' => min( ceil( ( $subtotal / $threshold ) * 100 ), 100 ), ); }
WC()->cart->calculate_shipping();
$packages = WC()->shipping()->get_packages();
if ( empty( $packages ) ) { return $data; }
// Only look at first package for this feature $available_methods = $packages[0]['rates'];
foreach ( $available_methods as $available_method ) { if ( $available_method instanceof WC_Shipping_Free_Shipping ) { $has_free_shipping = true; break; } }
if ( ! $has_free_shipping ) { $shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $packages[0] ); $shipping_methods = $shipping_zone->get_shipping_methods( true );
foreach ( $shipping_methods as $shipping_method ) { if ( $shipping_method instanceof WC_Shipping_Free_Shipping && ( 'min_amount' === $shipping_method->requires || 'either' === $shipping_method->requires ) ) {
if ( 'no' === $shipping_method->ignore_discounts && ! empty( WC()->cart->get_coupon_discount_totals() ) ) { foreach ( WC()->cart->get_coupon_discount_totals() as $coupon_value ) { $subtotal -= $coupon_value; } }
if ( $subtotal >= $shipping_method->min_amount ) { $has_free_shipping = true; } else { $amount_remaining = $shipping_method->min_amount - $subtotal; $fill_percentage = ceil( ( $subtotal / $shipping_method->min_amount ) * 100 ); } break; } } }
if ( ! $has_free_shipping && is_null( $amount_remaining ) ) { return $data; }
$data = array( 'has_free_shipping' => $has_free_shipping, 'amount_remaining' => $amount_remaining, 'fill_percentage' => $has_free_shipping ? 100 : $fill_percentage, );
return apply_filters( 'xoo_wsc_shipping_bar_data', $data ); }
function maybe_output_shipping_progress_bar() { $quantity = array_sum( wp_list_pluck( WC()->cart->get_cart(), 'quantity' ) ); $data = $this->get_free_shipping_data(); $fill_percent = $this->get_fill_percentage( $data ); $has_free_shipping = $this->does_cart_qualifies_for_free_shipping( $data ); $amount_remaining = $this->get_remaining_amount_to_qualify_for_free_shipping( $data );
$free_shipping_message = SettingsManager::instance()->get_setting( 'side_cart_free_shipping_message' ); $amount_remaining_message = SettingsManager::instance()->get_setting( 'side_cart_amount_remaining_message' );
if ( empty( $free_shipping_message ) ) { $free_shipping_message = __( 'Congrats! You get free standard shipping.', 'checkout-wc' ); }
if ( empty( $amount_remaining_message ) ) { // translators: %s is the amount remaining for free shipping $amount_remaining_message = __( 'You\'re %s away from free shipping!', 'checkout-wc' ); }
$amount_remaining = '<strong>' . wc_price( $amount_remaining, array( 'decimals' => 0 ) ) . '</strong>';
$amount_remaining_message = sprintf( $amount_remaining_message, $amount_remaining );
if ( SettingsManager::instance()->get_setting( 'enable_free_shipping_progress_bar' ) === 'yes' && ! empty( $data ) && 0 < $quantity ) : ?> <div class="cfw-side-cart-free-shipping-progress-wrap"> <p class="cfw-xtra-small"> <?php if ( $has_free_shipping ) : ?> <?php echo $free_shipping_message; ?> <?php else : ?> <?php echo $amount_remaining_message; ?> <?php endif; ?> </p>
<div class="cfw-side-cart-free-shipping-progress"> <div class="cfw-side-cart-free-shipping-progress-indicator" style="width: <?php echo $fill_percent; ?>%;"></div> </div> </div> <?php endif; }
public function add_custom_css_property( $properties ) { $properties['--cfw-side-cart-free-shipping-progress-indicator'] = SettingsManager::instance()->get_setting( 'side_cart_free_shipping_progress_indicator_color' ); $properties['--cfw-side-cart-free-shipping-progress-background'] = SettingsManager::instance()->get_setting( 'side_cart_free_shipping_progress_bg_color' ); $properties['--cfw-side-cart-button-bottom-position'] = SettingsManager::instance()->get_setting( 'floating_cart_button_bottom_position' ) . 'px'; $properties['--cfw-side-cart-button-right-position'] = SettingsManager::instance()->get_setting( 'floating_cart_button_right_position' ) . 'px';
return $properties; }
function add_side_cart_to_fragments( array $fragments ): array { $fragments['#cfw-side-cart-inner'] = $this->get_side_cart_fragment(); $fragments['.cfw-side-cart-floating-button .cfw-side-cart-quantity'] = self::get_quantity();
return $fragments; }
function add_localized_settings( array $event_data ) : array { $event_data['settings']['enable_ajax_add_to_cart'] = SettingsManager::instance()->get_setting( 'enable_ajax_add_to_cart' ) === 'yes'; $event_data['runtime_params']['openCart'] = $this->itemJustAddedToCart;
return $event_data; }
function remove_cart_breadcrumb( $breadcrumbs ) { unset( $breadcrumbs['cart'] );
return $breadcrumbs; }
public function run_on_plugin_activation() { SettingsManager::instance()->add_setting( 'enable_side_cart', 'no' ); SettingsManager::instance()->add_setting( 'enable_ajax_add_to_cart', 'no' ); SettingsManager::instance()->add_setting( 'enable_free_shipping_progress_bar', 'no' ); SettingsManager::instance()->add_setting( 'side_cart_free_shipping_threshold', '' ); SettingsManager::instance()->add_setting( 'side_cart_amount_remaining_message', '' ); SettingsManager::instance()->add_setting( 'side_cart_free_shipping_message', '' ); SettingsManager::instance()->add_setting( 'side_cart_free_shipping_threshold', '' ); SettingsManager::instance()->add_setting( 'side_cart_free_shipping_progress_indicator_color', cfw_get_active_template()->get_default_setting( 'button_color' ) ); SettingsManager::instance()->add_setting( 'enable_floating_cart_button', 'yes' ); SettingsManager::instance()->add_setting( 'floating_cart_button_bottom_position', '20' ); SettingsManager::instance()->add_setting( 'floating_cart_button_right_position', '20' ); }
public static function get_cart_icon(): string { ob_start(); ?> <div class="cfw-side-cart-quantity-wrap"> <svg xmlns="http://www.w3.org/2000/svg" class="cfw-side-cart-quantity-icon" fill="none" viewBox="0 0 24 24" stroke="currentColor"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z" /> </svg>
<?php echo self::get_quantity(); ?> </div> <?php
return ob_get_clean(); }
public static function get_quantity(): string { $quantity = array_sum( wp_list_pluck( WC()->cart->get_cart(), 'quantity' ) );
ob_start(); ?> <div class="cfw-side-cart-quantity <?php echo 0 === $quantity ? 'cfw-hidden' : ''; ?>"> <?php echo $quantity; ?> </div> <?php
return ob_get_clean(); } }
|