| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Elementor\Modules\Checkout\Fields; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die(); |
| 6 |
|
| 7 |
use Sellkit\Elementor\Modules\Checkout\Fields\Base; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class select. |
| 11 |
* |
| 12 |
* @since 1.1.0 |
| 13 |
* @SuppressWarnings(PHPMD.NPathComplexity) |
| 14 |
*/ |
| 15 |
class Select extends Base { |
| 16 |
/** |
| 17 |
* Type of field. |
| 18 |
* |
| 19 |
* @return string |
| 20 |
* @since 1.1.0 |
| 21 |
*/ |
| 22 |
public function type() { |
| 23 |
return 'select'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Adds additional class for fields if required. |
| 28 |
* |
| 29 |
* @param array $args checkout fields options. |
| 30 |
* @param string $key field key. |
| 31 |
* @return array |
| 32 |
* @since 1.1.0 |
| 33 |
*/ |
| 34 |
protected function additional_class( $args, $key ) { |
| 35 |
$args['class'][] = 'country-state'; |
| 36 |
|
| 37 |
$country_key = 'sellkit-checkout-shipping-cc'; |
| 38 |
|
| 39 |
if ( 'billing_state' === $key ) { |
| 40 |
$country_key = 'sellkit-checkout-billing-cc'; |
| 41 |
} |
| 42 |
|
| 43 |
$country_code = sellkit_htmlspecialchars( INPUT_COOKIE, $country_key ); |
| 44 |
|
| 45 |
$states = WC()->countries->get_states( $country_code ); |
| 46 |
|
| 47 |
if ( ( 'billing_state' === $key || 'shipping_state' === $key ) && ( ! is_array( $states ) || empty( $states ) ) ) { |
| 48 |
return $args; |
| 49 |
} |
| 50 |
|
| 51 |
$args['class'][] = 'sellkit-checkout-field-select'; |
| 52 |
|
| 53 |
return $args; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Customized html per field. |
| 58 |
* |
| 59 |
* @param string $field field html string. |
| 60 |
* @param array $args checkout fields options. |
| 61 |
* @param string $key key of field. |
| 62 |
* @return void |
| 63 |
* @since 1.1.0 |
| 64 |
*/ |
| 65 |
public function field( $field, $args, $key ) { |
| 66 |
echo '<i class="fa fa-angle-down sellkit-select-appearance"></i>'; |
| 67 |
|
| 68 |
if ( 'shipping_country' === $key || 'billing_country' === $key ) { |
| 69 |
$default = ''; |
| 70 |
if ( array_key_exists( 'default', $args ) ) { |
| 71 |
$default = $args['default']; |
| 72 |
} |
| 73 |
|
| 74 |
$this->woocommerce_field_country( $key, $default ); |
| 75 |
|
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
if ( 'shipping_state' === $key || 'billing_state' === $key ) { |
| 80 |
$country_code = filter_input( INPUT_COOKIE, 'sellkit-checkout-shipping-cc' ); |
| 81 |
|
| 82 |
if ( 'billing_state' === $key ) { |
| 83 |
$country_code = filter_input( INPUT_COOKIE, 'sellkit-checkout-billing-cc' ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( empty( $country_code ) ) { |
| 87 |
$country_code = 'US'; |
| 88 |
} |
| 89 |
|
| 90 |
$states = WC()->countries->get_states( $country_code ); |
| 91 |
|
| 92 |
$this->woocommerce_field_state( $key, $args, $states ); |
| 93 |
|
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$placeholder = $this->placeholder_required_value( $args ); |
| 98 |
|
| 99 |
?> |
| 100 |
<p id="<?php echo $key; ?>_field"> |
| 101 |
<span class="woocommerce-input-wrapper"> |
| 102 |
<select |
| 103 |
name="<?php echo esc_attr( $key ); ?>" |
| 104 |
id="<?php echo esc_attr( $key ); ?>" |
| 105 |
class="" |
| 106 |
autocomplete="country" |
| 107 |
data-placeholder="<?php echo esc_attr( $placeholder ); ?>" |
| 108 |
data-label="<?php echo ( array_key_exists( 'label', $args ) ) ? esc_attr( $args['label'] ) : ''; ?>" |
| 109 |
> |
| 110 |
<?php |
| 111 |
$default_value = ( array_key_exists( 'default', $args ) ) ? $args['default'] : ''; |
| 112 |
?> |
| 113 |
<?php foreach ( $args['options'] as $value => $label ) : ?> |
| 114 |
<?php |
| 115 |
if ( $value === $default_value ) { |
| 116 |
echo sprintf( |
| 117 |
/** Translators: 1:value 2: label */ |
| 118 |
'<option selected value="%1$s">%2$s</option>', |
| 119 |
esc_attr( $value ), |
| 120 |
esc_html( $label ) |
| 121 |
); |
| 122 |
|
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
echo sprintf( |
| 127 |
/** Translators: 1:value 2: label */ |
| 128 |
'<option value="%1$s">%2$s</option>', |
| 129 |
esc_attr( $value ), |
| 130 |
esc_html( $label ) |
| 131 |
); |
| 132 |
?> |
| 133 |
<?php endforeach; ?> |
| 134 |
</select> |
| 135 |
</span> |
| 136 |
</p> |
| 137 |
<?php |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Customize woocommerce country field. |
| 142 |
* |
| 143 |
* @param string $key field key. |
| 144 |
* @param string $default field default value. |
| 145 |
* @since 1.1.0 |
| 146 |
* @return void |
| 147 |
*/ |
| 148 |
private function woocommerce_field_country( $key, $default ) { |
| 149 |
?> |
| 150 |
<p id="<?php echo $key; ?>_field"> |
| 151 |
<span class="woocommerce-input-wrapper"> |
| 152 |
<?php |
| 153 |
$countries = WC()->countries->get_shipping_countries(); |
| 154 |
|
| 155 |
if ( 'billing_country' === $key ) { |
| 156 |
$countries = WC()->countries->get_allowed_countries(); |
| 157 |
} |
| 158 |
|
| 159 |
$field = '<select name="' . esc_attr( $key ) . '" id="' . $key . '" class="country_to_state" ><option value="">' . esc_html__( 'Select a country…', 'sellkit' ) . '</option>'; |
| 160 |
|
| 161 |
foreach ( $countries as $country_key => $country_label ) { |
| 162 |
$selected = ''; |
| 163 |
|
| 164 |
if ( $default === $country_key ) { |
| 165 |
$selected = 'selected="selected"'; |
| 166 |
} |
| 167 |
|
| 168 |
$field .= '<option ' . $selected . ' value="' . esc_attr( $country_key ) . '" >' . esc_html( $country_label ) . '</option>'; |
| 169 |
} |
| 170 |
|
| 171 |
$field .= '</select>'; |
| 172 |
$field .= '<noscript><input type="submit" name="woocommerce_checkout_update_totals" value="' . esc_attr__( 'Update country', 'sellkit' ) . '" /></noscript>'; |
| 173 |
|
| 174 |
echo $field; |
| 175 |
?> |
| 176 |
</span> |
| 177 |
</p> |
| 178 |
<?php |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Customize woocommerce state field. |
| 183 |
* |
| 184 |
* @param string $key field key. |
| 185 |
* @param array $args field options. |
| 186 |
* @param array $states country states. |
| 187 |
* @since 1.1.0 |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
private function woocommerce_field_state( $key, $args, $states ) { |
| 191 |
$placeholder = $this->placeholder_required_value( $args ); |
| 192 |
|
| 193 |
?> |
| 194 |
<p id="<?php echo $key; ?>_field"> |
| 195 |
<span class="woocommerce-input-wrapper"> |
| 196 |
<?php |
| 197 |
if ( ! is_array( $states ) || empty( $states ) ) { |
| 198 |
$state = filter_input( INPUT_COOKIE, 'sellkit-checkout-billing-state' ); |
| 199 |
|
| 200 |
if ( 'shipping_state' === $key ) { |
| 201 |
$state = filter_input( INPUT_COOKIE, 'sellkit-checkout-shipping-state' ); |
| 202 |
} |
| 203 |
|
| 204 |
if ( empty( $state ) ) { |
| 205 |
$state = ''; |
| 206 |
} |
| 207 |
|
| 208 |
echo sprintf( |
| 209 |
/* translators: 1: placeholder 2: name 3: id 4: default value */ |
| 210 |
'<input type="text" placeholder="%1$s" name="%2$s" id="%3$s" value="%4$s">', |
| 211 |
esc_attr( $placeholder ), |
| 212 |
esc_attr__( 'State', 'sellkit' ), |
| 213 |
esc_attr( 'sellkit-' . $key ), |
| 214 |
esc_attr( $state ) |
| 215 |
); |
| 216 |
|
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
$field = '<select id="sellkit-' . $key . '" name="' . esc_attr( $key ) . '" placeholder="' . esc_attr( $args['label'] ) . '"> |
| 221 |
<option value="">' . esc_html__( 'Select a state…', 'sellkit' ) . '</option>'; |
| 222 |
|
| 223 |
foreach ( $states as $state_key => $state_label ) { |
| 224 |
$field .= '<option value="' . esc_attr( $state_key ) . '" >' . esc_html( $state_label ) . '</option>'; |
| 225 |
} |
| 226 |
|
| 227 |
$field .= '</select>'; |
| 228 |
|
| 229 |
echo $field; |
| 230 |
?> |
| 231 |
</span> |
| 232 |
</p> |
| 233 |
<?php |
| 234 |
} |
| 235 |
} |
| 236 |
|