| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of ThirdNameFieldController |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
* |
| 8 |
* @autoload: a2wl_init |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace AliNext_Lite;; |
| 12 |
|
| 13 |
class ThirdNameFieldController { |
| 14 |
|
| 15 |
private $third_name_key = 'third_name'; |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
if (get_setting('order_third_name')){ |
| 19 |
|
| 20 |
add_filter( 'woocommerce_default_address_fields' , array($this, 'add_field_to_checkout') ); |
| 21 |
|
| 22 |
add_filter( 'woocommerce_formatted_address_replacements', array($this, 'formatted_address_replacements'), 99, 2 ); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
add_filter('woocommerce_admin_billing_fields', array($this, 'add_extra_customer_field')); |
| 27 |
add_filter('woocommerce_admin_shipping_fields', array($this, 'add_extra_customer_field')); |
| 28 |
|
| 29 |
add_filter( 'woocommerce_order_formatted_billing_address', array($this,'update_formatted_billing_address'), 99, 2); |
| 30 |
|
| 31 |
add_filter( 'woocommerce_order_formatted_shipping_address', array($this,'update_formatted_shipping_address'), 99, 2); |
| 32 |
|
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
public function add_extra_customer_field($fields){ |
| 37 |
$fields = $this->add_field_to_checkout($fields); |
| 38 |
return $fields; |
| 39 |
} |
| 40 |
|
| 41 |
public function add_field_to_checkout($address_fields) { |
| 42 |
|
| 43 |
$new_address_fields = array(); |
| 44 |
|
| 45 |
$thrid_name_field = array( |
| 46 |
'label' => esc_html__('Middle name', 'ali2woo'), |
| 47 |
'required' => true, |
| 48 |
'class' => array('form-row-wide', 'address-field'), |
| 49 |
'type' => 'text', |
| 50 |
|
| 51 |
); |
| 52 |
|
| 53 |
$last_key = false; |
| 54 |
|
| 55 |
foreach($address_fields as $key => $val){ |
| 56 |
if ($last_key === "last_name"){ |
| 57 |
$new_address_fields[$this->third_name_key] = $thrid_name_field; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
$new_address_fields[$key] = $address_fields[$key]; |
| 62 |
|
| 63 |
$last_key = $key; |
| 64 |
} |
| 65 |
|
| 66 |
return $new_address_fields; |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
public function formatted_address_replacements($address, $args ){ |
| 71 |
|
| 72 |
if (isset($args[$this->third_name_key])){ |
| 73 |
$address['{name}'] = $args[$this->third_name_key]." ".$args['first_name']." ".$args['last_name']; |
| 74 |
} |
| 75 |
|
| 76 |
return $address; |
| 77 |
} |
| 78 |
|
| 79 |
public function update_formatted_billing_address($address, $obj){ |
| 80 |
if (isset($address[$this->third_name_key])){ |
| 81 |
$address[$this->third_name_key] = $obj->get_meta('_billing_third_name'); |
| 82 |
} |
| 83 |
|
| 84 |
return $address; |
| 85 |
} |
| 86 |
|
| 87 |
public function update_formatted_shipping_address($address, $obj){ |
| 88 |
if (isset($address[$this->third_name_key])){ |
| 89 |
$address[$this->third_name_key] = $obj->get_meta('_shipping_third_name'); |
| 90 |
} |
| 91 |
|
| 92 |
return $address; |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|