PluginProbe
PostNL for WooCommerce / 5.9.12
PostNL for WooCommerce v5.9.12
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / src / Frontend / Checkout_Fields.php

Checkout_Fields.php in PostNL for WooCommerce 5.9.12, at src/Frontend/Checkout_Fields.php

168 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Frontend/Checkout_Fields file.
4 *
5 * @package PostNLWooCommerce\Frontend
6 */
7
8 namespace PostNLWooCommerce\Frontend;
9
10 use PostNLWooCommerce\Shipping_Method\Settings;
11 use PostNLWooCommerce\Utils;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Class Checkout_Fields
19 *
20 * @package PostNLWooCommerce\Frontend
21 */
22 class Checkout_Fields {
23 /**
24 * Settings class instance.
25 *
26 * @var Settings
27 */
28 protected $settings;
29
30 /**
31 * Init and hook in the integration.
32 */
33 public function __construct() {
34 $this->settings = Settings::get_instance();
35
36 $this->init_hooks();
37 }
38
39 /**
40 * Collection of hooks when initiation.
41 */
42 public function init_hooks() {
43 if ( ! $this->settings->is_reorder_nl_address_enabled() ) {
44 return;
45 }
46
47 if ( ! Utils::is_blocks_checkout() ) {
48 add_filter( 'woocommerce_default_address_fields', array( $this, 'add_house_number' ) );
49 }
50
51 add_filter( 'woocommerce_get_country_locale', array( $this, 'get_country_locale' ) );
52 add_filter( 'woocommerce_country_locale_field_selectors', array( $this, 'country_locale_field_selectors' ) );
53 }
54
55 /**
56 * Add House Number to address fields.
57 *
58 * @param array $address_fields_array Address fields.
59 *
60 * @return array
61 */
62 public function add_house_number( array $address_fields_array ): array {
63 $address_fields_array['house_number'] = array(
64 'type' => 'text',
65 'label' => __( 'House number', 'postnl-for-woocommerce' ),
66 'placeholder' => esc_attr__( 'House number', 'postnl-for-woocommerce' ),
67 'required' => false,
68 'hidden' => true,
69 );
70
71 return $address_fields_array;
72 }
73
74 /**
75 * Localization for NL address fields.
76 *
77 * @param array $checkout_fields Checkout fields.
78 *
79 * @return array
80 */
81 public function get_country_locale( array $checkout_fields ): array {
82 $is_blocks_checkout = Utils::is_blocks_checkout();
83
84 if ( $is_blocks_checkout ) {
85 $house_number_key = 'postnl/house_number';
86 } else {
87 $house_number_key = 'house_number';
88 }
89
90 $fields_to_order = array(
91 'first_name' => array( 'priority' => 1 ),
92 'last_name' => array( 'priority' => 2 ),
93 'company' => array( 'priority' => 3 ),
94 'country' => array( 'priority' => 4 ),
95 'postcode' => array( 'priority' => 5 ),
96 $house_number_key => array(
97 'priority' => 6,
98 'required' => true,
99 'hidden' => false,
100 ),
101 'address_2' => array(
102 'label' => esc_attr__( 'House Number Extension', 'postnl-for-woocommerce' ),
103 'placeholder' => esc_attr__( 'House Number Extension', 'postnl-for-woocommerce' ),
104 'priority' => 7,
105 ),
106 'address_1' => array(
107 'label' => __( 'Street', 'postnl-for-woocommerce' ),
108 'placeholder' => esc_attr__( 'Street Name', 'postnl-for-woocommerce' ),
109 'priority' => 8,
110 ),
111 'city' => array( 'priority' => 9 ),
112 );
113
114 if ( $is_blocks_checkout ) {
115 unset( $fields_to_order['company'] );
116 }
117
118 foreach ( $fields_to_order as $field_key => $field ) {
119 foreach ( $field as $override => $value ) {
120 $checkout_fields['NL'][ $field_key ][ $override ] = $value;
121 }
122 }
123
124 /**
125 * Hide house number from the other countries .
126 */
127 if ( $is_blocks_checkout ) {
128 $countries_codes = array_keys( WC()->countries->get_countries() );
129 foreach ( $countries_codes as $country_code ) {
130 if ( 'NL' !== $country_code ) {
131 $checkout_fields[ $country_code ][ $house_number_key ]['hidden'] = true;
132 $checkout_fields[ $country_code ][ $house_number_key ]['required'] = false;
133 }
134 }
135 }
136
137 return $checkout_fields;
138 }
139
140 /**
141 * Add fields selectors
142 *
143 * @param array $locale_fields local fields.
144 *
145 * @return array
146 */
147 public function country_locale_field_selectors( $locale_fields ) {
148 $additional_selectors = array(
149 'house_number' => '#billing_house_number_field, #shipping_house_number_field',
150 'first_name' => '#billing_first_name_field, #shipping_first_name_field',
151 'last_name' => '#billing_last_name_field, #shipping_last_name_field',
152 );
153
154 return array_merge( $locale_fields, $additional_selectors );
155 }
156
157 /**
158 * Is using blocks checkout.
159 *
160 * @return boolean
161 */
162 public function is_blocks_checkout(): bool {
163 $checkout_page_id = wc_get_page_id( 'checkout' );
164
165 return has_block( 'woocommerce/checkout', $checkout_page_id );
166 }
167 }
168