Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add classes to selectbox options that should theoretically match possible referring URL slugs; get the referring URL and select the option that matches. Being used to choose a WooCommerce variation relevant to the grouped product that the user came from.
jQuery(document).ready(function() {
var referrer = document.referrer;
var urlArray = referrer.split('/');
var referrerSlug = urlArray.slice(-1)[0];
jQuery("select#location > option").each(function() {
var label = this.value;
label = label.replace(/\s+/g, '-').toLowerCase(); //lower case and replace spaces with hyphens
label = label.replace("'", ""); //strip apostrophes
if (jQuery(this).hasClass(referrerSlug)) {
jQuery(this).prop('selected', true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment