如何在客户列表中添加自定义字段? (opencart)

时间:2018-12-01 00:36:26

标签: opencart

如何将自定义字段放入getList函数并将其添加到opencart后端的客户列表中?

以下代码是表单getForm函数

// 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();
}

1 个答案:

答案 0 :(得分:0)

自定义字段值以Json格式存储在oc_customer表的custom_field列中。 因此,您可以在getList函数中获取这些值,然后将其传递给客户数组

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

现在,您需要获取自定义字段的名称,以在客户列表中显示为表格的列。

// 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']
    );
}

您可以在表格中创建更多列

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

在每个客户行中,您可以通过匹配自定义字段的ID,然后在该特定列中显示数据,在客户的自定义字段数据中找到custom_field

这些数组将为您提供列名,然后通过匹配来自客户JSON解码自定义值的custom_value_id,您可以显示..

{% 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 %}

需要花费更多的精力才能在右栏中显示与自定义值匹配的栏,因此请使用提示。