- Posts: 25
- Thank you received: 1
I create a plugin for add custom js in product page but i have a problem
8 years 7 months ago - 8 years 7 months ago #290180
by Krok
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
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:
In attachment, u can see what i have in the <script> tag inside the <head>
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:
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>
Attachment not found
Last edit: 8 years 7 months ago by Krok.
The following user(s) said Thank You: alin
Please Log in or Create an account to join the conversation.
8 years 7 months ago #290181
by nicolas
Replied by nicolas on topic I create a plugin for add custom js in product page but i have a problem
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 ?
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.
8 years 7 months ago - 8 years 7 months ago #290192
by Krok
Replied by Krok on topic I create a plugin for add custom js in product page but i have a problem
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
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: 8 years 7 months ago by Krok.
Please Log in or Create an account to join the conversation.
8 years 7 months ago #290235
by nicolas
Replied by nicolas on topic I create a plugin for add custom js in product page but i have a problem
Well, if you're sure, you can always add such code:
That way, the code after this code will only run once on each page.
Code:
static $done = false;
if(!$done){
$done = true;
}else{
return;
}
Please Log in or Create an account to join the conversation.
Time to create page: 0.085 seconds