Skip to content

Instantly share code, notes, and snippets.

@jgodson
jgodson / Cookies.js
Created June 21, 2020 15:27
Functions to manipulate cookies for various purposes
window.Cookies = window.Cookies || {};
// Prevent IE8-9 silent errors
if (!window.console) {
  window.console = {
    warn: function() {}
  }
}
/*
@jgodson
jgodson / replaceLiquid.js.liquid
Created June 21, 2020 15:24
JavaScript function to replace liquid args in a string with the given replacements
function replaceLiquid(string, replacements) {
  if (typeof replacements === 'string') {
    replacements = [replacements];
  }
  var regex = /{% raw %}({{\s*[\w\.]+\s*}}){% endraw %}/;
  var match = regex.exec(string);
  var index = 0;
  while (match) {
    if (replacements[index]) {
      string = string.replace(match[0], replacements[index]);
@jgodson
jgodson / stop-customers-from-using-a-discount-code.rb
Created February 12, 2018 15:28
Stop customers from using a discount code
# Stop customers from using a discount code
# This script will reject all discount codes. Useful if you have a sale
# and don't want additional discounts to be applied
# Set the rejection message for discount codes
REJECTION_MESSAGE = "Discount codes cannot be used during this sale"
class RejectAllDiscountCodes
# Initializes the campaign.
#
@jgodson
jgodson / remove-vat-outside-europe.rb
Created February 12, 2018 15:26
Remove VAT (Value Added Tax) outside of Europe
# Remove Value-Added Tax (VAT) outside of Europe
# This script will remove VAT when the destination country is not in Europe
# To be used only when the tax is included in the price of the items
# Set the VAT percentage included in product price
VAT_PERCENT = 20
# Message that will be shown to customers on the cart page
# Some themes will require code changes to support this
VAT_REMOVAL_MESSAGE = "VAT removed"
@jgodson
jgodson / checkout-script-messages.liquid
Last active November 30, 2020 21:39
Show script discount messages in checkout on your Shopify store
(function() {
// The events to listen for to append the messages
// page:change is all we really need, but the page:load will fire sooner
// so the messages will be less likely to pop-in
var events = ['page:load', 'page:change'];
events.forEach(function(event) {
document.addEventListener(event, appendScriptMessages);
});
@jgodson
jgodson / preventcontextmenu.js
Last active April 25, 2018 03:54
Prevent right click on images
document.addEventListener('contextmenu', function(evt) {
if (evt.target.nodeName === "IMG") {
evt.preventDefault();
}
});
@jgodson
jgodson / show-0-discount-codes.liquid
Last active November 12, 2020 03:36
Show script discounts applied with 0% discount code in checkout
<!-- BEGIN show 0% discount code discounts -->
<script>
(function() {
var shopCurrencyFormat = {{ 100000 | money | strip_html | json }};
var currencyPrefix = shopCurrencyFormat.match(/^(\D*)/)[1];
var currencySuffix = shopCurrencyFormat.match(/(\D*)$/)[1];
var thousandsSeperator = shopCurrencyFormat.match(/1(\D)/)[1];
var centsSeperator = shopCurrencyFormat.match(/0(\D)00/)[1];
// What needs to be replaced when grabbing the price from the DOM.
var valuesToReplace = [currencyPrefix, currencySuffix, thousandsSeperator, centsSeperator, '-'];
@jgodson
jgodson / gift-modal.md
Last active February 20, 2021 22:27
Free Gift Modal for Shopify Themes

How to install

  1. Copy the following code and paste it at the end of config/settings_schema.json, just after the last }. Then save the file.
,
  {
    "name": "Free Gift Offer",
    "settings": [
      {
        "type": "header",
        "content": "Offer a free gift with discount code on cart page"