Last active
May 9, 2021 19:21
-
-
Save vishalarora91/2768f4279a46c2a8a8b9c571bd80b954 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| $(function() { | |
| var currentAjaxRequest = null; | |
| var searchForms = $('form[action="/search"]').each(function() { | |
| var input = $(this).find('input[name="q"]'); | |
| $('<ul class="search-results"></ul>').appendTo($(this)); | |
| input.attr('autocomplete', 'off').bind('keyup change', function() { | |
| var term = $(this).val(); | |
| var form = $(this).closest('form'); | |
| var searchURL = '/search?type=product&q=' + term; | |
| var resultsList = form.find('.search-results'); | |
| if (term.length > 0 && term != $(this).attr('data-old-term')) { | |
| $(this).attr('data-old-term', term); | |
| if (currentAjaxRequest != null) currentAjaxRequest.abort(); | |
| var ajaxData = { | |
| "resources": { | |
| "type": 'product,collection', | |
| "limit": 6, | |
| "options": { | |
| "unavailable_products": 'last', | |
| "fields": 'title,product_type,variants.title' | |
| } | |
| } | |
| }; | |
| var url2 = '&resources[type]=product,collection' | |
| currentAjaxRequest = $.getJSON({ | |
| 'url': "/search/suggest.json?q=" + term + url2, | |
| 'type': 'GET', | |
| 'dataType': 'json', | |
| data: ajaxData, | |
| 'success': function(data) { | |
| resultsList.empty(); | |
| if (data.resources.results.products.length == 0) { | |
| resultsList.hide(); | |
| } else { | |
| resultsList.append('<li class="pshead">Products</li>'); | |
| $.each(data.resources.results.products, function(index, item) { | |
| var link = $('<a></a>').attr('href', item.url); | |
| var image_url = item.featured_image.url; | |
| if (image_url !== null) { | |
| var extension = image_url.split('.').pop(); | |
| var image = image_url.replace('.' + extension, '_thumb') + '.' + extension; | |
| } else { | |
| var image = 'https://cdn.shopify.com/shopifycloud/shopify/assets/no-image-2048-5e88c1b20e087fb7bbe9a3771824e743c244f437e4f8ba93bbf7b11b53f7824c_40x.gif' | |
| } | |
| link.append('<div class="thumbnail"><img src="' + image + '" /></div>'); | |
| link.append('<div class="content"><span class="title">' + item.title + '</span><span class="price">Rs. ' + item.price + '</span></div>'); | |
| link.wrap('<li class="pred-item"></li>'); | |
| resultsList.append(link.parent()); | |
| }); | |
| if (data.resources.results.collections.length == 0) {} else { | |
| resultsList.append('<li class="pshead col">Collections</li>'); | |
| $.each(data.resources.results.collections, function(index, item) { | |
| var link = $('<a></a>').attr('href', item.url); | |
| link.append('<span>' + item.title + '</span>') | |
| link.wrap('<li class="pred-item pred-search-tit"></li>'); | |
| resultsList.append(link.parent()); | |
| }); | |
| } | |
| // if(data.resources.results.products.length+data.resources.results.collections.length > 10) { | |
| resultsList.append('<li class="pred-item"><span class="title"><a href="' + searchURL + '">See all results</a></span></li>'); | |
| // } | |
| resultsList.fadeIn(200); | |
| } | |
| } | |
| }); | |
| } | |
| }); | |
| }); | |
| $('body').bind('click', function() { | |
| $('.search-results').hide(); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment