Skip to content

Instantly share code, notes, and snippets.

@drinkmorewaters
drinkmorewaters / get first 12 products and their variants with variants.js
Last active June 20, 2020 03:08
Get products from the unoptimised Shopify SDK
// Build a custom products query using the unoptimized version of the SDK
const productsQuery = this.props.client.graphQLClient.query((root) => {
root.addConnection('products', {args: {first: 12}}, (product) => {
product.add('title');
product.add('tags');
product.addConnection('variants', {args: {first: 12}}, (product) => {
product.add('id')
product.add('price')
})
});
@drinkmorewaters
drinkmorewaters / savings_calc_shopify_async_await.js
Last active May 20, 2020 06:03
Shopify savings calculator ASYNC AWAIT
// Async Await version, although, i am not sure it's made things easier
(async () => {
// Change the domain name
try {
const fetchIt = await fetch(`${window.location.origin}/cart.js`)
const response = await fetchIt.json()
const data = response
const loopOver = data.items.map((d) => {
let handleURL = `${window.location.origin}/products/${d.handle}.js`
@drinkmorewaters
drinkmorewaters / savings_calculator.js
Last active May 20, 2020 06:59
Shopify savings calculator
(function cartCheck() {
let fetchIt = fetch(`${window.location.origin}/cart.js`)
.then(response => response.json())
.then((data) => {
const dataMap = data.items.map((d) => {
let handleURL = `${window.location.origin}/products/${d.handle}.js`
return handleURL
})
for (let data of dataMap) {
fetch(data)
@drinkmorewaters
drinkmorewaters / color swatch for shopify 2.js
Last active May 11, 2020 01:18
A newer version using the browsers fetch API, next will be to store the data in localstorage for quicker retrieval. (NEEDS CLEANUP)
const colorSwatchChange = async (e) => {
e.preventDefault()
let title = document.querySelector('.product-single__title')
let description = document.querySelector('#descriptionFrames')
let productPrice = document.querySelector('.product__price')
let images = document.querySelectorAll('#mainProductImages')
let productThumbs = document.querySelectorAll('#productThumbs')
let intFrameWidth = window.innerWidth;
let target = e.currentTarget
let url = `${target.dataset.url}.js`
@drinkmorewaters
drinkmorewaters / color_swatch_for_shopify_store.js
Last active May 1, 2020 01:42
A color swatch selector for a shopify store. With poor styling. You can add this to a store then use tags and product type to make it work.
{% for variant in product.variants %}
<div class="">
{% for color_option in product.options_by_name['Color'].values %}
{% assign productColor = color_option %}
{% endfor %}
</div>
{% for x in product.tags %}
{% if x != productColor %}
{% assign currentProductTags = x %}
{% endif %}
@drinkmorewaters
drinkmorewaters / class_getter.cr
Created April 29, 2020 04:24
Class with getter Crystal
class Cars
getter price : Int64
getter condition : String
def initialize(@type : String, @price : Int64, @condition : String, @location : String, @color : String); end
end
johns_car = Cars.new "Holden", 10000, "Fair", "New York", "Red"
puts "Johns car is in #{johns_car.condition} condition, although, the price is cheap at only #{johns_car.price}"
@drinkmorewaters
drinkmorewaters / struct.cr
Created April 29, 2020 04:22
Create Struct in Crystal
struct Cars
getter price : Int64
getter condition : String
def initialize(@type : String, @price : Int64, @condition : String, @location : String, @color : String)end
end
johns_car = Cars.new "Holden", 10000, "Fair", "New York", "Red"
puts "Johns car is in #{johns_car.condition} condition, although, the price is cheap at only #{johns_car.price}"
@drinkmorewaters
drinkmorewaters / record_macro.cr
Last active April 29, 2020 04:23
Record Macro for creating Struct in Crystal
record Cars, type : String, price : Int64, condition : String, location : String, color : String do
def car_return
puts "Johns car is in #{condition} condition, although, the price is cheap at only $#{price}"
end
end
Johns_car = Cars.new "Ford", 10000, "Fair", "New York", "Red"
Johns_car.car_return
class Cars {
constructor(type, price, condition, color, location) {
this.type = type
this.price = price
this.condition = condition
this.color = color
this.location = location
}
sellCar(n) {
console.log(n.type, 'has been sold.')
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const run_questions = () => {
readline.question('Enter a number ', num1 => {
readline.question('Enter a second number ', num2 => {
const num2Parsed = Number.parseInt(num2)
const num1Parsed = Number.parseInt(num1)
const sumParsed = num1Parsed + num2Parsed