I want to be able to search through my WordPress Woocommerce store, by selecting multiple product tags. By default, if you add tags to a product, they are visible on the product page, and if you click on a tag, it searches for other products which are also tagged the same. The results of this search look url like this:
http://localhost/ccloud/?product_tag=option1
If you manually add other tags to this URL, it searches for products which are tagged with both selections, like this:
http://localhost/ccloud/?product_tag=option1+option2
This works, but obviously I want users to be able to do this using checkboxes.
Iâve created this form (which doesnât work)
<form name="search_by_tag" action=/product_tag.php method="get">
<input type=checkbox name="option1">Option 1<br>
<input type=checkbox name="option2">Option 2<br>
<input type=checkbox name="option3">Option 3<br>
<input type=checkbox name="option4">Option 4<p>
<input type=submit value="Search">
</form>
I think it doesnât work because itâs not sending the action correctly. The result of selecting multiple checkboxes and searching looks like this:
http://localhost/product_tag.php?option1=on&option2=on
How can I correct the url (the first part is missing the directory) and remove the .php
part etc? It doesn’t work at all if I remove the .php extension
Youâll have to use a little JavaScript to make this work, but you could do it like this.
HTML:
jQuery:
So, what we are doing is using a hidden field to store the selected tags, and moving the checkboxes out of the form. Then, when they submit, we populate the hidden field so that it gets included in the query string. Spaces turn into +’s in the URL.
product_tag
needs to be the name of the hidden field, and the action is/ccloud/
, so that you end up with a URL like you want. Here is a jsFiddle of it in action.A few issues I see in your code:
The
type
property should have quotes around it. Also, you are not setting avalue
property. And to get the URL to be/product_tag
you need to lop off that.php
and mayeb set the form action to the full site URL So this should be closer to what you want:And that said, you need to add more logicâeither via PHP or JavaScript like the user
dave
points outâto get this set. A simpleget
wouldnât work.