• Resolved evaldash

    (@evaldash)


    Good day. I created an inquiry form using this plugin, and have populated the searchable list item with my product names. I want for it to auto-select based on an item in the Woocommerce cart, I was trying to realise it using javascript and jQuery.

    I was making the extension via a PHP shortcode, but encountered a problem – I can’t figure out how to select a value in the searchable list. As i understand it’s a select2 field, but if that is so what’s its id? It doesn’t seem like it’s “rnFieldXX”, since when I try to apply Select2 functions to it it just returns an error, that the item is not a select2…

    The question I have now is how do I select a value (any value, I can’t even get to that point) programatically? I tried to do it with jQuery, to no avail. Help?

Viewing 1 replies (of 1 total)
  • Thread Starter evaldash

    (@evaldash)

    I managed to figure it out. The key thing I was missing was how to inform Javascript that the field’s value has changed, as it turns out you need to create and dispatch an event, not fire an existing one! If anyone needs the code, you can adapt mine, i created it as a shortcode:

    function import_button(){
        if (WC()->cart){
    		$cart_items = WC()->cart->get_cart();
    		
    		$book_names = array();
    		$book_ammounts = array();
    		
    		foreach ($cart_items as $cart_item){
    			$this_book_name = $cart_item['data']->get_title();
    			$this_book_ammount = $cart_item['quantity'];
    			
    			array_push($book_names, $this_book_name);
    			array_push($book_ammounts, $this_book_ammount);
    		}
    		?>
    		<script>
    			function import_items_from_cart(){
    				var existing_books_list = document.getElementById("_s1").getElementsByTagName('select');
    				var existing_ammounts_list = document.getElementById("_s1").getElementsByClassName('redNaoNumber');
    				
    				var booksToBeAdded = <?php echo json_encode($book_names) ?>;
    				var bookAmmountsToBeAdded = <?php echo json_encode($book_ammounts) ?>;
    				
    				var addButton = document.getElementsByClassName("fa-plus")[0].parentElement;
    				
    				// check if book already exists in list and the ammount is correct, if not add it
    				for (var i=0; i<booksToBeAdded.length; i++){
    					var existing_index = get_existing_index(existing_books_list, booksToBeAdded[i]);
    					
    					// if the book exists in the list
    					if (existing_index!=null){
    						
    						if (existing_ammounts_list[existing_index].value<bookAmmountsToBeAdded[i]){
    							existing_ammounts_list[existing_index].value = bookAmmountsToBeAdded[i]; // update the ammount, just in case
    							$(existing_ammounts_list)[existing_index].dispatchEvent(new Event("change"));
    						}
    					}
    					else{
    						empty_field_index = findEmptyField(existing_books_list);
    
    						if (empty_field_index!=null){
    							var bookName = booksToBeAdded[i];
    							var option = $(existing_books_list[empty_field_index]).find('option:contains("'+ bookName +'")');
    							
    							if (option!=null){
    								$(option).prop('selected', true);
    								$(existing_books_list)[empty_field_index].dispatchEvent(new Event("change"));
    								
    								existing_ammounts_list[empty_field_index].value = bookAmmountsToBeAdded[i]; // update the ammount, just in case
    								$(existing_ammounts_list)[empty_field_index].dispatchEvent(new Event("change"));
    							}
    						}
    					}
    				}
    					
    				// gets the book's index in isankstine saskaita list
    				function get_existing_index(arr, value){
    					for (var i=0; i<arr.length; i++){
    						selected = $(arr[i]).find(":selected").text();
    						
    						if (selected == value){
    							return i;
    						}
    					}
    					return null;
    				}
    				
    				// finds a field that hasn't been used, if there aren't any adds one
    				function findEmptyField(arr){
    					var i;
    					for (i=0; i<arr.length; i++){
    						field_value = $(arr[i]).find(":selected").text();
    						
    						if (field_value == ""){
    							return i;
    						}
    					}
    					addButton.click();
    					return i;
    				}
    			}
    			
    			// button  adding
    			var ready = function () {
    				// If element exists
    				if (document.getElementsByClassName("btn-next")[0]) {
    					var button_html = '<button id="import_button" class="button" style="float: left !important;" onclick="import_items_from_cart()"> Įkelti iš krepšelio </button>';
    					
    					var btn_next = document.getElementsByClassName("btn-next")[0];
    					var btn_prev = document.getElementsByClassName("btn-prev")[0];
    					var first_step_btn = document.querySelector("li[data-step='1']");
    					
    					$(btn_prev).before(button_html); // add the button
    					
    					btn_next.addEventListener("click", function() { if (!first_step_btn.classList.contains('active')){ document.getElementById("import_button").style.visibility = "hidden";}});
    					first_step_btn.addEventListener("click", function() {document.getElementById("import_button").style.visibility = "visible";});
    					btn_prev.addEventListener("click", function() {if (first_step_btn.classList.contains('active')){document.getElementById("import_button").style.visibility = "visible";}});
    					
    					// Return so that we don't call requestAnimationFrame() again
    					return;
    				}
    				// If the body element isn't found, run ready() again at the next pain
    				window.requestAnimationFrame(ready);
    			};
    
    			// Initialize our ready() function
    			window.requestAnimationFrame(ready);
    		</script>
    	<?php }
    }
    add_shortcode('import_from_cart', 'import_button');
    • This reply was modified 4 years, 7 months ago by evaldash.
Viewing 1 replies (of 1 total)

The topic ‘Selecting a value programatically in a searchable list’ is closed to new replies.