PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.4.2
ShopBuilder – WooCommerce Builder For Elementor v3.4.2
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Modules / CheckoutEditor / GeneralCheckout.php

GeneralCheckout.php in ShopBuilder – WooCommerce Builder For Elementor 3.4.2, at app/Modules/CheckoutEditor/GeneralCheckout.php

176 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace RadiusTheme\SB\Modules\CheckoutEditor;
4
5 // Do not allow directly accessing this file.
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit( 'This script cannot be accessed directly.' );
8 }
9
10 use RadiusTheme\SB\Traits\SingletonTrait;
11
12 /**
13 * CheckoutEditorInit
14 */
15 class GeneralCheckout extends CheckoutEditorBase {
16 /**
17 * Singleton Trait.
18 */
19 use SingletonTrait;
20
21 /**
22 * Notifications hooks.
23 */
24 private function __construct() {
25 parent::__construct();
26 add_filter( 'woocommerce_get_country_locale', [ $this, 'get_country_locale' ] );
27 add_filter( 'woocommerce_default_address_fields', [ $this, 'default_address_fields' ], 9999 );
28 add_filter( 'woocommerce_form_field_args', [ $this, 'customize_billing_state' ], 10, 3 ); // The hook will work for initial page load.
29 }
30 /**
31 * Customize the billing state field arguments on the initial page load.
32 *
33 * @param array $args The field arguments.
34 * @param string $key The field key.
35 * @param mixed $value The field value.
36 * @return array Modified field arguments.
37 */
38 public function customize_billing_state( $args, $key, $value ) {
39 if ( 'billing_state' !== $key ) {
40 return $args;
41 }
42 $args['label'] = $this->billing_settings['billing_state']['label'] ?? esc_html__( 'State / Region', 'shopbuilder' );
43 $args['placeholder'] = $this->billing_settings['billing_state']['placeholder'] ?? esc_html__( 'State / Region', 'shopbuilder' );
44 return $args;
45 }
46 /**
47 * @param array $fields fields.
48 * @return array
49 */
50 public function default_address_fields( $fields ) {
51 // address_1.
52 if ( ! empty( $fields['address_1'] ) ) {
53 if ( ! empty( $this->billing_settings['billing_address_1'] ) ) {
54 $fields['address_1'] = wp_parse_args( $this->billing_settings['billing_address_1'], $fields['address_1'] );
55 } elseif ( ! empty( $this->shipping_settings['shipping_address_1'] ) ) {
56 $fields['address_1'] = wp_parse_args( $this->shipping_settings['shipping_address_1'], $fields['address_1'] );
57 }
58 }
59 if ( ! empty( $fields['address_2'] ) ) {
60 // address_2.
61 if ( ! empty( $this->billing_settings['billing_address_2'] ) ) {
62 $fields['address_2'] = wp_parse_args( $this->billing_settings['billing_address_2'], $fields['address_2'] );
63 } elseif ( ! empty( $this->shipping_settings['shipping_address_2'] ) ) {
64 $fields['address_2'] = wp_parse_args( $this->shipping_settings['shipping_address_2'], $fields['address_2'] );
65 }
66 }
67 if ( ! empty( $fields['city'] ) ) {
68 // city.
69 if ( ! empty( $this->billing_settings['billing_city'] ) ) {
70 $fields['city'] = wp_parse_args( $this->billing_settings['billing_city'], $fields['city'] );
71 } elseif ( ! empty( $this->shipping_settings['shipping_city'] ) ) {
72 $fields['city'] = wp_parse_args( $this->shipping_settings['shipping_city'], $fields['city'] );
73 }
74 }
75 if ( ! empty( $fields['state'] ) ) {
76 // state.
77 if ( ! empty( $this->billing_settings['billing_state'] ) ) {
78 $fields['state'] = wp_parse_args( $this->billing_settings['billing_state'], $fields['state'] );
79 } elseif ( ! empty( $this->shipping_settings['shipping_state'] ) ) {
80 $fields['state'] = wp_parse_args( $this->shipping_settings['shipping_state'], $fields['state'] );
81 }
82 }
83 if ( ! empty( $fields['postcode'] ) ) {
84 // State.
85 if ( ! empty( $this->billing_settings['billing_postcode'] ) ) {
86 $fields['postcode'] = wp_parse_args( $this->billing_settings['billing_postcode'], $fields['postcode'] );
87 } elseif ( ! empty( $this->shipping_settings['shipping_postcode'] ) ) {
88 $fields['postcode'] = wp_parse_args( $this->shipping_settings['shipping_postcode'], $fields['postcode'] );
89 }
90 }
91 // Inject priority from saved settings order so WC's address-i18n.js does not re-sort locale-managed fields with core defaults.
92 $billing_keys = array_keys( $this->billing_settings );
93 $shipping_keys = array_keys( $this->shipping_settings );
94 foreach ( [ 'address_1', 'address_2', 'city', 'state', 'postcode' ] as $key ) {
95 if ( empty( $fields[ $key ] ) ) {
96 continue;
97 }
98 $billing_index = array_search( 'billing_' . $key, $billing_keys, true );
99 $shipping_index = array_search( 'shipping_' . $key, $shipping_keys, true );
100 if ( false !== $billing_index ) {
101 $fields[ $key ]['priority'] = ( absint( $billing_index ) + 1 ) * 10;
102 } elseif ( false !== $shipping_index ) {
103 $fields[ $key ]['priority'] = ( absint( $shipping_index ) + 1 ) * 10;
104 }
105 }
106 return $fields;
107 }
108
109 /**
110 * @param array $fields fields.
111 * @return array
112 */
113 public function country_locale( $fields ) {
114 // State.
115 if ( isset( $fields['postcode']['label'] ) ) {
116 if ( ! empty( $this->billing_settings['billing_postcode']['label'] ) ) {
117 $fields['postcode']['label'] = $this->billing_settings['billing_postcode']['label'];
118 } elseif ( ! empty( $this->shipping_settings['shipping_postcode']['label'] ) ) {
119 $fields['postcode']['label'] = $this->shipping_settings['shipping_postcode']['label'];
120 }
121 }
122 if ( isset( $fields['postcode']['required'] ) ) {
123 if ( isset( $this->billing_settings['billing_postcode']['required'] ) ) {
124 $fields['postcode']['required'] = $this->billing_settings['billing_postcode']['required'];
125 } elseif ( isset( $this->shipping_settings['shipping_postcode']['required'] ) ) {
126 $fields['postcode']['required'] = $this->shipping_settings['shipping_postcode']['required'];
127 }
128 }
129 // Billing state.
130 if ( isset( $fields['state']['label'] ) ) {
131 if ( ! empty( $this->billing_settings['billing_state'] ) ) {
132 $fields['state']['label'] = $this->billing_settings['billing_state']['label'] ?? $fields['postcode']['label'];
133 } elseif ( ! empty( $this->shipping_settings['shipping_state'] ) ) {
134 $fields['state']['label'] = $this->shipping_settings['shipping_state']['label'] ?? $fields['postcode']['label'];
135 }
136 }
137 if ( isset( $fields['state']['required'] ) ) {
138 if ( isset( $this->billing_settings['billing_state']['required'] ) ) {
139 $fields['state']['required'] = $this->billing_settings['billing_state']['required'];
140 } elseif ( isset( $this->shipping_settings['shipping_state']['required'] ) ) {
141 $fields['state']['required'] = $this->shipping_settings['shipping_state']['required'];
142 }
143 }
144
145 // Billing city.
146 if ( isset( $fields['city']['label'] ) ) {
147 if ( ! empty( $this->billing_settings['billing_city'] ) ) {
148 $fields['city']['label'] = $this->billing_settings['billing_city']['label'] ?? $fields['city']['label'];
149 } elseif ( ! empty( $this->shipping_settings['shipping_city'] ) ) {
150 $fields['city']['label'] = $this->shipping_settings['shipping_city'] ?? $fields['city']['label'];
151 }
152 }
153
154 if ( isset( $fields['city']['required'] ) ) {
155 if ( isset( $this->billing_settings['billing_city']['required'] ) ) {
156 $fields['city']['required'] = $this->billing_settings['billing_city']['required'];
157 } elseif ( isset( $this->shipping_settings['shipping_city']['required'] ) ) {
158 $fields['city']['required'] = $this->shipping_settings['shipping_city']['required'];
159 }
160 }
161
162 return $fields;
163 }
164
165 /**
166 * @param array $country local.
167 * @return array
168 */
169 public function get_country_locale( $country ) {
170 foreach ( $country as $key => $fields ) {
171 $country[ $key ] = $this->country_locale( $fields );
172 }
173 return $country;
174 }
175 }
176