Skip to content

Instantly share code, notes, and snippets.

@aaliyan111
Forked from MarioRicalde/shopify_checkout.js
Created October 9, 2023 10:16
Show Gist options
  • Save aaliyan111/3f691183b0afcd139e4a0885b7c2bbc0 to your computer and use it in GitHub Desktop.
Save aaliyan111/3f691183b0afcd139e4a0885b7c2bbc0 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