Need help organizing my JavaScript into PHP for Woocommerce template
-
Hi all,
I have a woocommerce store set up, and the products have variants. These variants display in drop down menu’s. I have JavaScript that will take the options from the drop down menus and put them into check boxes. Users who are not logged in will see check boxes.
I found the template file,
wooajax.php, in my theme’s/includes/folder. Here is the file: http://pastebin.com/dpjE1vQtI need to figure out how to work this bit of JavaScript into this file:
<div class="check-boxes"> <?php if (!is_user_logged_in()): ?> <script> function myFunction() { var selects = document.getElementsByTagName('SELECT'); var container = document.getElementById('quickview-content'); // document.getElementsByTagName("SELECT")[0].setAttribute("type", "checkbox"); var i; for (var x = 0; x < selects.length; x++) { var select = selects[x]; for (i = 0; i < select.length; i++) { var checkbox = document.createElement('input'); checkbox.type = 'checkbox'; checkbox.name = 'option'; checkbox.id = 'randomId' + i; checkbox.value = select.options[i].text; var label = document.createElement('label') label.htmlFor = 'randomId' + i; label.appendChild(document.createTextNode(select.options[i].text)); container.appendChild(checkbox); container.appendChild(label); } } } myFunction(); </script> <?php endif ?> </div>I plugged it in on line 84, in the “product-info” div, but this displays the checkboxes at the very bottom of the div and the JavaScript renders in the HTML where the checkboxes should.
I’m thinking I need to add the script somewhere else, and just call the function here. How do I call a JavaScript function with PHP, and also where else can I put the script? Anywhere else, the
var selectsreturns null because the selects are not rendered yet. It has to be stuck with the AJAX so they render at the same time.Any help would be appreciated!
The topic ‘Need help organizing my JavaScript into PHP for Woocommerce template’ is closed to new replies.