| 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 |
return $fields; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @param array $fields fields. |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
public function country_locale( $fields ) { |
| 99 |
// State. |
| 100 |
if ( isset( $fields['postcode']['label'] ) ) { |
| 101 |
if ( ! empty( $this->billing_settings['billing_postcode']['label'] ) ) { |
| 102 |
$fields['postcode']['label'] = $this->billing_settings['billing_postcode']['label']; |
| 103 |
} elseif ( ! empty( $this->shipping_settings['shipping_postcode']['label'] ) ) { |
| 104 |
$fields['postcode']['label'] = $this->shipping_settings['shipping_postcode']['label']; |
| 105 |
} |
| 106 |
} |
| 107 |
if ( isset( $fields['postcode']['required'] ) ) { |
| 108 |
if ( isset( $this->billing_settings['billing_postcode']['required'] ) ) { |
| 109 |
$fields['postcode']['required'] = $this->billing_settings['billing_postcode']['required']; |
| 110 |
} elseif ( isset( $this->shipping_settings['shipping_postcode']['required'] ) ) { |
| 111 |
$fields['postcode']['required'] = $this->shipping_settings['shipping_postcode']['required']; |
| 112 |
} |
| 113 |
} |
| 114 |
// Billing state. |
| 115 |
if ( isset( $fields['state']['label'] ) ) { |
| 116 |
if ( ! empty( $this->billing_settings['billing_state'] ) ) { |
| 117 |
$fields['state']['label'] = $this->billing_settings['billing_state']['label'] ?? $fields['postcode']['label']; |
| 118 |
} elseif ( ! empty( $this->shipping_settings['shipping_state'] ) ) { |
| 119 |
$fields['state']['label'] = $this->shipping_settings['shipping_state']['label'] ?? $fields['postcode']['label']; |
| 120 |
} |
| 121 |
} |
| 122 |
if ( isset( $fields['state']['required'] ) ) { |
| 123 |
if ( isset( $this->billing_settings['billing_state']['required'] ) ) { |
| 124 |
$fields['state']['required'] = $this->billing_settings['billing_state']['required']; |
| 125 |
} elseif ( isset( $this->shipping_settings['shipping_state']['required'] ) ) { |
| 126 |
$fields['state']['required'] = $this->shipping_settings['shipping_state']['required']; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// Billing city. |
| 131 |
if ( isset( $fields['city']['label'] ) ) { |
| 132 |
if ( ! empty( $this->billing_settings['billing_city'] ) ) { |
| 133 |
$fields['city']['label'] = $this->billing_settings['billing_city']['label'] ?? $fields['city']['label']; |
| 134 |
} elseif ( ! empty( $this->shipping_settings['shipping_city'] ) ) { |
| 135 |
$fields['city']['label'] = $this->shipping_settings['shipping_city'] ?? $fields['city']['label']; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if ( isset( $fields['city']['required'] ) ) { |
| 140 |
if ( isset( $this->billing_settings['billing_city']['required'] ) ) { |
| 141 |
$fields['city']['required'] = $this->billing_settings['billing_city']['required']; |
| 142 |
} elseif ( isset( $this->shipping_settings['shipping_city']['required'] ) ) { |
| 143 |
$fields['city']['required'] = $this->shipping_settings['shipping_city']['required']; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return $fields; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @param array $country local. |
| 152 |
* @return array |
| 153 |
*/ |
| 154 |
public function get_country_locale( $country ) { |
| 155 |
foreach ( $country as $key => $fields ) { |
| 156 |
$country[ $key ] = $this->country_locale( $fields ); |
| 157 |
} |
| 158 |
return $country; |
| 159 |
} |
| 160 |
} |
| 161 |
|