| @@ -6,8 +6,9 @@ | ||
| 6 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | 7 | exit( 'This script cannot be accessed directly.' ); |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | +use PhpParser\Node\Expr\Empty_; | |
| 10 | 11 | use RadiusTheme\SB\Traits\SingletonTrait; |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * CheckoutEditorInit |
| @@ -24,85 +25,43 @@ | ||
| 24 | 25 | private function __construct() { |
| 25 | 26 | parent::__construct(); |
| 26 | 27 | add_filter( 'woocommerce_get_country_locale', [ $this, 'get_country_locale' ] ); |
| 27 | 28 | 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 | 29 | } |
| 30 | 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 | 31 | * @param array $fields fields. |
| 48 | 32 | * @return array |
| 49 | 33 | */ |
| 50 | 34 | public function default_address_fields( $fields ) { |
| 51 | 35 | // 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 | - } | |
| 36 | + if ( ! empty( $this->billing_settings['billing_address_1'] ) ) { | |
| 37 | + $fields['address_1'] = wp_parse_args( $this->billing_settings['billing_address_1'], $fields['address_1'] ); | |
| 38 | + } elseif ( ! empty( $this->shipping_settings['shipping_address_1'] ) ) { | |
| 39 | + $fields['address_1'] = wp_parse_args( $this->shipping_settings['shipping_address_1'], $fields['address_1'] ); | |
| 58 | 40 | } |
| 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 | - } | |
| 41 | + // address_2. | |
| 42 | + if ( ! empty( $this->billing_settings['billing_address_2'] ) ) { | |
| 43 | + $fields['address_2'] = wp_parse_args( $this->billing_settings['billing_address_2'], $fields['address_2'] ); | |
| 44 | + } elseif ( ! empty( $this->shipping_settings['shipping_address_2'] ) ) { | |
| 45 | + $fields['address_2'] = wp_parse_args( $this->shipping_settings['shipping_address_2'], $fields['address_2'] ); | |
| 66 | 46 | } |
| 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 | - } | |
| 47 | + // city. | |
| 48 | + if ( ! empty( $this->billing_settings['billing_city'] ) ) { | |
| 49 | + $fields['city'] = wp_parse_args( $this->billing_settings['billing_city'], $fields['city'] ); | |
| 50 | + } elseif ( ! empty( $this->shipping_settings['shipping_city'] ) ) { | |
| 51 | + $fields['city'] = wp_parse_args( $this->shipping_settings['shipping_city'], $fields['city'] ); | |
| 74 | 52 | } |
| 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 | - } | |
| 53 | + // state. | |
| 54 | + if ( ! empty( $this->billing_settings['billing_state'] ) ) { | |
| 55 | + $fields['state'] = wp_parse_args( $this->billing_settings['billing_state'], $fields['state'] ); | |
| 56 | + } elseif ( ! empty( $this->shipping_settings['shipping_state'] ) ) { | |
| 57 | + $fields['state'] = wp_parse_args( $this->shipping_settings['shipping_state'], $fields['state'] ); | |
| 82 | 58 | } |
| 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 | - } | |
| 59 | + // State. | |
| 60 | + if ( ! empty( $this->billing_settings['billing_postcode'] ) ) { | |
| 61 | + $fields['postcode'] = wp_parse_args( $this->billing_settings['billing_postcode'], $fields['postcode'] ); | |
| 62 | + } elseif ( ! empty( $this->shipping_settings['shipping_postcode'] ) ) { | |
| 63 | + $fields['postcode'] = wp_parse_args( $this->shipping_settings['shipping_postcode'], $fields['postcode'] ); | |
| 105 | 64 | } |
| 106 | 65 | return $fields; |
| 107 | 66 | } |
| 108 | 67 | |