[Skip to main content](#vessel-content)

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

##

- **[Krok](/forum/user/49481-krok "View Krok's Profile")**
- [![Krok's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/49481-krok "View Krok's Profile") Topic Author
- [ Offline](/forum/user/49481-krok)

Less More

- Posts: 25
- Thank you received: 1
-
-

8 years 7 months ago -  8 years 7 months ago [\#290180](#290180) by [Krok](/forum/user/49481-krok "View Krok's Profile")  *[I create a plugin for add custom js in product page but i have a problem](/forum/31-development/894220-i-create-a-plugin-for-add-custom-js-in-product-page-but-i-have-a-problem#290180 "Hello ! I create a plugin for add custom javascript in product page but the javascript code added by Factory::getDocument()-&gt;addScriptDeclaration(/&#42;Script set in a field&#42;/); is add ~250 times in same...")* was created by *[Krok](/forum/user/49481-krok "View Krok's Profile")*

Hello ! I create a plugin for add custom javascript in product page but the javascript code added by Factory::getDocument()-&gt;addScriptDeclaration(/*Script set in a field*/); is add ~250 times in same page ![:O](/surprise.gif ":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: Code:

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

In attachment, u can see what i have in the &lt;script&gt; tag inside the &lt;head&gt;

####  Attachment not found

Last edit: 8 years 7 months ago by [Krok](/forum/user/49481-krok "View Krok's Profile").

The following user(s) said Thank You: [alin](/forum/user/51267-alin "View alin's Profile")

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg5NDIyMC1pLWNyZWF0ZS1hLXBsdWdpbi1mb3ItYWRkLWN1c3RvbS1qcy1pbi1wcm9kdWN0LXBhZ2UtYnV0LWktaGF2ZS1hLXByb2JsZW0/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86541
- Thank you received: 14304
-
-

8 years 7 months ago [\#290181](#290181) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[I create a plugin for add custom js in product page but i have a problem](/forum/31-development/894220-i-create-a-plugin-for-add-custom-js-in-product-page-but-i-have-a-problem#290181 "Hello ! I create a plugin for add custom javascript in product page but the javascript code added by Factory::getDocument()-&gt;addScriptDeclaration(/&#42;Script set in a field&#42;/); is add ~250 times in same...")*

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-&gt;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](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg5NDIyMC1pLWNyZWF0ZS1hLXBsdWdpbi1mb3ItYWRkLWN1c3RvbS1qcy1pbi1wcm9kdWN0LXBhZ2UtYnV0LWktaGF2ZS1hLXByb2JsZW0/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Krok](/forum/user/49481-krok "View Krok's Profile")**
- [![Krok's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/49481-krok "View Krok's Profile") Topic Author
- [ Offline](/forum/user/49481-krok)

Less More

- Posts: 25
- Thank you received: 1
-
-

8 years 7 months ago -  8 years 7 months ago [\#290192](#290192) by [Krok](/forum/user/49481-krok "View Krok's Profile")  Replied by *[Krok](/forum/user/49481-krok "View Krok's Profile")* on topic *[I create a plugin for add custom js in product page but i have a problem](/forum/31-development/894220-i-create-a-plugin-for-add-custom-js-in-product-page-but-i-have-a-problem#290192 "Hello ! I create a plugin for add custom javascript in product page but the javascript code added by Factory::getDocument()-&gt;addScriptDeclaration(/&#42;Script set in a field&#42;/); is add ~250 times in same...")*

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: 8 years 7 months ago by [Krok](/forum/user/49481-krok "View Krok's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg5NDIyMC1pLWNyZWF0ZS1hLXBsdWdpbi1mb3ItYWRkLWN1c3RvbS1qcy1pbi1wcm9kdWN0LXBhZ2UtYnV0LWktaGF2ZS1hLXByb2JsZW0/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86541
- Thank you received: 14304
-
-

8 years 7 months ago [\#290235](#290235) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[I create a plugin for add custom js in product page but i have a problem](/forum/31-development/894220-i-create-a-plugin-for-add-custom-js-in-product-page-but-i-have-a-problem#290235 "Hello ! I create a plugin for add custom javascript in product page but the javascript code added by Factory::getDocument()-&gt;addScriptDeclaration(/&#42;Script set in a field&#42;/); is add ~250 times in same...")*

Well, if you're sure, you can always add such code: 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](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg5NDIyMC1pLWNyZWF0ZS1hLXBsdWdpbi1mb3ItYWRkLWN1c3RvbS1qcy1pbi1wcm9kdWN0LXBhZ2UtYnV0LWktaGF2ZS1hLXByb2JsZW0/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop Install &amp; Update Product &amp; Category Display Content Creation Sales and Orders Orders management Reports &amp; Statistics Customers Checkout Payment Methods Shipping Methods Import &amp; Export (CSV, XLS…) Specific Features Affilation Taxes Wishlist Vote Carousel &amp; Slider Mass Actions Filters &amp; Search Discount &amp; Coupon Feature Request Integrations Showcase General Talk about HikaShop Development HikaShop 3.0 Beta Translation Commercial Jobs HikaShop Components HikaMarket Market: Feature requests Market: How to? Market: Bug Report HikaSerial General talk about HikaSerial Serial: Bug Report Serial: How to? Support en Français de HikaSerial (French Support) HikaSubscription HikaAuction Auction: Feature requests Auction: How to? Auction: Bug report Hikashop around the world Support en Français (French Support) Russian FAQ. Курс выживания с Hikashop. Частые вопросы и о Расширения Hikashop Наши сайты на Hikashop 日本語版サポート(Japanese Support) Thai German Archives Bug Report How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.054 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)
