Untitled

 avatar
webwizards
plain_text
3 hours ago
6.8 kB
0
Indexable
// Add custom column headers for billing fields and B2BKing custom fields
add_action('b2bking_b2bcustomers_column_header', 'add_custom_billing_columns_header');
function add_custom_billing_columns_header() {
    ?>
    <th><?php esc_html_e('First Name', 'b2bking'); ?></th>
    <th><?php esc_html_e('Last Name', 'b2bking'); ?></th>
    <th><?php esc_html_e('Street Address', 'b2bking'); ?></th>
    <th><?php esc_html_e('Country/State', 'b2bking'); ?></th>
    <th><?php esc_html_e('City', 'b2bking'); ?></th>
    <th><?php esc_html_e('Postcode', 'b2bking'); ?></th>
    <?php
    
    // Add B2BKing custom fields headers
    $custom_fields = get_posts([
        'post_type' => 'b2bking_custom_field',
        'post_status' => 'publish',
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'meta_query'=> array(
            'relation' => 'AND',
            array(
                'key' => 'b2bking_custom_field_status',
                'value' => 1
            ),
            array(
                'relation' => 'OR',
                array(
                    'key' => 'b2bking_custom_field_billing_connection',
                    'value' => 'none'
                ),
                array(
                    'key' => 'b2bking_custom_field_billing_connection',
                    'value' => 'billing_vat'
                ),
            ),                         
        )
    ]);
    
    foreach ($custom_fields as $custom_field) {
        ?>
        <th><?php echo esc_html($custom_field->post_title); ?></th>
        <?php
    }
}

// Add custom column content for billing fields and B2BKing custom fields
add_filter('b2bking_b2bcustomers_row_content', 'add_custom_billing_columns_content', 10, 2);
function add_custom_billing_columns_content($echostring, $user_id) {
    // Get billing information
    $first_name = get_user_meta($user_id, 'billing_first_name', true);
    $last_name = get_user_meta($user_id, 'billing_last_name', true);
    $address = get_user_meta($user_id, 'billing_address_1', true);
    $address_2 = get_user_meta($user_id, 'billing_address_2', true);
    $country = get_user_meta($user_id, 'billing_country', true);
    $state = get_user_meta($user_id, 'billing_state', true);
    $city = get_user_meta($user_id, 'billing_city', true);
    $postcode = get_user_meta($user_id, 'billing_postcode', true);
    
    // Format address (combine address_1 and address_2 if both exist)
    $full_address = $address;
    if (!empty($address_2)) {
        $full_address .= ', ' . $address_2;
    }
    
    // Format country and state
    $country_state = '';
    if (!empty($country)) {
        // Get country name from country code
        $countries = WC()->countries->get_countries();
        $country_name = isset($countries[$country]) ? $countries[$country] : $country;
        $country_state = $country_name;
        
        if (!empty($state)) {
            // Get state name from state code
            $states = WC()->countries->get_states($country);
            $state_name = isset($states[$state]) ? $states[$state] : $state;
            $country_state .= ', ' . $state_name;
        }
    }
    
    // Handle empty values
    $first_name = !empty($first_name) ? $first_name : '-';
    $last_name = !empty($last_name) ? $last_name : '-';
    $full_address = !empty($full_address) ? $full_address : '-';
    $country_state = !empty($country_state) ? $country_state : '-';
    $city = !empty($city) ? $city : '-';
    $postcode = !empty($postcode) ? $postcode : '-';
    
    // Add the billing field columns to the existing row content
    $echostring .= '<td>' . esc_html($first_name) . '</td>';
    $echostring .= '<td>' . esc_html($last_name) . '</td>';
    $echostring .= '<td>' . esc_html($full_address) . '</td>';
    $echostring .= '<td>' . esc_html($country_state) . '</td>';
    $echostring .= '<td>' . esc_html($city) . '</td>';
    $echostring .= '<td>' . esc_html($postcode) . '</td>';
    
    // Add B2BKing custom fields columns
    $custom_fields = get_posts([
        'post_type' => 'b2bking_custom_field',
        'post_status' => 'publish',
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'meta_query'=> array(
            'relation' => 'AND',
            array(
                'key' => 'b2bking_custom_field_status',
                'value' => 1
            ),
            array(
                'relation' => 'OR',
                array(
                    'key' => 'b2bking_custom_field_billing_connection',
                    'value' => 'none'
                ),
                array(
                    'key' => 'b2bking_custom_field_billing_connection',
                    'value' => 'billing_vat'
                ),
            ),                         
        )
    ]);
    
    foreach ($custom_fields as $custom_field) {
        $field_value = get_user_meta($user_id, 'b2bking_custom_field_' . $custom_field->ID, true);
        $field_value = !empty($field_value) ? $field_value : '-';
        $echostring .= '<td>' . esc_html($field_value) . '</td>';
    }
    
    return $echostring;
}

// Add custom column footers for billing fields and B2BKing custom fields
add_action('b2bking_b2bcustomers_column_footer', 'add_custom_billing_columns_footer');
function add_custom_billing_columns_footer() {
    ?>
    <th><?php esc_html_e('First Name', 'b2bking'); ?></th>
    <th><?php esc_html_e('Last Name', 'b2bking'); ?></th>
    <th><?php esc_html_e('Street Address', 'b2bking'); ?></th>
    <th><?php esc_html_e('Country/State', 'b2bking'); ?></th>
    <th><?php esc_html_e('City', 'b2bking'); ?></th>
    <th><?php esc_html_e('Postcode', 'b2bking'); ?></th>
    <?php
    
    // Add B2BKing custom fields footers
    $custom_fields = get_posts([
        'post_type' => 'b2bking_custom_field',
        'post_status' => 'publish',
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'meta_query'=> array(
            'relation' => 'AND',
            array(
                'key' => 'b2bking_custom_field_status',
                'value' => 1
            ),
            array(
                'relation' => 'OR',
                array(
                    'key' => 'b2bking_custom_field_billing_connection',
                    'value' => 'none'
                ),
                array(
                    'key' => 'b2bking_custom_field_billing_connection',
                    'value' => 'billing_vat'
                ),
            ),                         
        )
    ]);
    
    foreach ($custom_fields as $custom_field) {
        ?>
        <th><?php echo esc_html($custom_field->post_title); ?></th>
        <?php
    }
}
Editor is loading...
Leave a Comment