- Posts: 25
- Thank you received: 5
Accordion with current category/subcategory
2 years 1 month ago - 2 years 1 month ago #362586
by Reyesar
Accordion with current category/subcategory was created by Reyesar
-- url of the page with the problem -- :
ethemba.com/_Guests/AR/
-- HikaShop version -- : 5.1.0
-- Joomla version -- : 5.1.1
-- PHP version -- : 8.1
Hello
I wanted to use the HikaShop Content Module to show all categories and subcategories using UI accordion. Also I want to remark in which one you are when you are watching a product. When I select a category in the brand menu the accordion opens and select the correct category/subcategory. But I´m not able to make it work when a product is selected from the home page from the instances of the HikaShop Content Module. In that case the selection is the last category selected not the product one.
I am still really newbie to hikashop and I would appreciate some indication of how can I fix this problem.
I attach listing-list.php with my changes. The category identification is coded here:
Thank you very much and sorry for the inconveniences
-- HikaShop version -- : 5.1.0
-- Joomla version -- : 5.1.1
-- PHP version -- : 8.1
Hello
I wanted to use the HikaShop Content Module to show all categories and subcategories using UI accordion. Also I want to remark in which one you are when you are watching a product. When I select a category in the brand menu the accordion opens and select the correct category/subcategory. But I´m not able to make it work when a product is selected from the home page from the instances of the HikaShop Content Module. In that case the selection is the last category selected not the product one.
I am still really newbie to hikashop and I would appreciate some indication of how can I fix this problem.
I attach listing-list.php with my changes. The category identification is coded here:
Code:
<ul uk-accordion="multiple: true" class="CatListAccordion" id="Accordion<?php echo $this->params->get('ul_class_name'); ?>">
<?php
foreach($this->rows as $k => $row) {
if($only_if_products && $row->number_of_products < 1 && $row->category_id <> 560)
continue;
$found = 0;
$child_found = 0; $child2_found = 0;
if($in_hikashop_context) {
$found = $last_category_selected;
if($cid == $row->category_id) {
$found = (int)$row->category_id;
$app->setUserState(HIKASHOP_COMPONENT.'.last_category_selected', (int)$row->category_id);
} else {
foreach($row->childs as $child) {
if($cid == $child->category_id) {
$found = (int)$row->category_id;
$child_found = (int)$child->category_id;
} else { foreach($child->childs as $child2) { if($cid == $child2->category_id) { $found = (int)$row->category_id; $child_found = (int)$child->category_id; $child2_found = (int)$child2->category_id; } } }
}
}
}
$link = $this->getLink($row);
$class = ($found == $row->category_id) ? ' current active' : '';
?>
<li
<?php
if ($found == $row->category_id) {
echo ('class="uk-open cat-selected"');
}
?>
Last edit: 2 years 1 month ago by nicolas.
Please Log in or Create an account to join the conversation.
2 years 1 month ago #362592
by nicolas
Replied by nicolas on topic Accordion with current category/subcategory
Hi,
When you are on a page listing the products of a category, the line:
will match the id of the category from the URL (in $cid) and the id of the categories of the categories listing module. And if it founds a match, it will use the line:
to set the current category as the "last_category_selected".
Then, when you are on a product page, the line:
will get that value to automatically open the accordion.
However, if you directly access the details page of a product from a products listing module, without accessing a page listing the products, this $last_category_selected will be empty, and thus the accordion won't open. That's what you're seeing.
We choose this method so that if you have products with multiple categories, the system will be able to open the correct category from which you accessed the product. The drawback is that there is no fallback logic when the product is accessed directly.
Instead of :
, you could have something like this:
This would use the first category of the product as the $last_category_selected so it would work even if the product page is accessed directly. However, if you have several categories linked to a product, it won't be able to pickup the correct one.
When you are on a page listing the products of a category, the line:
Code:
$cid == $row->category_id
Code:
$app->setUserState(HIKASHOP_COMPONENT.'.last_category_selected', (int)$row->category_id);
Then, when you are on a product page, the line:
Code:
$last_category_selected = (int)$app->getUserState(HIKASHOP_COMPONENT.'.last_category_selected', 0);
However, if you directly access the details page of a product from a products listing module, without accessing a page listing the products, this $last_category_selected will be empty, and thus the accordion won't open. That's what you're seeing.
We choose this method so that if you have products with multiple categories, the system will be able to open the correct category from which you accessed the product. The drawback is that there is no fallback logic when the product is accessed directly.
Instead of :
Code:
$last_category_selected = (int)$app->getUserState(HIKASHOP_COMPONENT.'.last_category_selected', 0);
Code:
$cid = hikaInput::get()->getInt('cid', 0);
$productClass = hikashop_get('class.product');
$category_ids = $productClass->getCategories($cid);
$last_category_selected = reset($category_ids);
Please Log in or Create an account to join the conversation.
2 years 1 month ago - 2 years 1 month ago #362604
by Reyesar
Replied by Reyesar on topic Accordion with current category/subcategory
OK
Many thanks for the explanation.
Each product has two categories. For example if a watch is G-Stock Trend, it has G-STOCK and G-STOCK TREND I n order to appear in both listings. I understand why from category listing is working.
Actually i have
So $cid = hikaInput::get()->getInt('cid', 0); is the same as read cid from the URL. With this code from the module no category is selected but from the category listing is selected another one.
If I use your proposal, when I open a product details (nodule and category list) nothing is selected.
So, can I delete the main category and leave only the subcategory (only G-Shock trend), configure the main category G-SHOCK to show every child, place your code and test?
Thank you very much for your patience and time
Best rewards
Many thanks for the explanation.
Each product has two categories. For example if a watch is G-Stock Trend, it has G-STOCK and G-STOCK TREND I n order to appear in both listings. I understand why from category listing is working.
Actually i have
Code:
if($in_hikashop_context) {
if(hikaInput::get()->getString('ctrl','category') == 'product' && hikaInput::get()->getString('task','listing') == 'show') {
$last_category_selected = (int)$app->getUserState(HIKASHOP_COMPONENT.'.last_category_selected', 0);
$config =& hikashop_config();
$pathway_sef_name = $config->get('pathway_sef_name', 'category_pathway');
$cid = hikaInput::get()->getInt($pathway_sef_name, 0);
} else {
$cid = hikaInput::get()->getInt('cid', 0);
}
}
If I use your proposal, when I open a product details (nodule and category list) nothing is selected.
So, can I delete the main category and leave only the subcategory (only G-Shock trend), configure the main category G-SHOCK to show every child, place your code and test?
Thank you very much for your patience and time
Best rewards
Last edit: 2 years 1 month ago by nicolas.
Please Log in or Create an account to join the conversation.
2 years 1 month ago #362609
by nicolas
Replied by nicolas on topic Accordion with current category/subcategory
Hi,
Note that if the second category is the child of the first one, you don't usually need the first one. You can have only the child category linked to the product. Then, in the settings of your menu item, under the "products options" tab, instead of "direct sub elements" you can select "all sub elements" in the "sub elements filter" setting. That way, the products listing of the main category will also contain the products linked only to the child categories of the main category.For example if a watch is G-Stock Trend, it has G-STOCK and G-STOCK TREND I n order to appear in both listings. I understand why from category listing is working.
Then, either you didn't add the code as you should have, or the first category of the product is not in the list of categories of the module.If I use your proposal, when I open a product details (nodule and category list) nothing is selected.
yes, I suppose this will work better.So, can I delete the main category and leave only the subcategory (only G-Shock trend), configure the main category G-SHOCK to show every child, place your code and test?
Please Log in or Create an account to join the conversation.
2 years 1 month ago #362622
by Reyesar
Replied by Reyesar on topic Accordion with current category/subcategory
I have already change all products to have only one category.
I have also replace the code with the one you send me but in the products there is nothing selected.
I´m going to test and make changes to try to find the error. As soon I discover it I will publish the final code just in case it could help another.
Thank you very much
I have also replace the code with the one you send me but in the products there is nothing selected.
I´m going to test and make changes to try to find the error. As soon I discover it I will publish the final code just in case it could help another.
Thank you very much
The following user(s) said Thank You: nicolas
Please Log in or Create an account to join the conversation.
Time to create page: 0.211 seconds