Skip to content

Instantly share code, notes, and snippets.

@msmithstubbs
Created January 10, 2015 14:39
Show Gist options
  • Save msmithstubbs/83c905fa91e775d7f1ee to your computer and use it in GitHub Desktop.
Save msmithstubbs/83c905fa91e775d7f1ee to your computer and use it in GitHub Desktop.
Back in Stock form for Supply theme (Shopify)
<div class="BIS_form">
<label for="BIS_email">Email me when restocked</label>
<input id="BIS_email" name="email" type="email" data-product-id="{{product.id}}" placeholder="your@email.com" value="{{customer.email}}">
<button class="btn" id="BIS_button">Notify Me</button>
<p class="BIS_response"> </p>
</div>
<style>
.BIS_form {
background: #f5f5f5;
margin: 20px 0;
padding: 12px;
}
.BIS_form label {
font-weight: bold;
}
.BIS_form .BIS_response {
margin-top: 1em;
}
</style>
<script>
jQuery(function($) {
var $container = $('.BIS_form'),
$response = $container.find('.BIS_response');
var showMessage = function(msg) {
$response.text(msg);
};
var responseToMessage = function(data) {
var msg = '';
if (data.status == 'OK') {
msg = data.message; // just show the success message
} else { // it was an error
for (var k in data.errors) { // collect all the error messages into a string
msg += data.errors[k].join();
}
}
return msg;
};
var toggleForm = function(variant) {
variant.available ? $container.hide() : $container.show();
}
$container.on('click', '#BIS_button', function() {
var variantId = Shopify.urlParam('variant'),
productId = BISPopover.product.id,
email = $container.find('#BIS_email').val();
showMessage('');
BISPopover.create(email, variantId, productId).then(function(data) {
showMessage(responseToMessage(data));
});
});
var originalSelectCallback = window.selectCallback;
window.selectCallback = function(variant, selector) {
try {
toggleForm(variant);
}
finally {
originalSelectCallback.apply(window, arguments);
}
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment