Skip to content

Instantly share code, notes, and snippets.

View willbroderick's full-sized avatar

Will Broderick willbroderick

  • Clean Canvas Ltd
  • Nottingham, UK
View GitHub Profile
@willbroderick
willbroderick / template-variable-encoder.js
Last active June 16, 2021 06:37
Escape/encode strings used to build HTML in a template literal
/**
* Use with template literals to build HTML with correct escaping.
*
* Example:
*
* const tve = createTemplateVariableEncoder();
* tve.add('dataValue', 'What is "this"?', 'attribute');
* tve.add('title', 'Title & Heading', 'html');
* tve.add('richText', '<p>Content to show</p>', 'raw');
*
@willbroderick
willbroderick / load-once.v1.0.js
Created March 17, 2021 08:18
Script for loading JS & CSS files once (into a Shopify theme, but easy to make generic)
/*
// normal usage
theme.loadScriptOnce('https://maps.googleapis.com/maps/api/js?key=' + key, function() {
_.createMap($container);
});
// using 'before run' parameter to fix load when a site unexpectedly has RequireJS on it (by temporarily removing window.define)
if(window.define) {
theme.loadScriptOnce('https://player.vimeo.com/api/player.js', function(){
_.vimeoVars.apiReady = true;
@willbroderick
willbroderick / modelviewer-extra.js
Last active June 21, 2022 16:16
Shopify Model Viewer UI inside slideshow
/*
A Gist to keep track of what was needed to make the Shopify Model Viewer UI
propagate events in a way that allows us to use it in a slideshow/carousel.
Works in Slick and Swiper. Other JS was obviously required outside here.
This is just for reference. This was called on a loop for each 3D model.
Add this SCSS:
.shopify-model-viewer-ui {
@willbroderick
willbroderick / search.json.liquid
Last active December 13, 2021 18:08
Drop-in fallback template for Shopify Predictive Search API
{%- layout none -%}
{%- paginate search.results by 6 -%}
{%- capture products_output -%}
{%- for result in search.results -%}
{%- if result.object_type == 'product' -%}
,{"title":{{ result.title | json }},"url":{{ result.url | json }}
{%- if result.images and result.images.size > 0 -%}
,"image":{{ result.images[0] | img_url: 'master' | json }}
{%- endif -%}
,"vendor":{{ result.vendor | json }},"price_max":{{ result.price_max }},"price_min":{{ result.price_min }},"compare_at_price_min":{{ result.compare_at_price_min }}}
@willbroderick
willbroderick / instructions.txt
Created February 20, 2019 09:31
Pre-order in a Clean Themes Sectioned theme (excluding Boost)
Warning: This does NOT work with quick-buys.
1. Copy and paste the following to the bottom of Templates/product.liquid:
{% for collection in product.collections %}
{% if collection.handle == 'pre-order' %}
<script>$('[action^="/cart/add"] input[type=submit]').val('Pre order'); theme.OptionManager.strings.buttonDefault = 'Pre order';</script>
{% endif %}
{% endif %}
@willbroderick
willbroderick / load-script.js
Last active January 8, 2019 14:01
Load a script from a URL, with before/after callbacks & queueing
// Load a script from a URL, with callbacks
// If a 'before' callback is specified, script will load synchronously
{
window.scriptsLoaded = {};
window.loadScriptOnce = function(src, callback, beforeRun) {
if(typeof window.scriptsLoaded[src] === 'undefined') {
window.scriptsLoaded[src] = [];
var tag = document.createElement('script');
tag.src = src;
@willbroderick
willbroderick / show-featured-image-not-variant-image.js
Last active February 27, 2018 17:44
Show featured image on page load, not selected variant image - Symmetry Sections
@willbroderick
willbroderick / product-json.liquid
Last active November 13, 2017 15:17
Replacement for {{ product | json }} with accurate inventory information removed
{% comment %}## Add this snippet, then replace {{ product | json }} with {% include 'product-json' %} ##{% endcomment %}
{% capture product_json %}{{ product | json }}{% endcapture %}
{% assign product_json_split = product_json | split: '"inventory_quantity":' %}
{%- for str in product_json_split -%}
{%- if forloop.first -%}{{ str }}
{%- else -%}
"inventory_quantity":
{%- assign str_split = str | split: ',' -%}
{%- for str2 in str_split -%}
{%- unless forloop.first -%},{%- endunless -%}
@willbroderick
willbroderick / overflowing-tables.js
Created October 19, 2017 15:27
Overflowing tables - wrap in a scrollable container
// copy this to the very bottom of themes.js.liquid (might need to change the '.rte' depending on theme - this one's for Showcase)
$('.rte table').wrap('<div style="overflow: auto; border: 1px solid rgba(0,0,0,0.0.5)">');
@willbroderick
willbroderick / expression-two-per-row.css
Created October 18, 2017 13:41
Expression - Two products per-row on mobile
@media only screen and (max-width: 767px) {
#content .productlist .product { width: 48%; }
#content .productlist .product:nth-child(2n+1) { margin-left: 4%; }
#page-collection #content .productlist .product:nth-child(2n) { margin-left: 4%; }
#page-collection #content .productlist .product:nth-child(2n+1) { margin-left: 0; }
#content .productlist .product .foot .buttoncont, #content .productdetail .buttoncont { max-width: 100%; }
}