Hello,
I have created several custom fields under Customers > Custom Fields.
I would like to use these fields on the account page to output conditional information.
For example:
{{% if account_custom_field[1] == X %}}
DO THIS
{{% elseif account_custom_field[1] == Y %}}
DO THIS
{{% endif %}}
Any idea how I can achieve this? I have spent hours trying and failing to add the relevant code to controller/account.php with no luck
I am using Opencart 3.0.1
Hope this makes sense, please let me know if you need any further clarification. Any help would be greatly appreciated!
I have created several custom fields under Customers > Custom Fields.
I would like to use these fields on the account page to output conditional information.
For example:
{{% if account_custom_field[1] == X %}}
DO THIS
{{% elseif account_custom_field[1] == Y %}}
DO THIS
{{% endif %}}
Any idea how I can achieve this? I have spent hours trying and failing to add the relevant code to controller/account.php with no luck
I am using Opencart 3.0.1
Hope this makes sense, please let me know if you need any further clarification. Any help would be greatly appreciated!
The catalog/controller/account/account.php file does not import any custom fields. An example of the custom fields with the accounts can be found in catalog/controller/account/edit.php file:
An example of using custom fields with TWIG files can be found, in this case, in the catalog/view/theme/your_theme/template/account/edit.twig file - starting from this line:
Code: Select all
// Custom Fields
$data['custom_fields'] = array();
$this->load->model('account/custom_field');
$custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
foreach ($custom_fields as $custom_field) {
if ($custom_field['location'] == 'account') {
$data['custom_fields'][] = $custom_field;
}
}
if (isset($this->request->post['custom_field']['account'])) {
$data['account_custom_field'] = $this->request->post['custom_field']['account'];
} elseif (isset($customer_info['custom_field'])) {
$data['account_custom_field'] = json_decode($customer_info['custom_field'], true);
} else {
$data['account_custom_field'] = array();
}
Code: Select all
{% if custom_field.type == 'select' %}
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Thank you for your reply! I have tried that, and gone so far as to copy the entire edit.php controller. I can get the Custom field names to output, but for the life of me I can't figure out how to get the value of a specific custom field for the currently logged in account.straightlight wrote: ↑Thu Oct 19, 2017 12:37 amThe catalog/controller/account/account.php file does not import any custom fields. An example of the custom fields with the accounts can be found in catalog/controller/account/edit.php file:
An example of using custom fields with TWIG files can be found, in this case, in the catalog/view/theme/your_theme/template/account/edit.twig file - starting from this line:Code: Select all
// Custom Fields...
Code: Select all
{% if custom_field.type == 'select' %}
This is as close as I have gotten:
Code: Select all
{% for custom_field in custom_fields %}
// All my custom fields are select elements
{% if custom_field.type == 'select' %}
{{ custom_field.name }}
{% for custom_field_value in custom_field.custom_field_value %}
{% if account_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == account_custom_field[custom_field.custom_field_id] %}
// I would assume this would output the currently selected value for the logged in user but no dice :(
{{ custom_field_value.name }}
{% else %}
// Always get this...
No selection has been made
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
Well, you're doing great so far. All you need to do is to rely on the edit.twig file that outputs each option types other than select and you're good to go.
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Hmm I am still not having much luck..straightlight wrote: ↑Thu Oct 19, 2017 6:33 amWell, you're doing great so far. All you need to do is to rely on the edit.twig file that outputs each option types other than select and you're good to go.
I have added this to the the account.php file:
Code: Select all
// Custom Fields
$data['custom_fields'] = array();
$this->load->model('account/custom_field');
$custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
foreach ($custom_fields as $custom_field) {
if ($custom_field['location'] == 'account') {
$data['custom_fields'][] = $custom_field;
}
}
if (isset($this->request->post['custom_field']['account'])) {
$data['account_custom_field'] = $this->request->post['custom_field']['account'];
} elseif (isset($customer_info)) {
$data['account_custom_field'] = json_decode($customer_info['custom_field'], true);
} else {
$data['account_custom_field'] = array();
}
Code: Select all
{% for custom_field in custom_fields %}
{% for custom_field_value in custom_field.custom_field_value %}
{% if account_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == account_custom_field[custom_field.custom_field_id] %}
{% if custom_field_value.name == 'Oval' %}
LINK TO OVAL STUFF
{% else %}
YOU'VE FAILED AGAIN
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
I do not see the $customer_info defined anywhere in your code. If you are not posting values from a form that targets the account.php file, then you would still need to add the $customer_info from the edit.php file and ensuring the entered variable ID that gets transferred to the account model is properly defined in the mean time. By posting partial codes on the forum, it would be harden to resolve this issue.
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Thanks so much for steering me in the right direction! I finally managed to get it to work! Here is what I added to account.php:straightlight wrote: ↑Thu Oct 19, 2017 7:45 amI do not see the $customer_info defined anywhere in your code. If you are not posting values from a form that targets the account.php file, then you would still need to add the $customer_info from the edit.php file and ensuring the entered variable ID that gets transferred to the account model is properly defined in the mean time. By posting partial codes on the forum, it would be harden to resolve this issue.
Code: Select all
$this->load->model('account/customer');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->model_account_customer->editCustomer($this->customer->getId(), $this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('account/account', '', true));
}
if ($this->request->server['REQUEST_METHOD'] != 'POST') {
$customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
}
// Custom Fields
$data['custom_fields'] = array();
$this->load->model('account/custom_field');
$custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
foreach ($custom_fields as $custom_field) {
if ($custom_field['location'] == 'account') {
$data['custom_fields'][] = $custom_field;
}
}
if (isset($this->request->post['custom_field']['account'])) {
$data['account_custom_field'] = $this->request->post['custom_field']['account'];
} elseif (isset($customer_info)) {
$data['account_custom_field'] = json_decode($customer_info['custom_field'], true);
} else {
$data['account_custom_field'] = array();
}
Thanks again!
Who is online
Users browsing this forum: alanjones and 80 guests