Post by smoc999 » Wed Oct 18, 2017 2:44 pm

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!

Newbie

Posts

Joined
Wed Oct 18, 2017 2:40 pm

Post by straightlight » Thu Oct 19, 2017 12:37 am

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:

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

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


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by smoc999 » Thu Oct 19, 2017 6:23 am

straightlight wrote:
Thu Oct 19, 2017 12:37 am
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:

Code: Select all

// Custom Fields...
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

{% if custom_field.type == 'select' %}
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.

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

Newbie

Posts

Joined
Wed Oct 18, 2017 2:40 pm

Post by straightlight » Thu Oct 19, 2017 6:33 am

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


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by smoc999 » Thu Oct 19, 2017 7:35 am

straightlight wrote:
Thu Oct 19, 2017 6:33 am
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.
Hmm I am still not having much luck..

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();
		}
		
And added the following to the account.twig file (this works perfectly / as expected in edit.twig)

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 %}
This is driving me insane lol!

Newbie

Posts

Joined
Wed Oct 18, 2017 2:40 pm

Post by straightlight » Thu Oct 19, 2017 7:45 am

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


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by smoc999 » Thu Oct 19, 2017 7:51 am

straightlight wrote:
Thu Oct 19, 2017 7:45 am
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.
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:

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!

Newbie

Posts

Joined
Wed Oct 18, 2017 2:40 pm
Who is online

Users browsing this forum: alanjones and 80 guests