SHARE
TWEET

Untitled

a guest Aug 22nd, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.  
  3.     function checkVariable() {
  4.      
  5.        {% if product.has_only_default_variant %}
  6.        var selectedVariable = $('input.single_variant_id').val();
  7.       {% else %}
  8.       var selectedVariable = $('select.form-options.no-js-required').find(":selected").val();
  9.       {% endif %}
  10.       // now that we have the selected variable we should use the AJAX API to find the variant ID of the product and check inventory.
  11.       jQuery.getJSON('/products/{{product.handle}}.js', function(product) {
  12.                      // Grab the length of the array from cookies then iterate through them.
  13.                      for (var i = 0; i < product.variants.length; i++) {
  14.         var variantID = product.variants[i].id; // now that we can grab the variant ID, check the inventory.
  15.         if (variantID == selectedVariable) {
  16.           var variant_avail = product.variants[i].available;
  17.           var variant_quantity = product.variants[i].inventory_quantity;
  18.           var inventory_policy = product.variants[i].inventory_policy;
  19.            
  20.           if (variant_avail) {
  21.              
  22.             if (variant_quantity < 1) {
  23.              
  24.               // Check the inventory policy that it is set to continue or deny. If deny then no pre-order.
  25.               if (inventory_policy == 'continue') {
  26.             // Show pre-order
  27.               $('.message-pre').text('Available for pre-order')
  28.              
  29.               } else {
  30.              
  31.               $('.message-pre').text('')
  32.               }
  33.              
  34.              
  35.             } else {
  36.                 // Check the inventory policy that it is set to continue or deny. If deny then no pre-order.
  37.               if (inventory_policy == 'continue') {
  38.             // Show pre-order
  39.               $('.message-pre').text('Available for pre-order')
  40.              
  41.               } else {
  42.              
  43.               $('.message-pre').text('')
  44.               }
  45.             }
  46.          
  47.           } else {
  48.      
  49.           }
  50.        
  51.         }
  52.       }
  53.       })
  54.     };
  55.  
  56.  
  57.  
  58.   // current product handle.
  59. $( document ).ready(function() {
  60.   checkVariable();
  61.   $('.form-field-select-wrapper select').on('change', function() {
  62.     setTimeout(function() { // Theme has takes a second to change main select.
  63.       checkVariable();
  64.     }, 400);
  65.   });
  66.   });
  67. </script>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top