Created
October 22, 2019 05:02
-
-
Save MarioRicalde/6b3f60a5814612f8497081c8ea4188cf to your computer and use it in GitHub Desktop.
Shopify Checkout.liquid javascript helper to parse product list.
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
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