Get the Category Menu in Product Listing view

  • Posts: 8
  • Thank you received: 1
11 years 4 months ago #110163

In a random product list, I need to now the menu item associated to the category of each product.
The final goal is to always have the link of the product under the category link.

In more detail:

  1. Each product belongs to a single category;
  2. Every category has its own menu item, so that the URLs look just as: /category1, /category2,... /categoryN;
  3. Every product should have an URL like: /category1/productA, /category1/productB,... /categoryN/productC;
    a) This works inside the category page because the Product Sef name is empty;
    b) This doesn't work if a product is accessed from the homepage where there is a module to present random products from all categories;

Since in the module it is only possible to configure an Itemid value, I fetched the Itemid of each product in the product/listing_img_desc.php view. So, I replaced this line:

$link = hikashop_completeLink('product&task=show&cid='.$this->row->product_id.'&name='.$this->row->alias.$this->itemid.$this->category_pathway);

with:

$database = JFactory::getDBO();
$menuClass = hikashop_get('class.menus');
$query = "SELECT category_id FROM ".hikashop_table('product_category').' WHERE product_id='.$this->row->product_id.' ORDER BY product_category_id ASC';
$database->setQuery($query);
$categories_id = $database->loadColumn();
$itemid = "&Itemid=".$menuClass->getItemidFromCategory($categories_id[0]);
$link = hikashop_completeLink('product&task=show&cid='.$this->row->product_id.'&name='.$this->row->alias.$itemid.$this->category_pathway);

Is it possible to validate this solution and also tell if there is another way of getting the category_id of the product in this view (without a database query)?

Last edit: 11 years 4 months ago by pipedream. Reason: text correction

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

  • Posts: 2334
  • Thank you received: 403
11 years 4 months ago #110223

Hi there,

Your method should work but you can also try with this, which seems to be an easier way:

$link = hikashop_completeLink('product&task=show&cid='.$this->row->product_id.'&name='.$this->row->alias.$this->element->category_id.$this->category_pathway);

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

  • Posts: 8
  • Thank you received: 1
11 years 4 months ago #110263

Hi Eliot,

$this->element->category_id returns only the parent category.

I have the module configured to return all the sub-categories under this parent category.

Also, if I don't find and insert the correct Itemid in the hikashop_completeLink function, the link returned has the format:

/component/hikashop/productAcategoryId
Ex.: /component/hikashop/33-product12

instead of the wanted:

/categoryName/33-product

Last edit: 11 years 4 months ago by pipedream.

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

  • Posts: 2334
  • Thank you received: 403
11 years 4 months ago #110267

My bad, I didn't understand the first time.
In this case your solution looks good.
You could make it cleaner by loading the categories for each product in the view.html.php so you object $this->row would already have the categories but it's not especially easy.

The following user(s) said Thank You: pipedream

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

  • Posts: 8
  • Thank you received: 1
11 years 4 months ago #110268

Merci, Eliot!

Yes, I looked at view.html.php to copy the database query.
Even is less prettier, I prefer to have the code in the template, so that it can be easier to upgrade HikaShop. :)

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

Time to create page: 0.074 seconds
Powered by Kunena Forum