Skip to content

Instantly share code, notes, and snippets.

@paulmiller3000
Created October 23, 2019 13:53
Show Gist options
  • Save paulmiller3000/55826c7c25785a08b874abef279d3278 to your computer and use it in GitHub Desktop.
Save paulmiller3000/55826c7c25785a08b874abef279d3278 to your computer and use it in GitHub Desktop.
Parse URL for WooCommerce
document.addEventListener('DOMContentLoaded', function() {
const currentUrl = window.location.pathname;
let parentCategory = false;
let productName = currentUrl.substr(18, (currentUrl.length - 18));
let slashCount = productName.match(/\//g);
// Count the number of slashes. If 1, this is a parent category
slashCount = slashCount ? slashCount.length : 0;
if ( slashCount == 1 ) {
parentCategory = true;
}
console.log('Is this a parent category? ', parentCategory);
if (parentCategory) {
if ( productName.endsWith('/') ) {
productName = productName.substr(0, productName.length - 1).toLowerCase();;
console.log(productName);
}
}
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment