Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- function checkVariable() {
- {% if product.has_only_default_variant %}
- var selectedVariable = $('input.single_variant_id').val();
- {% else %}
- var selectedVariable = $('select.form-options.no-js-required').find(":selected").val();
- {% endif %}
- // now that we have the selected variable we should use the AJAX API to find the variant ID of the product and check inventory.
- jQuery.getJSON('/products/{{product.handle}}.js', function(product) {
- // Grab the length of the array from cookies then iterate through them.
- for (var i = 0; i < product.variants.length; i++) {
- var variantID = product.variants[i].id; // now that we can grab the variant ID, check the inventory.
- if (variantID == selectedVariable) {
- var variant_avail = product.variants[i].available;
- var variant_quantity = product.variants[i].inventory_quantity;
- var inventory_policy = product.variants[i].inventory_policy;
- if (variant_avail) {
- if (variant_quantity < 1) {
- // Check the inventory policy that it is set to continue or deny. If deny then no pre-order.
- if (inventory_policy == 'continue') {
- // Show pre-order
- $('.message-pre').text('Available for pre-order')
- } else {
- $('.message-pre').text('')
- }
- } else {
- // Check the inventory policy that it is set to continue or deny. If deny then no pre-order.
- if (inventory_policy == 'continue') {
- // Show pre-order
- $('.message-pre').text('Available for pre-order')
- } else {
- $('.message-pre').text('')
- }
- }
- } else {
- }
- }
- }
- })
- };
- // current product handle.
- $( document ).ready(function() {
- checkVariable();
- $('.form-field-select-wrapper select').on('change', function() {
- setTimeout(function() { // Theme has takes a second to change main select.
- checkVariable();
- }, 400);
- });
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement