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 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3.0.1 6.1.0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7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Blocks / Domain / Services / CheckoutFieldsFrontend.php
woocommerce / src / Blocks / Domain / Services Last commit date
CheckoutFieldsSchema 1 year ago Email 1 year ago CheckoutFields.php 2 weeks ago CheckoutFieldsAdmin.php 2 years ago CheckoutFieldsFrontend.php 2 weeks ago CheckoutLink.php 1 year ago CreateAccount.php 1 year ago DraftOrders.php 4 months ago FeatureGating.php 1 year ago GoogleAnalytics.php 2 years ago Hydration.php 2 weeks ago Notices.php 2 weeks ago functions.php 2 years ago
CheckoutFieldsFrontend.php
392 lines
1 <?php
2
3 namespace Automattic\WooCommerce\Blocks\Domain\Services;
4
5 use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema\DocumentObject;
6 use WC_Customer;
7 use WC_Order;
8
9 /**
10 * Service class managing checkout fields and its related extensibility points on the frontend.
11 */
12 class CheckoutFieldsFrontend {
13
14 /**
15 * Checkout field controller.
16 *
17 * @var CheckoutFields
18 */
19 private $checkout_fields_controller;
20
21 /**
22 * Sets up core fields.
23 *
24 * @param CheckoutFields $checkout_fields_controller Instance of the checkout field controller.
25 */
26 public function __construct( CheckoutFields $checkout_fields_controller ) {
27 $this->checkout_fields_controller = $checkout_fields_controller;
28 }
29
30 /**
31 * Initialize hooks. This is not run Store API requests.
32 */
33 public function init() {
34 // Show custom checkout fields on the order details page.
35 add_action( 'woocommerce_order_details_after_customer_address', array( $this, 'render_order_address_fields' ), 10, 2 );
36 add_action( 'woocommerce_order_details_after_customer_details', array( $this, 'render_order_other_fields' ), 10 );
37
38 // Show custom checkout fields on the My Account page.
39 add_action( 'woocommerce_my_account_after_my_address', array( $this, 'render_address_fields' ), 10, 1 );
40
41 // Edit account form under my account (for contact details).
42 add_filter( 'woocommerce_edit_account_form_fields', array( $this, 'edit_account_form_fields' ), 10, 1 );
43 add_action( 'woocommerce_save_account_details', array( $this, 'save_account_form_fields' ), 10, 1 );
44
45 // Edit address form under my account.
46 add_filter( 'woocommerce_address_to_edit', array( $this, 'edit_address_fields' ), 10, 2 );
47 add_action( 'woocommerce_customer_save_address', array( $this, 'save_address_fields' ), 10, 4 );
48 }
49
50 /**
51 * Render custom fields.
52 *
53 * @param array $fields List of additional fields with values.
54 * @return string
55 */
56 protected function render_additional_fields( $fields ) {
57 return ! empty( $fields ) ? '<dl class="wc-block-components-additional-fields-list">' . implode( '', array_map( array( $this, 'render_additional_field' ), $fields ) ) . '</dl>' : '';
58 }
59
60 /**
61 * Render custom field.
62 *
63 * @param array $field An additional field and value.
64 * @return string
65 */
66 protected function render_additional_field( $field ) {
67 return sprintf(
68 '<dt>%1$s</dt><dd>%2$s</dd>',
69 esc_html( $field['label'] ),
70 esc_html( $field['value'] )
71 );
72 }
73
74 /**
75 * Renders address fields on the order details page.
76 *
77 * @param string $address_type Type of address (billing or shipping).
78 * @param WC_Order $order Order object.
79 */
80 public function render_order_address_fields( $address_type, $order ) {
81 $fields = $this->checkout_fields_controller->get_order_additional_fields_with_values( $order, 'address', $address_type, 'view' );
82
83 $context = array(
84 'caller' => 'CheckoutFieldsFrontend::render_order_address_fields',
85 'address_type' => $address_type,
86 'order' => $order,
87 );
88
89 $fields = $this->checkout_fields_controller->filter_fields_for_order_confirmation( $fields, $context );
90
91 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
92 echo $this->render_additional_fields( $fields );
93 }
94
95 /**
96 * Renders additional fields on the order details page.
97 *
98 * @param WC_Order $order Order object.
99 */
100 public function render_order_other_fields( $order ) {
101 $fields = array_merge(
102 $this->checkout_fields_controller->get_order_additional_fields_with_values( $order, 'contact', 'other', 'view' ),
103 $this->checkout_fields_controller->get_order_additional_fields_with_values( $order, 'order', 'other', 'view' ),
104 );
105
106 $context = array(
107 'caller' => 'CheckoutFieldsFrontend::render_order_other_fields',
108 'order' => $order,
109 );
110
111 $fields = $this->checkout_fields_controller->filter_fields_for_order_confirmation( $fields, $context );
112
113 if ( ! $fields ) {
114 return;
115 }
116
117 echo '<section class="wc-block-order-confirmation-additional-fields-wrapper">';
118 echo '<h2>' . esc_html__( 'Additional information', 'woocommerce' ) . '</h2>';
119 echo $this->render_additional_fields( $fields ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
120 echo '</section>';
121 }
122
123 /**
124 * Renders address fields on the account page.
125 *
126 * @param string $address_type Type of address (billing or shipping).
127 */
128 public function render_address_fields( $address_type ) {
129 if ( ! in_array( $address_type, array( 'billing', 'shipping' ), true ) ) {
130 return;
131 }
132
133 $customer = new WC_Customer( get_current_user_id() );
134
135 $document_object = new DocumentObject();
136 $document_object->set_customer( $customer );
137 $document_object->set_context( $address_type . '_address' );
138 $fields = $this->checkout_fields_controller->get_contextual_fields_for_location( 'address', $document_object );
139
140 if ( ! $fields || ! $customer ) {
141 return;
142 }
143
144 foreach ( $fields as $key => $field ) {
145 $value = $this->checkout_fields_controller->format_additional_field_value(
146 $this->checkout_fields_controller->get_field_from_object( $key, $customer, $address_type ),
147 $field
148 );
149
150 if ( ! $value ) {
151 continue;
152 }
153
154 printf( '<br><strong>%s</strong>: %s', wp_kses_post( $field['label'] ), wp_kses_post( $value ) );
155 }
156 }
157
158 /**
159 * Adds additional contact fields to the My Account edit account form.
160 */
161 public function edit_account_form_fields() {
162 $customer = new WC_Customer( get_current_user_id() );
163
164 $document_object = new DocumentObject();
165 $document_object->set_customer( $customer );
166 $document_object->set_context( 'contact' );
167 $fields = $this->checkout_fields_controller->get_contextual_fields_for_location( 'contact', $document_object );
168
169 foreach ( $fields as $key => $field ) {
170 $field_key = CheckoutFields::get_group_key( 'other' ) . $key;
171 $form_field = $field;
172 $form_field['id'] = $field_key;
173 $form_field['value'] = $this->checkout_fields_controller->get_field_from_object( $key, $customer, 'contact' );
174
175 if ( 'select' === $field['type'] ) {
176 $form_field['options'] = array_column( $field['options'], 'label', 'value' );
177 }
178
179 if ( 'checkbox' === $field['type'] ) {
180 $form_field['checked_value'] = '1';
181 $form_field['unchecked_value'] = '0';
182 }
183
184 woocommerce_form_field( $field_key, $form_field, wc_get_post_data_by_key( $key, $form_field['value'] ) );
185 }
186 }
187
188 /**
189 * Adds additional address fields to the My Account edit address form.
190 *
191 * @param array $address Address fields.
192 * @param string $address_type Type of address (billing or shipping).
193 * @return array Updated address fields.
194 */
195 public function edit_address_fields( $address, $address_type ) {
196 $customer = new WC_Customer( get_current_user_id() );
197
198 $document_object = new DocumentObject();
199 $document_object->set_customer( $customer );
200 $document_object->set_context( $address_type . '_address' );
201 $fields = $this->checkout_fields_controller->get_contextual_fields_for_location( 'address', $document_object );
202
203 foreach ( $fields as $key => $field ) {
204 $field_key = CheckoutFields::get_group_key( $address_type ) . $key;
205 $address[ $field_key ] = $field;
206 $address[ $field_key ]['value'] = $this->checkout_fields_controller->get_field_from_object( $key, $customer, $address_type );
207
208 if ( 'select' === $field['type'] ) {
209 $address[ $field_key ]['options'] = array_column( $field['options'], 'label', 'value' );
210
211 // If a placeholder is set, add a placeholder option if it doesn't exist already.
212 if (
213 ! empty( $address[ $field_key ]['placeholder'] )
214 && ! array_key_exists( '', $address[ $field_key ]['options'] )
215 ) {
216 $address[ $field_key ]['options'] = array( '' => $address[ $field_key ]['placeholder'] ) + $address[ $field_key ]['options'];
217 }
218 }
219
220 if ( 'checkbox' === $field['type'] ) {
221 $address[ $field_key ]['checked_value'] = '1';
222 $address[ $field_key ]['unchecked_value'] = '0';
223 }
224 }
225
226 return $address;
227 }
228
229 /**
230 * Validates and saves additional address fields to the customer object on the My Account page.
231 *
232 * Customer is not provided by this hook so we handle save here.
233 *
234 * @param integer $user_id User ID.
235 */
236 public function save_account_form_fields( $user_id ) {
237 try {
238 $customer = new WC_Customer( $user_id );
239 $result = $this->update_additional_fields_for_customer( $customer, 'contact', 'other' );
240
241 if ( is_wp_error( $result ) ) {
242 foreach ( $result->get_error_messages() as $error_message ) {
243 wc_add_notice( $error_message, 'error' );
244 }
245 }
246
247 $customer->save();
248 } catch ( \Exception $e ) {
249 wc_add_notice(
250 sprintf(
251 /* translators: %s: Error message. */
252 __( 'An error occurred while saving account details: %s', 'woocommerce' ),
253 esc_html( $e->getMessage() )
254 ),
255 'error'
256 );
257 }
258 }
259
260 /**
261 * For the My Account page, save address fields. This uses the Store API endpoint for saving addresses so
262 * extensibility hooks are consistent across the codebase.
263 *
264 * The caller saves the customer object if there are no errors. Nonces are checked before this method executes.
265 *
266 * @param integer $user_id User ID.
267 * @param string $address_type Type of address (billing or shipping).
268 * @param array $address Address fields.
269 * @param WC_Customer $customer Customer object.
270 */
271 public function save_address_fields( $user_id, $address_type, $address = [], $customer = null ) {
272 try {
273 $customer = $customer ?? new WC_Customer( $user_id );
274 $result = $this->update_additional_fields_for_customer( $customer, 'address', $address_type );
275
276 if ( is_wp_error( $result ) ) {
277 foreach ( $result->get_error_messages() as $error_message ) {
278 wc_add_notice( $error_message, 'error' );
279 }
280 }
281
282 $customer->save();
283 } catch ( \Exception $e ) {
284 wc_add_notice(
285 sprintf(
286 /* translators: %s: Error message. */
287 __( 'An error occurred while saving address details: %s', 'woocommerce' ),
288 esc_html( $e->getMessage() )
289 ),
290 'error'
291 );
292 }
293 }
294
295 /**
296 * Get posted additional field values.
297 *
298 * @param string $location The location to get fields for.
299 * @param string $group The group to get fields for.
300 * @param boolean $sanitize Whether to sanitize the field values.
301 * @return array The posted field values and sanitized field values.
302 */
303 protected function get_posted_additional_field_values( $location, $group, $sanitize = true ) {
304 $additional_fields = $this->checkout_fields_controller->get_fields_for_location( $location );
305 $field_values = [];
306
307 // phpcs:disable WordPress.Security.NonceVerification.Missing
308 foreach ( $additional_fields as $field_key => $field_data ) {
309 $post_key = CheckoutFields::get_group_key( $group ) . $field_key;
310 $field_values[ $field_key ] = wc_clean( wp_unslash( $_POST[ $post_key ] ?? '' ) );
311
312 if ( $sanitize ) {
313 $field_values[ $field_key ] = $this->checkout_fields_controller->sanitize_field( $field_key, $field_values[ $field_key ] );
314 }
315 }
316 // phpcs:enable WordPress.Security.NonceVerification.Missing
317 return $field_values;
318 }
319
320 /**
321 * Validate and save additional fields for a given customer.
322 *
323 * @param WC_Customer $customer Customer object.
324 * @param string $location Location to save fields for.
325 * @param string $group Group to save fields for.
326 * @return true|\WP_Error True if successful, \WP_Error if there are errors.
327 */
328 protected function update_additional_fields_for_customer( $customer, $location, $group ) {
329 // Get all values from the POST request before validating.
330 $field_values = $this->get_posted_additional_field_values( $location, $group, false ); // These values are used to see if required fields have values.
331 $sanitized_field_values = $this->get_posted_additional_field_values( $location, $group ); // These values are used to validate custom rules, generate the document object, and save fields to the account.
332
333 $document_object = new DocumentObject(
334 [
335 'customer' => [
336 ( 'address' === $location ? $group . '_address' : 'additional_fields' ) => $sanitized_field_values,
337 ],
338 ]
339 );
340 $document_object->set_customer( $customer );
341 $document_object->set_context( 'address' === $location ? $group . '_address' : $location );
342 $fields = $this->checkout_fields_controller->get_contextual_fields_for_location( $location, $document_object );
343
344 // Holds values to be persisted to the customer object.
345 $persist_fields = [];
346 $errors = new \WP_Error();
347
348 // Validate individual fields agains the document object. Errors are added to the $errors object, and each field is validated regardless of other field errors.
349 foreach ( $fields as $field_key => $field ) {
350 $field_value = $field_values[ $field_key ];
351
352 if ( empty( $field_value ) ) {
353 if ( true === $field['required'] ) {
354 $errors->add(
355 'required_field',
356 /* translators: %s: is the field label */
357 sprintf( __( '%s is required', 'woocommerce' ), '<strong>' . $field['label'] . '</strong>' )
358 );
359 continue;
360 }
361 $persist_fields[ $field_key ] = '';
362 continue;
363 }
364
365 $sanitized_field_value = $sanitized_field_values[ $field_key ];
366 $valid_check = $this->checkout_fields_controller->validate_field( $field, $sanitized_field_value );
367
368 if ( is_wp_error( $valid_check ) && $valid_check->has_errors() ) {
369 // Get one error message from the WP_Error object per field to avoid overlapping error messages.
370 $errors->add( $valid_check->get_error_code(), $valid_check->get_error_message() );
371 continue;
372 }
373
374 $persist_fields[ $field_key ] = $sanitized_field_value;
375 }
376
377 // Validate all fields for this location (this runs custom validation callbacks). If an error is found, no values will be persisted to the customer object.
378 $location_validation = $this->checkout_fields_controller->validate_fields_for_location( $sanitized_field_values, $location, $group );
379
380 if ( is_wp_error( $location_validation ) && $location_validation->has_errors() ) {
381 $errors->merge_from( $location_validation );
382 return $errors;
383 }
384
385 foreach ( $persist_fields as $field_key => $field_value ) {
386 $this->checkout_fields_controller->persist_field_for_customer( $field_key, $field_value, $customer, $group );
387 }
388
389 return $errors->has_errors() ? $errors : true;
390 }
391 }
392