How to add additional field for products (displayed in the category list view)?

  • Posts: 41
  • Thank you received: 9
5 years 2 months ago #330761

-- HikaShop version -- : 4.4.1 Essential
-- Joomla version -- : 3.9.23
-- PHP version -- : 7.4

Hello!

I'm building a web-shop for the client (as always - using Hikashop Essential as my shop engine of choice for many years). Client asked to add an additional field for the products, in which to display a product reference number showin in the schematic added to header of each category. It should look like in the attached image.



If I understand correctly, I have to somehow add a custom field to product through the backend, and edit the Diplay > Views > listing_list template of the Hikashop, right? Do you have some kind of tutorial example on how to do this properly? Many thanks in advance.

Attachments:
Last edit: 5 years 2 months ago by Savickis.

Please Log in or Create an account to join the conversation.

  • Posts: 85686
  • Thank you received: 14044
  • MODERATOR
5 years 2 months ago #330790

Hi,

Supposing that this is a screenshot of a products listing (and not a variants listing), and supposing that you don't have an old override on the product view and supposing that you really have the 4.4.1 installed on your website and not an old version of HikaShop, you don't need to edit any code.
Just create a custom field of the table "product" via the Display>Custom fields menu in the backend and in its display options, make sure that the "frontend products listing" setting is activated. Then, just fill it in for your products and the column will appear automatically on the products listing.
And if you want to move the column to be at the beginning and not in the middle, you can edit the view file listing_table via the menu Display>Views and move the blocks of code displaying the custom product fields:

<?php
	if(hikashop_level(2) && !empty($this->productFields)) {
		$usefulFields = array();
		foreach ($this->productFields as $field) {
			$fieldname = $field->field_namekey;
			foreach($this->rows as $product) {
				if(!empty($product->$fieldname)) {
					$usefulFields[] = $field;
					break;
				}
			}
		}
		$productFields = $usefulFields;

		if(!empty($productFields)) {
			foreach($productFields as $field) {
				$columns++;
?>
					<th class="hikashop_product_field title hk_center"><?php
						echo $this->fieldsClass->getFieldName($field);
					?></th>
<?php
			}
		}
	}
?>
and:
<?php
	if(hikashop_level(1) && !empty($productFields)) {
		foreach($productFields as $field) {
			$namekey = $field->field_namekey;
?>
						<td><?php
			if(!empty($field->field_products)) {
							$field_products = is_string($field->field_products) ? explode(',', trim($field->field_products, ',')) : $field->field_products;
							if(!in_array($this->row->product_id, $field_products))
								continue;
			}
			if(!empty($this->row->$namekey))
				echo '<p class="hikashop_product_field'.$namekey.'">'.$this->fieldsClass->show($field,$this->row->$namekey).'</p>';
						?></td>
<?php
		}
	}
?>

The following user(s) said Thank You: Savickis

Please Log in or Create an account to join the conversation.

  • Posts: 41
  • Thank you received: 9
5 years 2 months ago #331123

Thank you @nicolas !

Interestingly enough - it didn't work for me by default. Custom field column appared right away in the backend product list column, but in the frontend of the shop additional custom field column only appared once I edited Display>Views>Frontend>list_table by removing "hikashop_level(2) &&" part of the IF statement.

Can you please elaborate what has been configured wrong in this instance, and why my custom fields only appare once this check is removed? It works now, but I would like to avoid such hack override that I was forced to make.

Many thanks nevertheless!

Last edit: 5 years 2 months ago by Savickis.

Please Log in or Create an account to join the conversation.

  • Posts: 85686
  • Thank you received: 14044
  • MODERATOR
5 years 2 months ago #331141

Hi,

It's a typo in the view file actually. It should be hikashop_level(1) instead of hikashop_level(2)
Change that and it should work properly.
We'll do the change on our end for the next version to fix this.

The following user(s) said Thank You: Savickis

Please Log in or Create an account to join the conversation.

  • Posts: 41
  • Thank you received: 9
5 years 2 months ago #331154

Nice to hear - I was wondering about that level(2) as well. Thanks and the best of luck!

The following user(s) said Thank You: Philip

Please Log in or Create an account to join the conversation.

Time to create page: 0.059 seconds
Powered by Kunena Forum