Created
January 29, 2018 22:26
-
-
Save boldsupport/2a034e74f68c269188d2224109de32c8 to your computer and use it in GitHub Desktop.
Linked to from: https://support.boldcommerce.com/hc/en-us/articles/360000120406
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="application/javascript"> | |
if (typeof BOLD === 'undefined') { | |
BOLD = {}; | |
} | |
BOLD.checkout = { | |
getCookie: function getCookie(name) { | |
var value = '; ' + document.cookie; | |
var parts = value.split('; ' + name + '='); | |
if (parts.length === 2) { | |
return parts.pop().split(';').shift(); | |
} | |
}, | |
getPath: function getPath(url) { | |
var parser = document.createElement('a'); | |
parser.href = url; | |
return parser.pathname; | |
}, | |
isCheckoutButton: function isCheckoutButton(elem) { | |
if (elem.form) { | |
var action = elem.form.getAttribute('action'); | |
if (action.search(/^\/checkout\b/) !== -1) { | |
return true; | |
} | |
if (action.search(/^\/cart\b/) !== -1 && elem.getAttribute('name') === 'checkout') { | |
return true; | |
} | |
var path = BOLD.checkout.getPath(action); | |
if (path.search(/^\/checkout\b/) !== -1) { | |
return true; | |
} | |
if (path.search(/^\/cart\b/) !== -1 && elem.getAttribute('name') === 'checkout') { | |
return true; | |
} | |
} | |
return false; | |
}, | |
initialize: function initialize() { | |
document.addEventListener('click', function (event) { | |
if (BOLD.checkout.isCheckoutButton(event.target)) { | |
event.target.form.action = '/apps/checkout/begin-checkout?shop=' + Shopify.shop; | |
var cartCookie = BOLD.checkout.getCookie('cart'); | |
var element = document.createElement('INPUT'); | |
element.type = 'HIDDEN'; | |
element.name = 'cart_id'; | |
element.value = cartCookie; | |
event.target.form.appendChild(element); | |
if (typeof BOLDCURRENCY !== 'undefined') { | |
var currencyElement = document.createElement('INPUT'); | |
currencyElement.type = 'HIDDEN'; | |
currencyElement.name = 'bold_cart_params[currency_iso_name]'; | |
currencyElement.value = BOLDCURRENCY.currencyCookie.read(); | |
event.target.form.appendChild(currencyElement); | |
} | |
} | |
}); | |
} | |
}; | |
(function () { | |
BOLD.checkout.initialize(); | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment