Prevent product from being displayed in categories it's not published in

  • Posts: 88
  • Thank you received: 19
5 years 11 months ago #301380

-- HikaShop version -- : 4.0.1

Hi

Hikashop allow a product to be displayed in all categories, even if the product is not published in more than one category.
I would like to prevent this, so I'm looking to modify the product/show_default.php file.

I can get an array of the categories, the product belong to with this code:

$productClass = hikashop_get('class.product');
$categories = $productClass->getCategories($this->element->product_id);

My problem is finding the category that I'm currently "in". The best way I could come up with, is access the active menu items hikashop parameters, like this:

// Using current menu item to find current hika category params
$app 			= JFactory::getApplication();
$currentMenuItem 	= $app->getMenu()->getActive();
$params 		= $currentMenuItem->params;
$currentCategory	= $params->get('hk_category')->category;

// Check if product is published in current hika category
$productClass = hikashop_get('class.product');
// $displayProduct now contains a boolean value we can test with
$displayProduct = array_key_exists($currentCategory, $this->categories);

Can you see a better way to solve this ??

Thanks

Last edit: 5 years 11 months ago by dyvel.

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

  • Posts: 82868
  • Thank you received: 13375
  • MODERATOR
5 years 11 months ago #301382

Hi,

Another to do it could be to directly look at the URL and its parameters in $_SERVER
There you'll have the alias of the category somewhere and you should be able to extract it and then load the category id based on that alias with a MySQL query to the hikashop_category table.
But your solution is better as far as I can see.

Last edit: 5 years 11 months ago by nicolas.

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

Time to create page: 0.056 seconds
Powered by Kunena Forum