Naposledy aktivní 1713289911

Malin's Avatar Malin revidoval tento gist 1713289911. Přejít na revizi

1 file changed, 108 insertions

custom-checkout-fields.php(vytvořil soubor)

@@ -0,0 +1,108 @@
1 + // Add a custom checkout fields for QRh, Tipo Cliente & NIF/CIF/NIE/DNI and save them to the database as META data
2 +
3 + /**
4 + * Add a 'Tipo cliente' dropdown field to the WooCommerce checkout page with specific options
5 + */
6 + add_filter('woocommerce_checkout_fields', 'custom_add_tipo_cliente_dropdown');
7 + function custom_add_tipo_cliente_dropdown($fields) {
8 + $fields['billing']['billing_tipo_cliente'] = array(
9 + 'type' => 'select',
10 + 'label' => __('Tipo cliente', 'woocommerce'),
11 + 'required' => true,
12 + 'class' => array('form-row-wide'),
13 + 'options' => array(
14 + '' => __('Elige un tipo de cliente', 'woocommerce'),
15 + 'estudiante' => __('Estudiante', 'woocommerce'),
16 + 'salon_de_belleza' => __('Salón de belleza', 'woocommerce'),
17 + 'esteticista' => __('Esteticista', 'woocommerce'),
18 + 'maquillador' => __('Maquillador', 'woocommerce'),
19 + 'salon_peluqueria_barberia' => __('Salón Peluquería / Barbería', 'woocommerce'),
20 + 'peluquero_barbero_autonomo' => __('Peluquero / Barbero autónomo', 'woocommerce'),
21 + 'peluquero_barbero_empleado' => __('Peluquero / Barbero empleado', 'woocommerce'),
22 + ),
23 + 'priority' => 22,
24 + );
25 +
26 + return $fields;
27 + }
28 +
29 + /**
30 + * Process and validate the 'Tipo cliente' dropdown field
31 + */
32 + add_action('woocommerce_checkout_process', 'custom_validate_tipo_cliente_field');
33 + function custom_validate_tipo_cliente_field() {
34 + if (!isset($_POST['billing_tipo_cliente']) || empty($_POST['billing_tipo_cliente'])) {
35 + wc_add_notice(__('Elige un tipo de cliente por favor.', 'woocommerce'), 'error');
36 + }
37 + }
38 +
39 + // For a WooCommerce registration form, use the following instead:
40 + // add_action('woocommerce_register_form', 'custom_add_user_type_registration_dropdown');
41 +
42 + function custom_save_user_type( $user_id ) {
43 + if ( ! empty( $_POST['user_type'] ) ) {
44 + update_user_meta( $user_id, 'user_type', sanitize_text_field( $_POST['user_type'] ) );
45 + }
46 + }
47 + // For a generic WordPress registration
48 + //add_action( 'user_register', 'custom_save_user_type' );
49 + // For a WooCommerce-specific registration, use the following instead:
50 + add_action( 'woocommerce_created_customer', 'custom_save_user_type' );
51 +
52 + /**
53 + * Add a CIF/NIF field to the WooCommerce checkout page
54 + */
55 + add_filter('woocommerce_checkout_fields', 'custom_add_checkout_fields');
56 + function custom_add_checkout_fields($fields) {
57 + $fields['billing']['billing_nif'] = array(
58 + 'label' => __('CIF/NIF/DNI/NIE', 'woocommerce'),
59 + 'placeholder' => _x('CIF/NIF/DNI/NIE', 'placeholder', 'woocommerce'),
60 + 'required' => true,
61 + 'class' => array('form-row-wide'),
62 + 'clear' => true,
63 + 'priority' => 21, // Adjust the priority to position just after the last name
64 + );
65 +
66 + return $fields;
67 + }
68 +
69 + /**
70 + * Process and validate the CIF/NIF field
71 + */
72 + add_action('woocommerce_checkout_process', 'custom_validate_checkout_field_process');
73 + function custom_validate_checkout_field_process() {
74 + if (!$_POST['billing_nif'])
75 + wc_add_notice(__('Please enter your CIF/NIF.'), 'error');
76 + }
77 +
78 + /**
79 + * Save the 'Tipo cliente' field with the order and user meta
80 + */
81 + add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta_tipo_cliente');
82 + function custom_checkout_field_update_order_meta_tipo_cliente($order_id) {
83 + if (!empty($_POST['billing_tipo_cliente'])) {
84 + // Save to order meta
85 + update_post_meta($order_id, '_billing_tipo_cliente', sanitize_text_field($_POST['billing_tipo_cliente']));
86 +
87 + // Get the customer user ID
88 + $customer_user_id = get_post_meta($order_id, '_customer_user', true);
89 + if ($customer_user_id) {
90 + // Save to user meta as well
91 + update_user_meta($customer_user_id, 'billing_tipo_cliente', sanitize_text_field($_POST['billing_tipo_cliente']));
92 + }
93 + }
94 + }
95 +
96 + add_filter( 'woocommerce_checkout_get_value', 'custom_checkout_field_default_value', 10, 2 );
97 + function custom_checkout_field_default_value( $value, $input ) {
98 + $current_user = wp_get_current_user();
99 +
100 + if ( is_user_logged_in() && $input == 'billing_tipo_cliente' ) {
101 + $user_tipo_cliente = get_user_meta( $current_user->ID, 'billing_tipo_cliente', true );
102 + if ( !empty($user_tipo_cliente) ) {
103 + $value = $user_tipo_cliente;
104 + }
105 + }
106 +
107 + return $value;
108 + }
Novější Starší