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
|
<?php
namespace Objectiv\Plugins\Checkout\Action;
class UpdateCheckoutAction extends CFWAction { public function __construct() { parent::__construct( 'update_checkout', false ); }
public function action() { check_ajax_referer( 'update-order-review', 'security' );
\WC_Checkout::instance(); wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
if ( WC()->cart->is_empty() && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_update_order_review_expired', true ) ) { $target_selector = apply_filters( 'cfw_session_expired_target_element', 'form.woocommerce-checkout' );
$this->out( array( 'redirect' => false, 'fragments' => apply_filters( 'woocommerce_update_order_review_fragments', array( $target_selector => '<div class="woocommerce-error">' . cfw__( 'Sorry, your session has expired.', 'woocommerce' ) . ' <a href="' . esc_url( wc_get_page_permalink( 'shop' ) ) . '" class="wc-backward">' . cfw__( 'Return to shop', 'woocommerce' ) . '</a></div>', ) ), ) ); }
/** This action is documented in woocommerce/includes/class-wc-ajax.php */ do_action( 'woocommerce_checkout_update_order_review', isset( $_POST['post_data'] ) ? wp_unslash( $_POST['post_data'] ) : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
/** * Fires when updating CheckoutWC order review * * @since 2.0.0 * * @param string $post_data The POST data */ do_action( 'cfw_checkout_update_order_review', isset( $_POST['post_data'] ) ? wp_unslash( $_POST['post_data'] ) : '' );
parse_str( wp_unslash( $_POST['post_data'] ), $post_data );
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
if ( isset( $_POST['shipping_method'] ) && is_array( $_POST['shipping_method'] ) ) { foreach ( $_POST['shipping_method'] as $i => $value ) { $chosen_shipping_methods[ $i ] = wc_clean( $value ); } }
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
if ( ! empty( $_POST['payment_method'] ) ) { WC()->session->set( 'chosen_payment_method', $_POST['payment_method'] ); }
WC()->customer->set_props( array( 'billing_country' => isset( $_POST['country'] ) ? wc_clean( wp_unslash( $_POST['country'] ) ) : null, 'billing_state' => isset( $_POST['state'] ) ? wc_clean( wp_unslash( $_POST['state'] ) ) : null, 'billing_postcode' => isset( $_POST['postcode'] ) ? trim( wc_clean( wp_unslash( $_POST['postcode'] ) ) ) : null, 'billing_city' => isset( $_POST['city'] ) ? wc_clean( wp_unslash( $_POST['city'] ) ) : null, 'billing_address_1' => isset( $_POST['address'] ) ? wc_clean( wp_unslash( $_POST['address'] ) ) : null, 'billing_address_2' => isset( $_POST['address_2'] ) ? wc_clean( wp_unslash( $_POST['address_2'] ) ) : null, ) );
if ( wc_ship_to_billing_address_only() || ! WC()->cart->needs_shipping() ) { WC()->customer->set_props( array( 'shipping_country' => isset( $_POST['country'] ) ? wc_clean( wp_unslash( $_POST['country'] ) ) : null, 'shipping_state' => isset( $_POST['state'] ) ? wc_clean( wp_unslash( $_POST['state'] ) ) : null, 'shipping_postcode' => isset( $_POST['postcode'] ) ? trim( wc_clean( wp_unslash( $_POST['postcode'] ) ) ) : null, 'shipping_city' => isset( $_POST['city'] ) ? wc_clean( wp_unslash( $_POST['city'] ) ) : null, 'shipping_address_1' => isset( $_POST['address'] ) ? wc_clean( wp_unslash( $_POST['address'] ) ) : null, 'shipping_address_2' => isset( $_POST['address_2'] ) ? wc_clean( wp_unslash( $_POST['address_2'] ) ) : null, ) ); } else { WC()->customer->set_props( array( 'shipping_country' => isset( $_POST['s_country'] ) ? wc_clean( wp_unslash( $_POST['s_country'] ) ) : null, 'shipping_state' => isset( $_POST['s_state'] ) ? wc_clean( wp_unslash( $_POST['s_state'] ) ) : null, 'shipping_postcode' => isset( $_POST['s_postcode'] ) ? trim( wc_clean( wp_unslash( $_POST['s_postcode'] ) ) ) : null, 'shipping_city' => isset( $_POST['s_city'] ) ? wc_clean( wp_unslash( $_POST['s_city'] ) ) : null, 'shipping_address_1' => isset( $_POST['s_address'] ) ? wc_clean( wp_unslash( $_POST['s_address'] ) ) : null, 'shipping_address_2' => isset( $_POST['s_address_2'] ) ? wc_clean( wp_unslash( $_POST['s_address_2'] ) ) : null, ) ); }
if ( isset( $_POST['has_full_address'] ) && wc_string_to_bool( wc_clean( wp_unslash( $_POST['has_full_address'] ) ) ) ) { WC()->customer->set_calculated_shipping( true ); } else { WC()->customer->set_calculated_shipping( false ); }
WC()->customer->save();
do_action( 'cfw_update_checkout_after_customer_save', $post_data );
/** * Filters whether to redirect the checkout page during refresh * * @since 2.0.0 * * @param bool|string Boolean false means don't redirect, string means redirect to URL */ $redirect = apply_filters( 'cfw_update_checkout_redirect', false );
// Calculate shipping before totals. This will ensure any shipping methods that affect things like taxes are chosen prior to final totals being calculated. Ref: #22708. WC()->cart->calculate_shipping(); WC()->cart->calculate_totals();
unset( WC()->session->refresh_totals, WC()->session->reload_checkout );
/** * Fetch available gateways and make sure at least one is set * * This is to fix an issue where removing a free coupon doesn't show a selected gateway * until the second refresh - not idea! */ $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
reset( $available_gateways );
$first_gateway = key( $available_gateways ); $have_chosen_gateway = false;
foreach ( $available_gateways as $key => $available_gateway ) { if ( $available_gateway->chosen ) { $have_chosen_gateway = true; } }
if ( ! $have_chosen_gateway ) { if ( isset( $available_gateways[ WC()->session->get( 'chosen_payment_method' ) ] ) ) { $available_gateways[ WC()->session->get( 'chosen_payment_method' ) ]->chosen = true; } else { $available_gateways[ $first_gateway ]->chosen = true; WC()->session->set( 'chosen_payment_method', $first_gateway ); } }
/** * Filters payment methods during update_checkout refresh * * @since 2.0.0 * * @param bool|string Boolean false means don't redirect, string means redirect to URL */ $updated_payment_methods = apply_filters( 'cfw_update_payment_methods', cfw_get_payment_methods( $available_gateways, false, true, false ) );
/** This action is documented in woocommerce/includes/class-wc-checkout.php */ do_action( 'woocommerce_check_cart_items' );
/** * Validate Postcode */ $checkout_fieldsets = WC()->checkout()->get_checkout_fields(); $checkout_fields = array_merge( $checkout_fieldsets['billing'], $checkout_fieldsets['shipping'] ); $postcode_fields = array( 'shipping_postcode' => 'shipping_country', );
if ( ! empty( $_POST['bill_to_different_address'] ) && 'same_as_shipping' !== $_POST['bill_to_different_address'] ) { $postcode_fields['billing_postcode'] = 'billing_country'; }
foreach ( $postcode_fields as $postcode_field => $country_field ) { if ( ! empty( $_POST[ $postcode_field ] ) && ! empty( $_POST[ $country_field ] ) ) { if ( ! \WC_Validation::is_postcode( trim( $_POST[ $postcode_field ] ), $_POST[ $country_field ] ) ) { $field_label = $checkout_fields[ $postcode_field ]['label'];
if ( stripos( $postcode_field, 'shipping_' ) !== false ) { $field_label = sprintf( cfw_x( 'Shipping %s', 'checkout-validation', 'woocommerce' ), $field_label ); } else { $field_label = sprintf( cfw_x( 'Billing %s', 'checkout-validation', 'woocommerce' ), $field_label ); }
switch ( $_POST[ $country_field ] ) { case 'IE': /* translators: %1$s: field name, %2$s finder.eircode.ie URL */ $postcode_validation_notice = sprintf( cfw__( '%1$s is not valid. You can look up the correct Eircode <a target="_blank" href="%2$s">here</a>.', 'woocommerce' ), '<strong>' . esc_html( $field_label ) . '</strong>', 'https://finder.eircode.ie' ); break; default: /* translators: %s: field name */ $postcode_validation_notice = sprintf( cfw__( '%s is not a valid postcode / ZIP.', 'woocommerce' ), '<strong>' . esc_html( $field_label ) . '</strong>' ); }
wc_add_notice( $postcode_validation_notice, 'error' ); } } }
// Chosen shipping methods $chosen_shipping_methods_labels = array();
$packages = WC()->shipping->get_packages();
foreach ( $packages as $i => $package ) { $chosen_method = WC()->session->get( 'chosen_shipping_methods' )[ $i ] ?? false; $available_methods = $package['rates'];
if ( $chosen_method && method_exists( $available_methods[ $chosen_method ], 'get_label' ) ) { $chosen_shipping_methods_labels[] = $available_methods[ $chosen_method ]->get_label(); } }
/** * Filters chosen shipping methods label * * @since 2.0.0 * * @param string $chosen_shipping_methods_labels The chosen shipping methods */ $chosen_shipping_methods_labels = apply_filters( 'cfw_payment_method_address_review_shipping_method', $chosen_shipping_methods_labels );
$update_checkout_output = array( 'needs_payment' => WC()->cart->needs_payment(), 'fragments' => apply_filters( 'woocommerce_update_order_review_fragments', /** This filter is documented in woocommerce/includes/class-wc-ajax.php */ array( '.cfw-review-pane-shipping-address-value' => '<div class="cfw-review-pane-content cfw-review-pane-shipping-address-value">' . cfw_get_review_pane_shipping_address( WC()->checkout() ) . '</div>', '.cfw-review-pane-contact-value' => '<div class="cfw-review-pane-content cfw-review-pane-contact-value">' . apply_filters( 'cfw_review_pane_contact_value', WC()->checkout()->get_value( 'billing_email' ) ). '</div>', '.cfw-review-pane-shipping-method-value' => '<div class="cfw-review-pane-content cfw-review-pane-shipping-method-value">' . join( ', ', $chosen_shipping_methods_labels ) . '</div>', '.cfw-review-pane-payment-method-value' => '<div class="cfw-review-pane-content cfw-review-pane-payment-method-value">' . cfw_get_review_pane_payment_method() . '</div>', '#cfw-checkout-before-order-review' => $this->get_action_output( 'woocommerce_checkout_before_order_review', 'cfw-checkout-before-order-review' ), '#cfw-checkout-after-order-review' => $this->get_action_output( 'woocommerce_checkout_after_order_review', 'cfw-checkout-after-order-review' ), '#cfw-place-order' => cfw_get_place_order(), '#cfw-totals-list' => cfw_get_totals_html(), '#cfw-cart' => cfw_get_item_summary_table( WC()->cart ), '#cfw-mobile-total' => '<span id="cfw-mobile-total" class="total amount cfw-display-table-cell">' . WC()->cart->get_total() . '</span>', '#cfw-billing-methods' => $updated_payment_methods, '#cfw-shipping-methods' => '<div id="cfw-shipping-methods" class="cfw-module">' . cfw_get_shipping_methods_html() . '</div>', '#cfw-review-order-totals' => cfw_return_function_output( 'cfw_order_review_step_totals_review_pane' ), ) ), 'redirect' => $redirect, 'show_shipping_tab' => cfw_show_shipping_tab(), 'has_valid_shipping_method' => cfw_has_valid_shipping_methods(), );
// Do this last so that anything that runs above can bubble up a notice $update_checkout_output['notices'] = $this->get_notices();
$this->out( $update_checkout_output ); }
function get_action_output( $action, $container = '' ) { ob_start();
echo '<div id="' . $container . '">'; do_action( $action ); echo '</div>';
return ob_get_clean(); }
/** * @return array */ function get_notices(): array { /** * Set notices */ $all_notices = WC()->session->get( 'wc_notices', array() );
// Filter out empty messages foreach ( $all_notices as $key => $notice ) { if ( empty( array_filter( $notice ) ) ) { unset( $all_notices[ $key ] ); } }
/** This filter is documented in woocommerce/includes/wc-notice-functions.php **/ $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) ); $notices = array();
foreach ( $notice_types as $notice_type ) { if ( wc_notice_count( $notice_type ) > 0 && isset( $all_notices[ $notice_type ] ) ) { $notices[ $notice_type ] = array();
// In WooCommerce 3.9+, messages can be an array with two properties: // - notice // - data foreach ( $all_notices[ $notice_type ] as $notice ) { $notices[ $notice_type ][] = $notice['notice'] ?? $notice; } } }
wc_clear_notices();
return $notices; } }
|