Display Products

  • Posts: 52
  • Thank you received: 3
3 months 2 weeks ago #362947

-- HikaShop version -- : 5.1.0
-- Joomla version -- : 5.1.4
-- PHP version -- : 8.1

Hi.
I'm developing a 3rd party component for my webite for product guarantees. i have a form there, and i need to display a select dropdown list to display my hikashop product names (only main products, not characteristics)

is there any specific method that i can grab this list and display as a select dropdown ?

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

  • Posts: 83007
  • Thank you received: 13398
  • MODERATOR
3 months 2 weeks ago #362951

Hi,

Well, if you're developing a component following the best practices of Joomla 3/4/5 development, you must have a XML file with the fields of your form using the types available in Joomla:
manual.joomla.org/docs/general-concepts/...lds/standard-fields/

And thus, you have 2 basic ways:
- You can create your own type of field based on the "list" type :
manual.joomla.org/docs/general-concepts/...-extending-listfield
In it, you can run your own MySQL query to load the products data from the hikashop_product table in order to populate the field.
- You can use the "sql" type:
manual.joomla.org/docs/general-concepts/.../standard-fields/sql
With it, you can directly provide the elements of a MySQL query to populate the field.

While these two approaches are fine, you'll quickly have a problem once the HikaShop you're taking the data from has more than a few thousand products because that's a lot of data to load from the database.

We actually had the same issue ourselves. For example, when you create a new menu item of the type "HikaShop product page", you need to select a product so that when the user click on the menu item he directly sees the product details page of that product.
And thus, we created our own type of form field called "selectproduct".
You need to add the field path "/components/com_hikashop/fields" to be able to use it.
And you can see an example of how it is used in the components/com_hikashop/views/product/tmpl/show.xml file:

								<field
												id="product_id"
												name="product_id"
												type="selectproduct"
												label="Select a product"
												description="Select here the product to display for the current link"
								/>
This field type uses the "namebox" system we created in HikaShop. This displays a selector which allows for retrieval of the data little by little while you scroll, like a pagination system, and also supports a tree system, all in one, and more.
If you look at the code of the file /components/com_hikashop/fields/selectproduct.php you can see that we're actually just encapsulating a namebox with the type "product" in that form field type:
		$nameboxType = hikashop_get('type.namebox');
		$select = '<div style="height:130px; margin-left:150px;">' .
				$nameboxType->display(
					'jform[params][product_id]',
					$this->value,
					hikashopNameboxType::NAMEBOX_SINGLE,
					'product',
					array(
						'delete' => false,
						'default_text' => '<em>'.JText::_('HIKA_NONE').'</em>',
					)
				).
				'</div>';

Inside HikaShop views, we directly call the namebox type. So, if you already have HikaShop loaded on the page (with an include of the main helper.php file of HikaShop), and you don't rely on XML forms in your component, you could skip the XML and directly use PHP to load the namebox on your form, like we do in HikaShop.

Last edit: 3 months 2 weeks ago by nicolas.
The following user(s) said Thank You: demoisellegol

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

Time to create page: 0.055 seconds
Powered by Kunena Forum