Hi,
When you are on a page listing the products of a category, the line:
$cid == $row->category_id
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:
$app->setUserState(HIKASHOP_COMPONENT.'.last_category_selected', (int)$row->category_id);
to set the current category as the "last_category_selected".
Then, when you are on a product page, the line:
$last_category_selected = (int)$app->getUserState(HIKASHOP_COMPONENT.'.last_category_selected', 0);
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 :
$last_category_selected = (int)$app->getUserState(HIKASHOP_COMPONENT.'.last_category_selected', 0);
, you could have something like this:
$cid = hikaInput::get()->getInt('cid', 0);
$productClass = hikashop_get('class.product');
$category_ids = $productClass->getCategories($cid);
$last_category_selected = reset($category_ids);
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.