| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Renderer; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\CustomerResource; |
| 6 |
use FluentCart\Api\Resource\FrontendResource\CustomerAddressResource; |
| 7 |
use FluentCart\App\App; |
| 8 |
use FluentCart\App\Helpers\Helper; |
| 9 |
use FluentCart\App\Services\Localization\LocalizationManager; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
|
| 12 |
class CheckoutFieldsSchema |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Per-request memo for getFieldsSettings(). In production every HTTP |
| 16 |
* request runs in a fresh PHP process, so this lives exactly one request. |
| 17 |
* Long-running processes that simulate multiple requests (test suites, |
| 18 |
* CLI) must clear it between simulated requests via |
| 19 |
* resetFieldsCache() — as a function-static it was |
| 20 |
* unreachable and leaked the first request's field settings into every |
| 21 |
* subsequent one. |
| 22 |
*/ |
| 23 |
private static $fieldsCache = null; |
| 24 |
|
| 25 |
public static function resetFieldsCache(): void |
| 26 |
{ |
| 27 |
self::$fieldsCache = null; |
| 28 |
} |
| 29 |
|
| 30 |
public static function titleMap(): array |
| 31 |
{ |
| 32 |
return [ |
| 33 |
'full_name' => __('Name', 'fluent-cart'), |
| 34 |
'first_name' => __('First Name', 'fluent-cart'), |
| 35 |
'last_name' => __('Last Name', 'fluent-cart'), |
| 36 |
'email' => __('Email', 'fluent-cart'), |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
public static function typeMap(): array |
| 41 |
{ |
| 42 |
return [ |
| 43 |
'full_name' => 'text', |
| 44 |
'first_name' => 'text', |
| 45 |
'last_name' => 'text', |
| 46 |
'email' => 'email', |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
private static function autocompleteMap(): array |
| 51 |
{ |
| 52 |
return [ |
| 53 |
'full_name' => 'name', |
| 54 |
'first_name' => 'given-name', |
| 55 |
'last_name' => 'family-name', |
| 56 |
'email' => 'email', |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
public static function getNameEmailFieldsSchema($cart = null, $scope = 'render') |
| 61 |
{ |
| 62 |
|
| 63 |
$fieldData = []; |
| 64 |
if ($cart && $scope === 'render') { |
| 65 |
$customerName = trim($cart->first_name . ' ' . $cart->last_name); |
| 66 |
$customerEmail = $cart->email; |
| 67 |
if (is_user_logged_in()) { |
| 68 |
$user = wp_get_current_user(); |
| 69 |
$customerName = trim($user->first_name . ' ' . $user->last_name); |
| 70 |
$fieldData['first_name'] = $user->first_name; |
| 71 |
$fieldData['last_name'] = $user->last_name; |
| 72 |
if (empty($customerName)) { |
| 73 |
$customerName = $user->display_name; |
| 74 |
} |
| 75 |
$customerEmail = $user->user_email; |
| 76 |
} |
| 77 |
|
| 78 |
$fieldData['full_name'] = $customerName; |
| 79 |
$fieldData['email'] = $customerEmail; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
$fieldsSchema = self::getFieldsSettings(); |
| 84 |
|
| 85 |
$titleMap = static::titleMap(); |
| 86 |
$autocompleteMap = static::autocompleteMap(); |
| 87 |
$nameFields = []; |
| 88 |
$infoFields = Arr::get($fieldsSchema, 'basic_info', []); |
| 89 |
foreach ($infoFields as $fieldKey => $basicField) { |
| 90 |
if (!isset($titleMap[$fieldKey])) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
if (Arr::get($basicField, 'enabled', 'no') !== 'yes') { |
| 94 |
continue; |
| 95 |
} |
| 96 |
$key = 'billing_' . $fieldKey; |
| 97 |
$isRequired = Arr::get($basicField, 'required', 'no') === 'yes' ? 'yes' : ''; |
| 98 |
$label = Arr::get($titleMap, $fieldKey); |
| 99 |
$nameFields[$key] = [ |
| 100 |
'name' => $key, |
| 101 |
'id' => $key, |
| 102 |
'type' => 'text', |
| 103 |
'data-type' => 'text', |
| 104 |
'required' => $isRequired, |
| 105 |
'label' => '', |
| 106 |
'aria-label' => $label, |
| 107 |
'placeholder' => $label . ($isRequired ? ' *' : ''), |
| 108 |
'autocomplete' => isset($autocompleteMap[$fieldKey]) ? $autocompleteMap[$fieldKey] : 'on', |
| 109 |
'value' => Arr::get($fieldData, $fieldKey) |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
$nameFields = apply_filters('fluent_cart/checkout_page_name_fields_schema', $nameFields, [ |
| 114 |
'cart' => $cart, |
| 115 |
'scope' => $scope |
| 116 |
]); |
| 117 |
|
| 118 |
|
| 119 |
return [ |
| 120 |
'type' => 'section', |
| 121 |
'title' => '', |
| 122 |
'id' => 'billing_personal_information_section', |
| 123 |
'fields' => $nameFields, |
| 124 |
]; |
| 125 |
} |
| 126 |
|
| 127 |
public static function getAddressBaseFields($config, $scope = 'render') |
| 128 |
{ |
| 129 |
|
| 130 |
$configDefaults = [ |
| 131 |
'type' => 'billing', // billing or shipping |
| 132 |
'product_type' => 'digital', // digital or physical |
| 133 |
'with_shipping' => false, // only for billing type, if true and type is billing, then shipping fields will be returned without full_name field |
| 134 |
'full_name' => '', |
| 135 |
'country' => '', |
| 136 |
'address_1' => '', |
| 137 |
'address_2' => '', |
| 138 |
'state' => '', |
| 139 |
'city' => '', |
| 140 |
'postcode' => '', |
| 141 |
'company_name' => '', |
| 142 |
'phone' => '', |
| 143 |
]; |
| 144 |
|
| 145 |
$config = wp_parse_args($config, $configDefaults); |
| 146 |
|
| 147 |
$selectedCountry = Arr::get($config, 'country'); |
| 148 |
if (empty($selectedCountry)) { |
| 149 |
$HTTP_CF_IP_COUNTRY = Arr::get(App::request()->server(), 'HTTP_CF_IPCOUNTRY'); |
| 150 |
$selectedCountry = $HTTP_CF_IP_COUNTRY ?? $selectedCountry; |
| 151 |
} |
| 152 |
|
| 153 |
$states = []; |
| 154 |
if (empty($config['state']) || empty($selectedCountry)) { |
| 155 |
$states = [ |
| 156 |
[ |
| 157 |
'name' => __('Select an option', 'fluent-cart'), |
| 158 |
'value' => '' |
| 159 |
] |
| 160 |
]; |
| 161 |
} |
| 162 |
|
| 163 |
$addressLocale = []; |
| 164 |
if (!empty($selectedCountry)) { |
| 165 |
$states = array_merge($states, LocalizationManager::getInstance()->statesOptions($selectedCountry)); |
| 166 |
$addressLocale = LocalizationManager::getInstance()->addressLocales($selectedCountry); |
| 167 |
} |
| 168 |
|
| 169 |
$stateLabel = Arr::get($addressLocale, 'state.label', __('State', 'fluent-cart')); |
| 170 |
$countries = [ |
| 171 |
[ |
| 172 |
'name' => __('Select a Country', 'fluent-cart'), |
| 173 |
'value' => '' |
| 174 |
] |
| 175 |
]; |
| 176 |
|
| 177 |
$countries = array_merge($countries, Helper::getCountryList()); |
| 178 |
|
| 179 |
$type = Arr::get($config, 'type', 'billing'); // billing or shipping |
| 180 |
|
| 181 |
if (!in_array($type, ['billing', 'shipping'])) { |
| 182 |
$type = 'billing'; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
if (empty($states) && !Arr::get($addressLocale, 'state.hidden')) { |
| 187 |
$stateInput = [ |
| 188 |
'name' => $type . '_state', |
| 189 |
'id' => $type . '_state', |
| 190 |
'type' => 'text', |
| 191 |
'data-type' => 'text', |
| 192 |
'label' => '', |
| 193 |
'required' => 'yes', |
| 194 |
'autocomplete' => 'address-level2', |
| 195 |
'placeholder' => $stateLabel, |
| 196 |
'aria-label' => $stateLabel, |
| 197 |
'value' => $config['state'], |
| 198 |
'wrapper_atts' => [ |
| 199 |
'id' => $type . '_state_wrapper' |
| 200 |
] |
| 201 |
]; |
| 202 |
} else { |
| 203 |
$stateInput = [ |
| 204 |
'name' => $type . '_state', |
| 205 |
'id' => $type . '_state', |
| 206 |
'type' => 'select', |
| 207 |
'data-type' => 'select', |
| 208 |
'label' => '', |
| 209 |
'options' => $states, |
| 210 |
'required' => 'yes', |
| 211 |
'autocomplete' => 'address-level2', |
| 212 |
'placeholder' => $stateLabel, |
| 213 |
'aria-label' => $stateLabel, |
| 214 |
'value' => $config['state'], |
| 215 |
'wrapper_atts' => [ |
| 216 |
'id' => $type . '_state_wrapper' |
| 217 |
] |
| 218 |
]; |
| 219 |
} |
| 220 |
|
| 221 |
$fields = [ |
| 222 |
'full_name' => [ |
| 223 |
'id' => $type . '_full_name', |
| 224 |
'name' => $type . '_full_name', |
| 225 |
'type' => 'text', |
| 226 |
'data-type' => 'text', |
| 227 |
'label' => '', |
| 228 |
'is_core' => 'yes', |
| 229 |
'required' => 'yes', |
| 230 |
'autocomplete' => 'given-name', |
| 231 |
'value' => $config['full_name'], |
| 232 |
'placeholder' => esc_attr__('Full Name', 'fluent-cart'), |
| 233 |
'aria-label' => esc_attr__('Your Full Name', 'fluent-cart'), |
| 234 |
], |
| 235 |
'country' => [ |
| 236 |
'name' => $type . '_country', |
| 237 |
'id' => $type . '_country', |
| 238 |
'type' => 'select', |
| 239 |
'options' => $countries, |
| 240 |
'data-type' => 'text', |
| 241 |
'label' => '', |
| 242 |
'aria-label' => esc_attr__('Country / Region', 'fluent-cart'), |
| 243 |
'required' => 'yes', |
| 244 |
'autocomplete' => 'country', |
| 245 |
'placeholder' => esc_attr__('Country / Region', 'fluent-cart'), |
| 246 |
'value' => $selectedCountry, |
| 247 |
], |
| 248 |
'address_1' => [ |
| 249 |
'name' => $type . '_address_1', |
| 250 |
'id' => $type . '_address_1', |
| 251 |
'type' => 'text', |
| 252 |
'data-type' => 'text', |
| 253 |
'label' => '', |
| 254 |
'aria-label' => esc_attr__('Street Address', 'fluent-cart'), |
| 255 |
/* translators: use local order of street name and house number. */ |
| 256 |
'placeholder' => esc_attr__('Street Address', 'fluent-cart'), |
| 257 |
'required' => 'yes', |
| 258 |
'autocomplete' => 'address-line1', |
| 259 |
'value' => $config['address_1'], |
| 260 |
], |
| 261 |
'address_2' => [ |
| 262 |
'name' => $type . '_address_2', |
| 263 |
'id' => $type . '_address_2', |
| 264 |
'type' => 'text', |
| 265 |
'data-type' => 'text', |
| 266 |
'label' => '', |
| 267 |
'aria-label' => esc_attr__('Apt, suite, unit', 'fluent-cart'), |
| 268 |
'label_class' => array(''), |
| 269 |
'placeholder' => esc_attr__('Apt, suite, unit', 'fluent-cart'), |
| 270 |
'autocomplete' => 'address-line2', |
| 271 |
'value' => $config['address_2'], |
| 272 |
], |
| 273 |
'state' => $stateInput, |
| 274 |
'city' => [ |
| 275 |
'name' => $type . '_city', |
| 276 |
'id' => $type . '_city', |
| 277 |
'type' => 'text', |
| 278 |
'data-type' => 'text', |
| 279 |
'label' => '', |
| 280 |
'aria-label' => esc_attr__('Town / City', 'fluent-cart'), |
| 281 |
'required' => 'yes', |
| 282 |
'autocomplete' => 'address-level2', |
| 283 |
'placeholder' => esc_attr__('Town / City', 'fluent-cart'), |
| 284 |
'value' => $config['city'], |
| 285 |
], |
| 286 |
'postcode' => [ |
| 287 |
'name' => $type . '_postcode', |
| 288 |
'id' => $type . '_postcode', |
| 289 |
'type' => 'text', |
| 290 |
'data-type' => 'text', |
| 291 |
'label' => '', |
| 292 |
'aria-label' => esc_attr__('Postal / ZIP Code', 'fluent-cart'), |
| 293 |
'required' => 'yes', |
| 294 |
'validate' => array('postcode'), |
| 295 |
'autocomplete' => 'postal-code', |
| 296 |
'placeholder' => esc_attr__('Postcode / ZIP', 'fluent-cart'), |
| 297 |
'value' => $config['postcode'], |
| 298 |
], |
| 299 |
'phone' => [ |
| 300 |
'name' => $type . '_phone', |
| 301 |
'id' => $type . '_phone', |
| 302 |
'type' => 'tel', |
| 303 |
'data-type' => 'text', |
| 304 |
'label' => '', |
| 305 |
'aria-label' => esc_attr__('Phone number', 'fluent-cart'), |
| 306 |
'placeholder' => esc_attr__('Phone number', 'fluent-cart'), |
| 307 |
'autocomplete' => 'tel', |
| 308 |
'value' => $config['phone'], |
| 309 |
], |
| 310 |
'company_name' => [ |
| 311 |
'name' => $type . '_company_name', |
| 312 |
'id' => $type . '_company_name', |
| 313 |
'type' => 'text', |
| 314 |
'data-type' => 'text', |
| 315 |
'label' => '', |
| 316 |
'aria-label' => esc_attr__('Company name', 'fluent-cart'), |
| 317 |
'placeholder' => esc_attr__('Company name', 'fluent-cart'), |
| 318 |
'autocomplete' => 'organization', |
| 319 |
'value' => $config['company_name'], |
| 320 |
], |
| 321 |
]; |
| 322 |
|
| 323 |
if ($type === 'billing') { |
| 324 |
unset($fields['full_name']); |
| 325 |
} |
| 326 |
|
| 327 |
$requirementsFields = []; |
| 328 |
if ($scope === 'render') { |
| 329 |
$requirementsFields = self::getCheckoutFieldsRequirements($type, $config['product_type'], $config['with_shipping']); |
| 330 |
|
| 331 |
$formattedFields = []; |
| 332 |
foreach ($fields as $key => $field) { |
| 333 |
$requirement = Arr::get($requirementsFields, $key, ''); |
| 334 |
if (!$requirement) { |
| 335 |
continue; |
| 336 |
} |
| 337 |
if ($requirement === 'required') { |
| 338 |
$field['required'] = 'yes'; |
| 339 |
$field['placeholder'] = $field['placeholder'] . ' *'; |
| 340 |
} else { |
| 341 |
unset($field['required']); |
| 342 |
} |
| 343 |
$formattedFields[$key] = $field; |
| 344 |
} |
| 345 |
$fields = $formattedFields; |
| 346 |
} |
| 347 |
|
| 348 |
return apply_filters('fluent_cart/fields/address_base_fields', $fields, [ |
| 349 |
'config' => $config, |
| 350 |
'scope' => $scope, |
| 351 |
'requirements' => $requirementsFields |
| 352 |
]); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
private static function getRequirementType($config, $key) |
| 357 |
{ |
| 358 |
$enabled = Arr::get($config, $key . '.enabled', 'no') === 'yes'; |
| 359 |
if (!$enabled) { |
| 360 |
return ''; |
| 361 |
} |
| 362 |
|
| 363 |
if (Arr::get($config, $key . '.required', 'no') === 'yes') { |
| 364 |
return 'required'; |
| 365 |
} |
| 366 |
|
| 367 |
return 'optional'; |
| 368 |
} |
| 369 |
|
| 370 |
public static function getCheckoutFieldsRequirements($addressType = 'billing', $productType = 'digital', $withShipping = false) // digital or physical |
| 371 |
{ |
| 372 |
$fieldsConfig = self::getFieldsSettings(); |
| 373 |
|
| 374 |
|
| 375 |
$shippingConfig = Arr::get($fieldsConfig, 'shipping_address', []); |
| 376 |
$billingConfig = Arr::get($fieldsConfig, 'billing_address', []); |
| 377 |
$businessConfig = Arr::get($fieldsConfig, 'business_details', []); |
| 378 |
$basicConfig = Arr::get($fieldsConfig, 'basic_info', []); |
| 379 |
$basicSchema = []; |
| 380 |
|
| 381 |
if (static::isFullNameRequired()) { |
| 382 |
$basicSchema = [ |
| 383 |
'full_name' => [ |
| 384 |
'billing' => '', |
| 385 |
'shipping' => 'required' |
| 386 |
], |
| 387 |
]; |
| 388 |
} else { |
| 389 |
$basicSchema = [ |
| 390 |
'first_name' => [ |
| 391 |
'billing' => 'required', |
| 392 |
'shipping' => 'required' |
| 393 |
], |
| 394 |
]; |
| 395 |
|
| 396 |
if (static::isLastNameEnabled()) { |
| 397 |
$basicSchema['last_name'] = [ |
| 398 |
'billing' => static::isLastNameRequired() ? 'required' : '', |
| 399 |
'shipping' => static::isLastNameRequired() ? 'required' : '' |
| 400 |
]; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
$dataConfig = [ |
| 405 |
'country' => [ |
| 406 |
'billing' => self::getRequirementType($billingConfig, 'country'), |
| 407 |
'shipping' => self::getRequirementType($shippingConfig, 'country') |
| 408 |
], |
| 409 |
'address_1' => [ |
| 410 |
'billing' => self::getRequirementType($billingConfig, 'address_1'), |
| 411 |
'shipping' => self::getRequirementType($shippingConfig, 'address_1') |
| 412 |
], |
| 413 |
'address_2' => [ |
| 414 |
'billing' => self::getRequirementType($billingConfig, 'address_2'), |
| 415 |
'shipping' => self::getRequirementType($shippingConfig, 'address_2') |
| 416 |
], |
| 417 |
'state' => [ |
| 418 |
'billing' => self::getRequirementType($billingConfig, 'state'), |
| 419 |
'shipping' => self::getRequirementType($shippingConfig, 'state') |
| 420 |
], |
| 421 |
'city' => [ |
| 422 |
'billing' => self::getRequirementType($billingConfig, 'city'), |
| 423 |
'shipping' => self::getRequirementType($shippingConfig, 'city') |
| 424 |
], |
| 425 |
'postcode' => [ |
| 426 |
'billing' => self::getRequirementType($billingConfig, 'postcode'), |
| 427 |
'shipping' => self::getRequirementType($shippingConfig, 'postcode') |
| 428 |
], |
| 429 |
'vat_number' => [ |
| 430 |
'billing' => self::getRequirementType($businessConfig, 'vat_number'), |
| 431 |
'shipping' => '' |
| 432 |
], |
| 433 |
'phone' => [ |
| 434 |
'billing' => self::getRequirementType($billingConfig, 'phone'), |
| 435 |
'shipping' => self::getRequirementType($shippingConfig, 'phone') |
| 436 |
] |
| 437 |
]; |
| 438 |
$dataConfig = array_merge( |
| 439 |
$basicSchema, |
| 440 |
$dataConfig |
| 441 |
); |
| 442 |
|
| 443 |
|
| 444 |
if ($withShipping && $addressType == 'billing') { |
| 445 |
// now we have to merge the shipping fields requirements with billing fields requirements |
| 446 |
foreach ($dataConfig as $key => $config) { |
| 447 |
if ($config[$addressType] === 'required') { |
| 448 |
$dataConfig[$key][$addressType] = 'required'; |
| 449 |
} elseif ($config[$addressType] === 'optional' && $dataConfig[$key][$addressType] !== 'required') { |
| 450 |
$dataConfig[$key][$addressType] = 'optional'; |
| 451 |
} else { |
| 452 |
$dataConfig[$key][$addressType] = ''; |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
$formattedReturn = []; |
| 458 |
foreach ($dataConfig as $key => $config) { |
| 459 |
if (Arr::get($config, $addressType)) { |
| 460 |
$formattedReturn[$key] = $config[$addressType]; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
return $formattedReturn; |
| 465 |
} |
| 466 |
|
| 467 |
public static function getFieldsSchemaConfig() |
| 468 |
{ |
| 469 |
return [ |
| 470 |
'basic_info' => [ |
| 471 |
'full_name' => [ |
| 472 |
'label' => __('Full Name', 'fluent-cart'), |
| 473 |
'help_text' => __('Enabling First/Last name will replace the default Full Name.', 'fluent-cart'), |
| 474 |
'type' => 'text', |
| 475 |
'can_alter' => 'no', |
| 476 |
], |
| 477 |
'first_name' => [ |
| 478 |
'label' => __('First Name', 'fluent-cart'), |
| 479 |
'type' => 'text', |
| 480 |
'can_alter' => 'yes', |
| 481 |
], |
| 482 |
'last_name' => [ |
| 483 |
'label' => __('Last Name', 'fluent-cart'), |
| 484 |
'type' => 'text', |
| 485 |
'can_alter' => 'yes', |
| 486 |
], |
| 487 |
'email' => [ |
| 488 |
'label' => __('Email Address', 'fluent-cart'), |
| 489 |
'type' => 'email', |
| 490 |
'can_alter' => 'no', |
| 491 |
], |
| 492 |
], |
| 493 |
'business_details' => [ |
| 494 |
'company_name' => [ |
| 495 |
'label' => __('Company', 'fluent-cart'), |
| 496 |
'type' => 'text', |
| 497 |
'can_alter' => 'yes', |
| 498 |
], |
| 499 |
'vat_number' => [ |
| 500 |
'label' => __('VAT/GST/Tax Number', 'fluent-cart'), |
| 501 |
'help_text' => __('Show a VAT/GST/Tax number field at checkout for customers to provide their tax ID.', 'fluent-cart'), |
| 502 |
'type' => 'text', |
| 503 |
'can_alter' => 'yes', |
| 504 |
], |
| 505 |
'legal_registration_id' => [ |
| 506 |
'label' => __('Legal Registration ID', 'fluent-cart'), |
| 507 |
'help_text' => __('Show a legal registration / business ID field at checkout (e.g. company registration number).', 'fluent-cart'), |
| 508 |
'type' => 'text', |
| 509 |
'can_alter' => 'yes', |
| 510 |
], |
| 511 |
'b2b_only_mode' => [ |
| 512 |
'label' => __('B2B Only Mode', 'fluent-cart'), |
| 513 |
'help_text' => __('When enabled, the B2B purchase toggle is hidden and business fields are always shown to all customers.', 'fluent-cart'), |
| 514 |
'type' => 'checkbox', |
| 515 |
'can_alter' => 'yes', |
| 516 |
], |
| 517 |
], |
| 518 |
'billing_address' => [ |
| 519 |
'country' => [ |
| 520 |
'label' => __('Country', 'fluent-cart'), |
| 521 |
'type' => 'text', |
| 522 |
'can_alter' => 'yes', |
| 523 |
], |
| 524 |
'state' => [ |
| 525 |
'label' => __('State', 'fluent-cart'), |
| 526 |
'type' => 'text', |
| 527 |
'can_alter' => 'yes', |
| 528 |
], |
| 529 |
'address_1' => [ |
| 530 |
'label' => __('Street Address', 'fluent-cart'), |
| 531 |
'type' => 'text', |
| 532 |
'can_alter' => 'yes', |
| 533 |
], |
| 534 |
'address_2' => [ |
| 535 |
'label' => __('Apt, suite, unit', 'fluent-cart'), |
| 536 |
'type' => 'text', |
| 537 |
'can_alter' => 'yes', |
| 538 |
], |
| 539 |
'city' => [ |
| 540 |
'label' => __('City', 'fluent-cart'), |
| 541 |
'type' => 'text', |
| 542 |
'can_alter' => 'yes', |
| 543 |
], |
| 544 |
'postcode' => [ |
| 545 |
'label' => __('Post Code', 'fluent-cart'), |
| 546 |
'type' => 'text', |
| 547 |
'can_alter' => 'yes', |
| 548 |
], |
| 549 |
'phone' => [ |
| 550 |
'label' => __('Phone', 'fluent-cart'), |
| 551 |
'type' => 'text', |
| 552 |
'can_alter' => 'yes', |
| 553 |
] |
| 554 |
], |
| 555 |
'shipping_address' => [ |
| 556 |
'full_name' => [ |
| 557 |
'label' => __('Full Name', 'fluent-cart'), |
| 558 |
'type' => 'text', |
| 559 |
'can_alter' => 'no', |
| 560 |
], |
| 561 |
'country' => [ |
| 562 |
'label' => __('Country', 'fluent-cart'), |
| 563 |
'type' => 'text', |
| 564 |
'can_alter' => 'yes', |
| 565 |
], |
| 566 |
'state' => [ |
| 567 |
'label' => __('State', 'fluent-cart'), |
| 568 |
'type' => 'text', |
| 569 |
'can_alter' => 'yes', |
| 570 |
], |
| 571 |
'address_1' => [ |
| 572 |
'label' => __('Street Address', 'fluent-cart'), |
| 573 |
'type' => 'text', |
| 574 |
'can_alter' => 'yes', |
| 575 |
], |
| 576 |
'address_2' => [ |
| 577 |
'label' => __('Apt, suite, unit', 'fluent-cart'), |
| 578 |
'type' => 'text', |
| 579 |
'can_alter' => 'yes', |
| 580 |
], |
| 581 |
'city' => [ |
| 582 |
'label' => __('City', 'fluent-cart'), |
| 583 |
'type' => 'text', |
| 584 |
'can_alter' => 'yes', |
| 585 |
], |
| 586 |
'postcode' => [ |
| 587 |
'label' => __('Post Code', 'fluent-cart'), |
| 588 |
'type' => 'text', |
| 589 |
'can_alter' => 'yes', |
| 590 |
], |
| 591 |
'phone' => [ |
| 592 |
'label' => __('Phone', 'fluent-cart'), |
| 593 |
'type' => 'text', |
| 594 |
'can_alter' => 'yes', |
| 595 |
] |
| 596 |
], |
| 597 |
'agree_terms' => [ |
| 598 |
'label' => __('Agree Terms and Conditions', 'fluent-cart'), |
| 599 |
'type' => 'checkbox', |
| 600 |
'can_alter' => 'yes', |
| 601 |
] |
| 602 |
]; |
| 603 |
} |
| 604 |
|
| 605 |
public static function getFieldsSettings() |
| 606 |
{ |
| 607 |
if (self::$fieldsCache !== null) { |
| 608 |
return self::$fieldsCache; |
| 609 |
} |
| 610 |
|
| 611 |
$defaults = [ |
| 612 |
'basic_info' => [ |
| 613 |
'full_name' => [ |
| 614 |
'required' => 'yes', |
| 615 |
'enabled' => 'yes' |
| 616 |
], |
| 617 |
'first_name' => [ |
| 618 |
'required' => 'no', |
| 619 |
'enabled' => 'no' |
| 620 |
], |
| 621 |
'last_name' => [ |
| 622 |
'required' => 'no', |
| 623 |
'enabled' => 'no' |
| 624 |
], |
| 625 |
'email' => [ |
| 626 |
'required' => 'yes', |
| 627 |
'enabled' => 'yes' |
| 628 |
], |
| 629 |
], |
| 630 |
'business_details' => [ |
| 631 |
'company_name' => [ |
| 632 |
'required' => 'no', |
| 633 |
'enabled' => 'no' |
| 634 |
], |
| 635 |
'vat_number' => [ |
| 636 |
'required' => 'no', |
| 637 |
'enabled' => 'no' |
| 638 |
], |
| 639 |
'legal_registration_id' => [ |
| 640 |
'required' => 'no', |
| 641 |
'enabled' => 'no' |
| 642 |
], |
| 643 |
'b2b_only_mode' => [ |
| 644 |
'enabled' => 'no', |
| 645 |
], |
| 646 |
], |
| 647 |
'billing_address' => [ |
| 648 |
'country' => [ |
| 649 |
'required' => 'yes', |
| 650 |
'enabled' => 'yes' |
| 651 |
], |
| 652 |
'state' => [ |
| 653 |
'required' => 'yes', |
| 654 |
'enabled' => 'yes' |
| 655 |
], |
| 656 |
'address_1' => [ |
| 657 |
'required' => 'yes', |
| 658 |
'enabled' => 'yes' |
| 659 |
], |
| 660 |
'address_2' => [ |
| 661 |
'required' => 'no', |
| 662 |
'enabled' => 'yes' |
| 663 |
], |
| 664 |
'city' => [ |
| 665 |
'required' => 'yes', |
| 666 |
'enabled' => 'yes' |
| 667 |
], |
| 668 |
'postcode' => [ |
| 669 |
'required' => 'yes', |
| 670 |
'enabled' => 'yes' |
| 671 |
], |
| 672 |
'phone' => [ |
| 673 |
'required' => 'no', |
| 674 |
'enabled' => 'no' |
| 675 |
], |
| 676 |
'company_name' => [ |
| 677 |
'required' => 'no', |
| 678 |
'enabled' => 'no' |
| 679 |
] |
| 680 |
], |
| 681 |
'shipping_address' => [ |
| 682 |
'full_name' => [ |
| 683 |
'required' => 'yes', |
| 684 |
'enabled' => 'yes', |
| 685 |
], |
| 686 |
'country' => [ |
| 687 |
'required' => 'yes', |
| 688 |
'enabled' => 'yes' |
| 689 |
], |
| 690 |
'state' => [ |
| 691 |
'required' => 'yes', |
| 692 |
'enabled' => 'yes' |
| 693 |
], |
| 694 |
'address_1' => [ |
| 695 |
'required' => 'yes', |
| 696 |
'enabled' => 'yes' |
| 697 |
], |
| 698 |
'address_2' => [ |
| 699 |
'required' => 'no', |
| 700 |
'enabled' => 'yes' |
| 701 |
], |
| 702 |
'city' => [ |
| 703 |
'required' => 'yes', |
| 704 |
'enabled' => 'yes' |
| 705 |
], |
| 706 |
'postcode' => [ |
| 707 |
'required' => 'yes', |
| 708 |
'enabled' => 'yes' |
| 709 |
], |
| 710 |
'phone' => [ |
| 711 |
'required' => 'no', |
| 712 |
'enabled' => 'no' |
| 713 |
], |
| 714 |
'company_name' => [ |
| 715 |
'required' => 'no', |
| 716 |
'enabled' => 'no' |
| 717 |
] |
| 718 |
], |
| 719 |
'agree_terms' => [ |
| 720 |
'required' => 'no', |
| 721 |
'enabled' => 'no', |
| 722 |
'text' => '' |
| 723 |
] |
| 724 |
]; |
| 725 |
|
| 726 |
$savedConfig = fluent_cart_get_option('_fc_checkout_fields', []); |
| 727 |
|
| 728 |
if (!$savedConfig || !is_array($savedConfig)) { |
| 729 |
return $defaults; |
| 730 |
} |
| 731 |
|
| 732 |
// Migrate company_name and vat_number from the old basic_info location for existing stores |
| 733 |
if (empty($savedConfig['business_details']) && !empty($savedConfig['basic_info'])) { |
| 734 |
if (!empty($savedConfig['basic_info']['company_name'])) { |
| 735 |
$savedConfig['business_details']['company_name'] = $savedConfig['basic_info']['company_name']; |
| 736 |
} |
| 737 |
if (!empty($savedConfig['basic_info']['vat_number'])) { |
| 738 |
$savedConfig['business_details']['vat_number'] = $savedConfig['basic_info']['vat_number']; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
$groupFields = ['basic_info', 'billing_address', 'shipping_address', 'business_details']; |
| 743 |
|
| 744 |
foreach ($defaults as $key => $default) { |
| 745 |
$savedValues = Arr::get($savedConfig, $key, []); |
| 746 |
if (in_array($key, $groupFields)) { |
| 747 |
$savedValues = Arr::only($savedValues, array_keys($default)); |
| 748 |
} |
| 749 |
|
| 750 |
$defaults[$key] = wp_parse_args($savedValues, $default); |
| 751 |
} |
| 752 |
|
| 753 |
return (self::$fieldsCache = $defaults); |
| 754 |
} |
| 755 |
|
| 756 |
public static function isTermsRequired(): bool |
| 757 |
{ |
| 758 |
$fieldSettings = self::getFieldsSettings(); |
| 759 |
return Arr::get($fieldSettings, 'agree_terms.enabled') === 'yes' |
| 760 |
&& Arr::get($fieldSettings, 'agree_terms.required') === 'yes'; |
| 761 |
} |
| 762 |
|
| 763 |
public static function isTermsVisible(): bool |
| 764 |
{ |
| 765 |
$fieldSettings = self::getFieldsSettings(); |
| 766 |
return Arr::get($fieldSettings, 'agree_terms.enabled') === 'yes'; |
| 767 |
} |
| 768 |
|
| 769 |
public static function getTermsText() |
| 770 |
{ |
| 771 |
$text = Arr::get(self::getFieldsSettings(), 'agree_terms.text', ''); |
| 772 |
|
| 773 |
if (!$text) { |
| 774 |
$text = __('I agree to the terms and conditions.', 'fluent-cart'); |
| 775 |
} |
| 776 |
|
| 777 |
return $text; |
| 778 |
} |
| 779 |
|
| 780 |
public static function isFirstNameEnabled(): bool |
| 781 |
{ |
| 782 |
$fieldsSchema = self::getFieldsSettings(); |
| 783 |
return Arr::get($fieldsSchema, 'basic_info.first_name.enabled') === 'yes'; |
| 784 |
|
| 785 |
} |
| 786 |
|
| 787 |
public static function isLastNameEnabled(): bool |
| 788 |
{ |
| 789 |
$fieldsSchema = self::getFieldsSettings(); |
| 790 |
return Arr::get($fieldsSchema, 'basic_info.last_name.enabled') === 'yes'; |
| 791 |
} |
| 792 |
|
| 793 |
public static function isLastNameRequired(): bool |
| 794 |
{ |
| 795 |
$fieldsSchema = self::getFieldsSettings(); |
| 796 |
return Arr::get($fieldsSchema, 'basic_info.last_name.required') === 'yes'; |
| 797 |
} |
| 798 |
|
| 799 |
public static function isFullNameRequired(): bool |
| 800 |
{ |
| 801 |
|
| 802 |
$isFirstNameEnabled = static::isFirstNameEnabled(); |
| 803 |
$isLastNameEnabled = static::isLastNameEnabled(); |
| 804 |
return !($isFirstNameEnabled || $isLastNameEnabled); |
| 805 |
} |
| 806 |
|
| 807 |
public static function isVatNumberEnabled(): bool |
| 808 |
{ |
| 809 |
$fieldSettings = self::getFieldsSettings(); |
| 810 |
return Arr::get($fieldSettings, 'business_details.vat_number.enabled') === 'yes'; |
| 811 |
} |
| 812 |
|
| 813 |
public static function isCompanyNameEnabled(): bool |
| 814 |
{ |
| 815 |
$fieldSettings = self::getFieldsSettings(); |
| 816 |
return Arr::get($fieldSettings, 'business_details.company_name.enabled') === 'yes'; |
| 817 |
} |
| 818 |
|
| 819 |
public static function isCompanyNameRequired(): bool |
| 820 |
{ |
| 821 |
$fieldSettings = self::getFieldsSettings(); |
| 822 |
return Arr::get($fieldSettings, 'business_details.company_name.enabled') === 'yes' |
| 823 |
&& Arr::get($fieldSettings, 'business_details.company_name.required') === 'yes'; |
| 824 |
} |
| 825 |
|
| 826 |
public static function isVatNumberRequired(): bool |
| 827 |
{ |
| 828 |
$fieldSettings = self::getFieldsSettings(); |
| 829 |
return Arr::get($fieldSettings, 'business_details.vat_number.enabled') === 'yes' |
| 830 |
&& Arr::get($fieldSettings, 'business_details.vat_number.required') === 'yes'; |
| 831 |
} |
| 832 |
|
| 833 |
public static function isLegalRegistrationIdEnabled(): bool |
| 834 |
{ |
| 835 |
$fieldSettings = self::getFieldsSettings(); |
| 836 |
return Arr::get($fieldSettings, 'business_details.legal_registration_id.enabled') === 'yes'; |
| 837 |
} |
| 838 |
|
| 839 |
public static function isLegalRegistrationIdRequired(): bool |
| 840 |
{ |
| 841 |
$fieldSettings = self::getFieldsSettings(); |
| 842 |
return Arr::get($fieldSettings, 'business_details.legal_registration_id.enabled') === 'yes' |
| 843 |
&& Arr::get($fieldSettings, 'business_details.legal_registration_id.required') === 'yes'; |
| 844 |
} |
| 845 |
|
| 846 |
public static function isB2BOnlyMode(): bool |
| 847 |
{ |
| 848 |
$fieldSettings = self::getFieldsSettings(); |
| 849 |
return Arr::get($fieldSettings, 'business_details.b2b_only_mode.enabled') === 'yes'; |
| 850 |
} |
| 851 |
|
| 852 |
public static function hasAnyBusinessFields(): bool |
| 853 |
{ |
| 854 |
return static::isCompanyNameEnabled() |
| 855 |
|| static::isVatNumberEnabled() |
| 856 |
|| static::isLegalRegistrationIdEnabled(); |
| 857 |
} |
| 858 |
} |
| 859 |
|