Skip to content

Instantly share code, notes, and snippets.

View procarrera's full-sized avatar

Pedro Carrera procarrera

View GitHub Profile
@procarrera
procarrera / scroll_to_element.js
Created September 6, 2021 16:23
Scroll to Element full compatibility with Safari
function ScrollTo(name) {
//init thread
ScrollToResolver(document.getElementById(name));
}
function ScrollToResolver(elem) {
var jump = parseInt(elem.getBoundingClientRect().top * .2) - 30;
document.body.scrollTop += jump;
document.documentElement.scrollTop += jump;
//lastjump detects anchor unreachable, also manual scrolling to cancel animation if scroll > jump
@procarrera
procarrera / add-query-string-to-url.js
Created August 29, 2021 19:36
Add Query String to URL and fire event every time it changes
// Add query string to URL
const newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?step=1';
window.history.pushState({path:newurl},'',newurl);
// Fire event everytime state changes
window.addEventListener('popstate', function (event) {
console.log(event)
});
@procarrera
procarrera / toLocaleDateString.js
Last active August 29, 2021 15:45
toLocaleDateString en-CA
var date = new Date("2021-09-01T03:00:00.000Z");
var options = { weekday: 'long', month: 'long', day: '2-digit', year: "numeric" };
console.log(date.toLocaleDateString('en-CA', options).replace(', ',' - '));
/* OUTPUT */
// Wednesday - September 01, 2021
async function getDetails (handle) {
const response = await window.fetch(`/products/${handle}.js`)
const data = await response.json()
console.log(data)
}
@procarrera
procarrera / shopify_product_recommentation.js
Created August 26, 2021 12:59
Product Recommendations Shopify
async function productRecommentation(id, limit) {
const response = await window.fetch(`/recommendations/products.json?product_id=${id}&limit=${limit}`)
const data = await response.json()
console.log(data)
}
@procarrera
procarrera / decamelize.js
Created August 25, 2021 18:31
Transform CamelCase into separated words
function decamelize(str, separator){
separator = typeof separator === 'undefined' ? '_' : separator;
return str
.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2')
.toLowerCase();
}
@procarrera
procarrera / preconnect-preload.html
Last active August 25, 2021 18:33
Web Vitals Shopify
<link rel="preconnect" href="https://cdn.shopify.com" crossorigin>
<link rel="preconnect" href="https://fonts.shopify.com" crossorigin>
<link rel="preconnect" href="https://monorail-edge.shopifysvc.com" crossorigin>
<link rel="preload" href="{{ 'lazysizes.js' | asset_url }}" as="script">
<link rel="preload" href="{{ 'vendor.js' | asset_url }}" as="script">
<link rel="preload" href="{{ 'theme.js' | asset_url }}" as="script">
<link rel="preload" href="{{ 'theme.css' | asset_url }}" as="style">
@procarrera
procarrera / checkout_button_shopify.html
Last active August 30, 2021 03:07
Shopify Checkout Link Generator
https://[yourstore.com]/cart/[variant_id]:[quantity]?attributes[Checkout-Method]=extra-val-2&note=Sure%20why%20not
{% if cart.attributes.yourCartAttribute %}
<script>
Shopify.updateCartAttributes([{
key: 'yourCartAttribute',
value: ''
}], function(onError) {});
</script>
{% endif %}
@procarrera
procarrera / update subscriptions.js
Created July 11, 2021 15:49
ReCharge Theme Engine
// this code will update subscriptions from customer dashboard
async function skipCharge(charge_id) {
console.log(charge_id);
const charges = await manageSubscriptions("charges");
const charge = {};
charges.charges.forEach((item) => {
if (item.id === charge_id) {
charge.obj = item;
}