Created
October 23, 2019 13:53
-
-
Save paulmiller3000/55826c7c25785a08b874abef279d3278 to your computer and use it in GitHub Desktop.
Parse URL for WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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