Skip to content

Instantly share code, notes, and snippets.

View bjornbennett's full-sized avatar
🏕️
Enjoying Covalence Full-Stack Dev Bootcamp

Bjorn Bennett bjornbennett

🏕️
Enjoying Covalence Full-Stack Dev Bootcamp
View GitHub Profile
@bjornbennett
bjornbennett / triggerClickEvent
Created May 3, 2018 03:46
jQuery - trigger events on click of any target class that you add
//--------------------------------------------------
// More detailed
//--------------------------------------------------
// execute on click
$( ".the-target" ).on( "click", function(e) {
e.preventDefault();
// do something meaningful...
console.log('do something');
});
// attach click event to trigger above
@bjornbennett
bjornbennett / stickyHeader.liquid
Created May 3, 2018 03:43
Shopify Liquid - This snippet provides a sticky header for your theme, utilizing jQuery.
<!-- HTML CODE -->
{%include 'header' %}
<div class="header__wrapper__extra">
{%include 'header' %}
</div>
<!-- SCSS -->
<style>
/* ===========================================
@bjornbennett
bjornbennett / showCollectionAnywhere.liquid
Created May 3, 2018 03:41
Shopify Liquid - This snippets shows a collections products anywhere. It grabs their image information, and outputs it to a new image. The product title displays in an h4 tag. Wrap these in div containers to control flow. Also limit the amount of images to create a set amount.
{% for product in collections.example.products %}
<img src="{{ product.images[0] | product_img_url: "compact" }}" alt="{{ product.title | escape }}" />
<h4>{{ product.title }}</h4>
{% endfor %}
@bjornbennett
bjornbennett / product-url-update-variant.liquid
Created May 3, 2018 03:30
Shopify Liquid - Some themes that I have used, wouldn't show which variant you were viewing when on it's product page, or wouldn't update when you changed the variant. For various reasons, I needed this information to be available within the URL. This snippet updates the URL when the options are changed. Super helpful.
<!-- ================================== NOTES ============================================= -->
<!-- Print out the variant titles and ids to a hidden div from which to reference -->
<!-- Also strips unnecessary spaces on both sides of the string-->
<div class="btc_container" style="display: none;">
{% for variant in product.variants %}
{% if variant.available %}
{%assign vt1 = variant.title | split: '/' | first | strip %}
{%assign vt2 = variant.title | split: '/' | last | strip %}
<span class="btc-variant-container" data-prod-option-1="{{vt1}}" data-prod-option-2="{{vt2}}" data-var-id="{{variant.id}}">{{vt1}}^{{vt2}}</span>
{% endif %}
@bjornbennett
bjornbennett / product-get-specific-product.liquid
Created May 3, 2018 03:25
Shopify Liquid - With this Liquid snippet, you are able to grab variants of a product, assign it to an option within a Select HTML element. This also grabs the product sku number, variant value, title, and variant URL. Currently set to 3, but can be extended for however many you need.
{% comment %}Assign products set in Settings to variables{% endcomment %}
{% assign productOne = all_products[settings.product_one] %}
{% assign productTwo = all_products[settings.product_two] %}
{% assign productThree = all_products[settings.product_three] %}
<!--Output variant info to option elements within a Select HTML element-->
<select>
{%for variant in productOne.variants%}
<option sku="{{variant.sku}}" value="{{variant.url | split: '?variant=' | last}}">1 - {{variant.title}} - {{productOne.url}}</option>
{%endfor%}
@bjornbennett
bjornbennett / Shopify-popup-modal.liquid
Last active July 26, 2021 16:02
Shopify jQuery Cookie Modal - This snippet adds a popup modal that use the js cookie jQuery plugin - https://github.com/js-cookie/js-cookie. Cookie expiration is controlled via settings for the client to control.
<!-- MODAL - insert this in the main template so it is available for any page -->
{% if settings.enable-home-newsletter-modal != blank%}
<div class="tbm_modal-container">
<div class="tbm_modal">
<div class="tbm_modal-left-col" style="background-image:url({{ settings.tbm_modal_bg_image | img_url: 'original' }});">
</div>
<div class="tbm_modal-right-col">
<h2>{{settings.tbm_cookie-modal-title}}</h2>
<p>{{settings.tbm_cookie-modal-content}}</p>
<!--FORM-->
@bjornbennett
bjornbennett / Shopify-homepage_blog.liquid
Created May 3, 2018 03:09
Shopify Liquid - This Snippet is helpful if you want a blog on your homepage, with control via settings for the client.
<!-- this Case grabs the blog count set by the client in settings, and sets the classes that will adjust the containers -->
{% case settings.homepage_blog_count %}
{% when '0' %}
{% when '1' %}
{% assign homepage_blog_grid = 'large--one-half push--large--one-quarter' %}
{% when '2' %}
{% assign homepage_blog_grid = 'large--one-half' %}
{% when '3' %}
{% assign homepage_blog_grid = 'large--one-third' %}
{% endcase %}
@bjornbennett
bjornbennett / forLoop-SettingsRepetition.liquid
Created May 3, 2018 03:04
Shopify Liquid - With this snippet, you will be able to output multiple custom settings via a For Loop. Useful when you want to output multiple sections
<!-- LOOPS THROUGH SETTINGS VARIABLES, OUTPUTS VIA A FOR-LOOP -->
<div class="container sixteen columns clearfix">
{% for i in (1..7) %}
{% capture tbm_title_setting %}identify_title{{i}}{%endcapture%}
{% capture tbm_content_setting %}identify_content{{i}}{%endcapture%}
{% capture tbm_link_setting %}identify_link{{i}}{%endcapture%}
{% assign tbm_title = settings[tbm_title_setting] %}
@bjornbennett
bjornbennett / divideProdDescriptUp.liquid
Last active March 11, 2021 13:48
Shopify Liquid - Divide product descriptions for more control
{% assign description_parts = product.description | split: '<!--split-->' %}
{%comment%}
Due to a limitation within Shopify, you have one section to write a general product description.
However, if you'd like to split sections and control them via CSS/jQuery/whatever, this provided a way to do that.
You may want the client to have access to editing the content, but you need to style the sections.
To divide sections, insert <!--split--> when you want a new section.
Shown below are snippets to use to output various sections.
Amount of parts: {{ description_parts.size }}
@bjornbennett
bjornbennett / AJAX-cart-item-count.js
Created May 3, 2018 02:48
Shopify Liquid / jQuery - Get cart item count
// on anchor click, after 300ms, fetch cart count.
$('a').click(function(){
setTimeout(function(){
$.getJSON('/cart.js', function(cart){
var result = cart.item_count;
console.log(result);
});
},300);
});