Guest User

Untitled

a guest
Jan 3rd, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. '<form class="addtocart CartCount"method="post" action="/cart/add"><input type="hidden" name="id" value="'+ val.variants[0].id +'" /><input min="1" type="number" id="quantity" name="quantity" value="1"/><input type="submit" value="Add to cart" class="btn" /></form>'
  2.  
  3. <script>
  4.  
  5. /**
  6. * Module to ajaxify all add to cart forms on the page.
  7. *
  8. * Copyright (c) 2015 Caroline Schnapp (11heavens.com)
  9. * Dual licensed under the MIT and GPL licenses:
  10. * http://www.opensource.org/licenses/mit-license.php
  11. * http://www.gnu.org/licenses/gpl.html
  12. *
  13. */
  14. Shopify.AjaxifyCart = (function($) {
  15.  
  16. // Some configuration options.
  17. // I have separated what you will never need to change from what
  18. // you might change.
  19.  
  20. var _config = {
  21.  
  22. // What you might want to change
  23. addToCartBtnLabel: 'Add to cart',
  24. addedToCartBtnLabel: 'Thank you!',
  25. addingToCartBtnLabel: 'Adding...',
  26. soldOutBtnLabel: 'Sold Out',
  27. howLongTillBtnReturnsToNormal: 1000, // in milliseconds.
  28. cartCountSelector: '#CartCount, .cart-count, #cart-count a:first, #gocart p a, #cart .checkout em, .item-count',
  29. cartTotalSelector: '#cart-price',
  30. // 'aboveForm' for top of add to cart form,
  31. // 'belowForm' for below the add to cart form, and
  32. // 'nextButton' for next to add to cart button.
  33. feedbackPosition: 'nextButton',
  34.  
  35. // What you will never need to change
  36. addToCartBtnSelector: '[type="submit"]',
  37. addToCartFormSelector: 'form[action="/cart/add"]',
  38. shopifyAjaxAddURL: '/cart/add.js',
  39. shopifyAjaxCartURL: '/cart.js'
  40. };
  41.  
  42. // We need some feedback when adding an item to the cart.
  43. // Here it is.
  44. var _showFeedback = function(success, html, $addToCartForm) {
  45. $('.ajaxified-cart-feedback').remove();
  46. var feedback = '<p class="ajaxified-cart-feedback ' + success + '">' + html + '</p>';
  47. switch (_config.feedbackPosition) {
  48. case 'aboveForm':
  49. $addToCartForm.before(feedback);
  50. break;
  51. case 'belowForm':
  52. $addToCartForm.after(feedback);
  53. break;
  54. case 'nextButton':
  55. default:
  56. $addToCartForm.find(_config.addToCartBtnSelector).after(feedback);
  57. break;
  58. }
  59. // If you use animate.css
  60. // $('.ajaxified-cart-feedback').addClass('animated bounceInDown');
  61. $('.ajaxified-cart-feedback').slideDown();
  62. };
  63. var _setText = function($button, label) {
  64. if ($button.children().length) {
  65. $button.children().each(function() {
  66. if ($.trim($(this).text()) !== '') {
  67. $(this).text(label);
  68. }
  69. });
  70. }
  71. else {
  72. $button.val(label).text(label);
  73. }
  74. };
  75. var _init = function() {
  76. $(document).ready(function() {
  77. $(_config.addToCartFormSelector).submit(function(e) {
  78. e.preventDefault();
  79. var $addToCartForm = $(this);
  80. var $addToCartBtn = $addToCartForm.find(_config.addToCartBtnSelector);
  81. _setText($addToCartBtn, _config.addingToCartBtnLabel);
  82. $addToCartBtn.addClass('disabled').prop('disabled', true);
  83. // Add to cart.
  84. $.ajax({
  85. url: _config.shopifyAjaxAddURL,
  86. dataType: 'json',
  87. type: 'post',
  88. data: $addToCartForm.serialize(),
  89. success: function(itemData) {
  90. // Re-enable add to cart button.
  91. $addToCartBtn.addClass('inverted');
  92. _setText($addToCartBtn, _config.addedToCartBtnLabel);
  93. _showFeedback('success','<i class="fa fa-check"></i> Added to cart! <a href="/cart">View cart</a> or <a href="/collections/all">continue shopping</a>.',$addToCartForm);
  94. window.setTimeout(function(){
  95. $addToCartBtn.prop('disabled', false).removeClass('disabled').removeClass('inverted');
  96. _setText($addToCartBtn,_config.addToCartBtnLabel);
  97. }, _config.howLongTillBtnReturnsToNormal);
  98. // Update cart count and show cart link.
  99. $.getJSON(_config.shopifyAjaxCartURL, function(cart) {
  100. if (_config.cartCountSelector && $(_config.cartCountSelector).size()) {
  101. var value = $(_config.cartCountSelector).html() || '0';
  102. $(_config.cartCountSelector).html(value.replace(/[0-9]+/,cart.item_count)).removeClass('hidden-count');
  103. }
  104. if (_config.cartTotalSelector && $(_config.cartTotalSelector).size()) {
  105. if (typeof Currency !== 'undefined' && typeof Currency.moneyFormats !== 'undefined') {
  106. var newCurrency = '';
  107. if ($('[name="currencies"]').size()) {
  108. newCurrency = $('[name="currencies"]').val();
  109. }
  110. else if ($('#currencies span.selected').size()) {
  111. newCurrency = $('#currencies span.selected').attr('data-currency');
  112. }
  113. if (newCurrency) {
  114. $(_config.cartTotalSelector).html('<span class=money>' + Shopify.formatMoney(Currency.convert(cart.total_price, "{{ shop.currency }}", newCurrency), Currency.money_format[newCurrency]) + '</span>');
  115. }
  116. else {
  117. $(_config.cartTotalSelector).html(Shopify.formatMoney(cart.total_price, "{{ shop.money_format | remove: "'" | remove: '"' }}"));
  118. }
  119. }
  120. else {
  121. $(_config.cartTotalSelector).html(Shopify.formatMoney(cart.total_price, "{{ shop.money_format | remove: "'" | remove: '"' }}"));
  122. }
  123. };
  124. });
  125. },
  126. error: function(XMLHttpRequest) {
  127. var response = eval('(' + XMLHttpRequest.responseText + ')');
  128. response = response.description;
  129. if (response.slice(0,4) === 'All ') {
  130. _showFeedback('error', response.replace('All 1 ', 'All '), $addToCartForm);
  131. $addToCartBtn.prop('disabled', false);
  132. _setText($addToCartBtn, _config.soldOutBtnLabel);
  133. $addToCartBtn.prop('disabled',true);
  134. }
  135. else {
  136. _showFeedback('error', '<i class="fa fa-warning"></i> ' + response, $addToCartForm);
  137. $addToCartBtn.prop('disabled', false).removeClass('disabled');
  138. _setText($addToCartBtn, _config.addToCartBtnLabel);
  139. }
  140. }
  141. });
  142. return false;
  143. });
  144. });
  145. };
  146. return {
  147. init: function(params) {
  148. // Configuration
  149. params = params || {};
  150. // Merging with defaults.
  151. $.extend(_config, params);
  152. // Action
  153. $(function() {
  154. _init();
  155. });
  156. },
  157. getConfig: function() {
  158. return _config;
  159. }
  160. }
  161. })(jQuery);
  162.  
  163. Shopify.AjaxifyCart.init();
  164.  
  165. </script>
  166.  
  167. {% comment %}
  168. If you want to animate your feedback message.
  169. {% endcomment %}
  170.  
  171. {% comment %}
  172. {{ '//cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.0/animate.min.css' | stylesheet_tag }}
  173. {% endcomment %}
  174.  
  175. {{ '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css' | stylesheet_tag }}
  176.  
  177. <style>
  178. .ajaxified-cart-feedback {
  179. display: block;
  180. line-height: 36px;
  181. font-size: 90%;
  182. vertical-align: middle;
  183. }
  184. .ajaxified-cart-feedback.success {
  185. color: #3D9970;
  186. }
  187. .ajaxified-cart-feedback.error {
  188. color: #FF4136;
  189. }
  190. .ajaxified-cart-feedback a {
  191. border-bottom: 1px solid;
  192. }
  193. </style>
  194.  
  195. <a href="/cart" class="site-header__cart">
  196. {% include 'icon-cart' %}
  197. <span class="visually-hidden">{{ 'layout.cart.title' | t }}</span>
  198. <span class="icon__fallback-text">{{ 'layout.cart.title' | t }}</span>
  199. {% if cart.item_count > -1 %}
  200. <div id="CartCount" class="site-header__cart-count">
  201. <span>{{ cart.item_count }}</span>
  202. <span class="icon__fallback-text medium-up--hide">{{ 'layout.cart.items_count' | t: count: cart.item_count }}</span>
  203. </div>
  204. {% include 'ajaxify-cart' %}
  205. {% endif %}
  206. </a>
Add Comment
Please, Sign In to add comment