Tags/topics: 
2
Answers
Vote up!
0
Vote down!

Custom Line Items - Attached Javascript Running Multiple Times

I'm using Drupal Commerce with Commerce Product Option module. Design goal is to have a visual product configurator - an area on the Add 2 Cart Form where product images update as the user selects individual line item options.

NOTE: The product is custom furniture and the Product Options are fabric finish, wood, cushion, etc. Consequently the universe of options is large, so having an individual SKUs and images for each possible product configuration is not practical. Hence Commerce Product Option.

So far I've implemented a semi-successful solution that uses jQuery to (A) modify the line item form to add jQuery modal dialog and product display area, and (B) add handlers to form elements. This javascript is attached to the form via a custom module, hook_form_alter() and $form['#attached']['js'].

The problem I'm having is with the Product Display form, which provides the user a SELECT list (ID=edit-product-id) to choose furniture configuration for a particular family of furniture (4 seat sofa, 3 seat sofa, loveseat, etc.). Upon initial display everything works, however if the user selects another configuration the javascript is run a second time (well actually four more times - which I assume is associated with the number of line options).

I'm seeking ideas on how to resolve this issue. Thanks in advance.

Chris

Asked by: cluther
on December 18, 2012

2 Answers

Vote up!
0
Vote down!

The solution was to modify the jQuery functions using .once(). For example:

...
DialogForm = "Confirm Selection";
$(".node-content").once().append(DialogForm);
...

Answer by: cluther
Posted: Jan 21, 2013
Vote up!
0
Vote down!

Hi Chris, could you provide a link with your solution (website). I am very interested in the outcome.

Answer by: monty
Posted: Nov 19, 2013