Skip to content

Instantly share code, notes, and snippets.

@MarioRicalde
Created October 22, 2019 05:02
Show Gist options
  • Save MarioRicalde/aade48a70d3657662c9ab6f8e0e1b5f1 to your computer and use it in GitHub Desktop.
Save MarioRicalde/aade48a70d3657662c9ab6f8e0e1b5f1 to your computer and use it in GitHub Desktop.
Shopify Checkout.liquid javascript helper to parse product list.
console.clear()
var product_rows = document.querySelectorAll('.sidebar .product-table .product')
var products_main = []
var products_accs = []
var product_main_strings = ["Regex Title"]
var product_accs_strings = ["Regex Title2", "Regex Title3"]
var matchProducts = function(i) { if ( this.product.name.match(i) ) this.array.push(this.product) }
String.prototype.processImage = function() {
return this.replace(/_small(?=\.\w+\?)/, '_large')
}
product_rows.forEach(function(row) {
var product = {
name: row.querySelector('.product__description__name').textContent,
variant: row.querySelector('.product__description__variant').textContent,
image: row.querySelector('.product-thumbnail__image').src.processImage()
}
product_main_strings.map(matchProducts, { array: products_main, product: product })
product_accs_strings.map(matchProducts, { array: products_accs, product: product })
})
console.log(products_main, products_main[0].image)
console.log(products_accs)
// Update Image
document.querySelector('.featured-image').style.backgroundImage = `url(${products_main[0].image}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment