Omar Alnabris
Omar Alnabris

Reputation: 13

How to add custom fields in customer list? (opencart)

How can I get custom fields into getList function and add they to customer list in backend of opencart?

the following code is form getForm function

// Custom Fields
$this->load->model('customer/custom_field');

$data['custom_fields'] = array();

$custom_fields = $this->model_customer_custom_field->getCustomFields();

$confirmation_info = $this->model_sale_confirmation->getConfirmation($confirmation_id);

foreach ($custom_fields as $custom_field) {
    $data['custom_fields'][] = array(
        'custom_field_id'    => $custom_field['custom_field_id'],
        'custom_field_value' => $this->model_customer_custom_field->getCustomFieldValues($custom_field['custom_field_id']),
        'name'               => $custom_field['name'],
        'value'              => $custom_field['value'],
        'type'               => $custom_field['type'],
        'location'           => $custom_field['location'],
        'sort_order'         => $custom_field['sort_order']
    );
}

$data['download']  = $this->url->link('tool/upload/download', 'user_token=' . $this->session->data['user_token'], true);

if (isset($this->request->post['custom_field'])) {
    $data['confirmation_custom_field'] = $this->request->post['custom_field'];
} elseif (!empty($confirmation_info)) {
    $data['confirmation_custom_field'] = json_decode($confirmation_info['custom_field'], true);
} else {
    $data['confirmation_custom_field'] = array();
}

Upvotes: 1

Views: 786

Answers (1)

Saqib Ashraf
Saqib Ashraf

Reputation: 94

Custom field values are stored in Json format in oc_customer table custom_field column. So you can get these values in getList() function and pass them in customers array like this

$data['customers'][] = array(
'account_custom_field'=>json_decode($result['custom_field'], true),

Now you need to get the name of the custom fields for displaying in customer list as columns of the table.

// Custom Fields
$this->load->model('customer/custom_field');

$data['custom_fields'] = array();

$custom_fields = $this->model_customer_custom_field->getCustomFields();

$confirmation_info = $this->model_sale_confirmation->getConfirmation($confirmation_id);

foreach ($custom_fields as $custom_field) {
    $data['custom_fields'][] = array(
        'custom_field_id'    => $custom_field['custom_field_id'],
        'custom_field_value' => $this->model_customer_custom_field->getCustomFieldValues($custom_field['custom_field_id']),
        'name'               => $custom_field['name'],
        'value'              => $custom_field['value'],
        'type'               => $custom_field['type'],
        'location'           => $custom_field['location'],
        'sort_order'         => $custom_field['sort_order']
    );
}

You can create more columns in the table

{% for custom_field in custom_fields %}
    <th>{{custom_field.name}}</th>
{% endfor %}

In the each customer row you can find the custom_field in customer's custom field data by matching id of custom field and then displaying the data in that particular column

These array will provide you the column names and then by matching custom_value_id from customer JSON Decode custom values you can display ..

{% for custom_field in custom__fields %}
    {% if customer.account_custom_field[custom_field.custom_field_id] %} 
            {{customer.account_custom_field[custom_field.custom_field_id]}} 
    {% endif %}
{% endfor %}

There is some more effort required to display the column matching custom value in the right column so use the hint.

Upvotes: 1

Related Questions