I create a plugin for add custom js in product page but i have a problem

  • Posts: 25
  • Thank you received: 1
4 years 1 month ago #290180

Hello ! I create a plugin for add custom javascript in product page but the javascript code added by Factory::getDocument()->addScriptDeclaration(/*Script set in a field*/); is add ~250 times in same page :O because the onHikashopBeforeDsiplay view is trigger many times, and i don't know how to solve this problem can u help me pls ?
My php code:

public function onHikashopBeforeDisplayView (&$view)
{
	$plugin = JPluginHelper::getPlugin('hikashop', 'CustomJs');
	$plg_params = new JRegistry();
	$plg_params->loadString($plugin->params);
	$user = Factory::getUser();
	$dom = Factory::getDocument();
	if($view->getName() === 'product' && $view->getLayout() === 'show_quantity')
	{
		if(intval($plg_params->get('allow')) === 1)
		{
			$dom->addScriptDeclaration($plg_params->get('javascript'));
		}
	}
}

In attachment, u can see what i have in the <script> tag inside the <head>

Attachments:
Last edit: 4 years 1 month ago by Krok.
The following user(s) said Thank You: alin

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

  • Posts: 75473
  • Thank you received: 11742
  • MODERATOR
4 years 1 month ago #290181

Hi,
I'm surprised that the trigger would do anything at all with your code.
That fonction is called by the main view. So for example product / listing.php or product / show.php and not for the sub view files called within the main view file.
So normally, you should never have the condition $view->getLayout() === 'show_quantity' valid in your plugin.

Are you sure that the js code you're looking at comes from the PHP code you gave in your message ?

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

  • Posts: 25
  • Thank you received: 1
4 years 1 month ago #290192

Hello nicolas,
yeah i'm sure this js code is from the field in the configuration page of my plugin.
I've the same problem with my component for customize products, the html code was duplicated many times but I've solved this problem by creating a page for the component

Last edit: 4 years 1 month ago by Krok.

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

  • Posts: 75473
  • Thank you received: 11742
  • MODERATOR
4 years 1 month ago #290235

Well, if you're sure, you can always add such code:

static $done = false;
if(!$done){
 $done = true;
}else{
 return;
}
That way, the code after this code will only run once on each page.

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

Time to create page: 0.065 seconds
Powered by Kunena Forum