Related products on product page

  • Posts: 171
  • Thank you received: 9
1 year 9 months ago #342861

-- HikaShop version -- : 4.6.0
-- Joomla version -- : 4.1.5
-- PHP version -- : 7.4

Hi there!

I wanna ask for some trick to solve my needs:
I successfully set up to show some random items on my product's page, but I'm unable to set it up to get these not randomly but maybe much more related to the actual item "automatically" - as there are thousands of items in the shop imported, setting up manually every item's related products is not an option.

I thought on based on the product's page product name maybe stripping long product names to separate words, and run on these a LIKE SQL query, but any idea/alternatives is appreciated.

Thanks in advance,
PePe

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

  • Posts: 81539
  • Thank you received: 13069
  • MODERATOR
1 year 9 months ago #342865

Hi,

I don't see a way to do that with just the options available by default.
What you could do is develop a small plugin implementing the onBeforeProductListingLoad event to add conditions to the MySQL query loading the products when on the product details page:
www.hikashop.com/support/documentation/6...reProductListingLoad
So if you have PHP knowledge, you can do that.
This plugin would be a good basis as it uses the same event:
www.hikashop.com/marketplace/product/222...-product-plugin.html

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

  • Posts: 171
  • Thank you received: 9
1 year 9 months ago #342893

nicolas wrote: Hi,

I don't see a way to do that with just the options available by default.
What you could do is develop a small plugin implementing the onBeforeProductListingLoad event to add conditions to the MySQL query loading the products when on the product details page:
www.hikashop.com/support/documentation/6...reProductListingLoad
So if you have PHP knowledge, you can do that.
This plugin would be a good basis as it uses the same event:
www.hikashop.com/marketplace/product/222...-product-plugin.html


Thanks Nicolas!

By your links I've finally made a small plugin, it seems working, would u be so kind, to check it over to be perfect, and maybe easily adopted by anyone who needs that?
<?php
class plgHikashopRelatedproductsbyname extends JPlugin
{
	function plgHikashopRelatedproductsbyname(&$subject, $config){
		parent::__construct($subject, $config);
	}

	function onBeforeProductListingLoad(&$filters){
		// STOP IF IT'S NOT THE PRODUCT VIEW
		if ((hikaInput::get()->getCmd('ctrl')) != 'product'){return;}
		
		// FOR LOADING THE CURRENT PRODUCT'S NAME
		$class = hikashop_get('class.product');
		$product_id = (int)hikashop_getCID('product_id');
		$product = $class->get($product_id);
		
		// SPLITTING CURRENT PRODUCT'S NAME INTO AN ARRAY WHICH IS MORE THAN 5 CHAR LONG TO BE MORE "RELATED"
		$words = array_filter(preg_split("/[\s,]+/", $product->product_name), function($val){
			return strlen($val) >5 ; 
		});

		// IF THERE'S NO DATA IN WORDS, STOP FILTERING
		if(!empty($words)) {

		// ADDING THE FILTER
		$filters[]='(b.product_name LIKE "%'.implode('%") OR (b.product_name LIKE "%', $words).'%")';
		}
		
	}
}

Last edit: 1 year 9 months ago by pepecortez.
The following user(s) said Thank You: Philip

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

  • Posts: 81539
  • Thank you received: 13069
  • MODERATOR
1 year 9 months ago #342894

Hi,

That's indeed quite nice ! Thanks for sharing.

I would add that line:

if ((hikaInput::get()->getCmd('task')) != 'show'){return;}
as a products listing would also have a ctrl with "product" in the URL and that would mean the code below would be processed on the listing. that might not change anything in your case, but it would help for some others based on how their shop is structured.

Also, there is a potential issue for others:
if they have several products listing modules on the product page, that plugin would add the condition on all the modules. If you want it only for one of them, it would require for example a static variable to add the condition only the first time, or only the second time or etc based on which module should receive the condition.

The following user(s) said Thank You: pepecortez

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

Time to create page: 0.068 seconds
Powered by Kunena Forum