# ali2woo-lite/trunk/includes/classes/controller/ThirdNameFieldController.php

AliNext – WooCommerce Dropshipping Plugin for AliExpress, version trunk. 96 lines.

- Page: https://pluginprobe.com/plugins/ali2woo-lite/trunk/code/includes/classes/controller/ThirdNameFieldController.php
- Raw: https://pluginprobe.com/plugins/ali2woo-lite/trunk/raw/includes/classes/controller/ThirdNameFieldController.php
- Modified: 2024-06-25T13:44:36+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ali2woo-lite/trunk/code/includes/classes/controller/ThirdNameFieldController.php#L10-L20`.

```php
<?php

/**
 * Description of ThirdNameFieldController
 *
 * @author Ali2Woo Team
 * 
 * @autoload: a2wl_init
 */

namespace AliNext_Lite;;

class ThirdNameFieldController {
    
    private $third_name_key = 'third_name';

    public function __construct() {
        if (get_setting('order_third_name')){
            
            add_filter( 'woocommerce_default_address_fields' , array($this, 'add_field_to_checkout') );
            
            add_filter( 'woocommerce_formatted_address_replacements', array($this, 'formatted_address_replacements'), 99, 2 );
            
        
            
            add_filter('woocommerce_admin_billing_fields', array($this, 'add_extra_customer_field'));
            add_filter('woocommerce_admin_shipping_fields', array($this, 'add_extra_customer_field'));
            
            add_filter( 'woocommerce_order_formatted_billing_address',  array($this,'update_formatted_billing_address'), 99, 2);
            
            add_filter( 'woocommerce_order_formatted_shipping_address', array($this,'update_formatted_shipping_address'), 99, 2);
        
        }
    }
    
    public function add_extra_customer_field($fields){
        $fields = $this->add_field_to_checkout($fields);
        return $fields;    
    }

    public function add_field_to_checkout($address_fields) {
        
        $new_address_fields = array();
        
        $thrid_name_field =  array(
            'label'     => esc_html__('Middle name', 'ali2woo'),
            'required'  => true,
            'class'     =>  array('form-row-wide', 'address-field'),
            'type'  => 'text',
            
        );
        
        $last_key = false; 
        
        foreach($address_fields as $key => $val){
            if ($last_key === "last_name"){
                $new_address_fields[$this->third_name_key] = $thrid_name_field;
                
            }
            
            $new_address_fields[$key] = $address_fields[$key];
            
            $last_key = $key;
        }
        
        return $new_address_fields;
        
    }
    
    public function formatted_address_replacements($address, $args ){
    
        if (isset($args[$this->third_name_key])){
            $address['{name}'] = $args[$this->third_name_key]." ".$args['first_name']." ".$args['last_name'];    
        }

        return $address;
    }
    
    public function update_formatted_billing_address($address, $obj){
        if (isset($address[$this->third_name_key])){
            $address[$this->third_name_key] = $obj->get_meta('_billing_third_name');    
        }
        
        return $address;
    }
    
    public function update_formatted_shipping_address($address, $obj){
        if (isset($address[$this->third_name_key])){
            $address[$this->third_name_key] = $obj->get_meta('_shipping_third_name');    
        }
            
        return $address;
    }

}

```
