简体   繁体   English

Shopify 异步 Ajax 添加到购物车 - 部分工作?

[英]Shopify Asynchronous Ajax Add to Cart - Working Partially?

I am building a special Shopify page, where multiple items can be added to the cart via Shopify's Ajax API.我正在构建一个特殊的 Shopify 页面,其中可以通过 Shopify 的 Ajax API 将多个项目添加到购物车。 I set up a test version of the page here: https://monstermuffin.com/pages/ajax-test我在这里设置了页面的测试版本: https : //monstermuffin.com/pages/ajax-test

Here is the js code I have currently to set up the asynchronous call / add to cart:这是我目前用于设置异步调用/添加到购物车的 js 代码:

 Shopify.queue = []; Shopify.moveAlong = function() { if (Shopify.queue.length) { var request = Shopify.queue.shift(); Shopify.addItem(request.variantId, request.quantity, request.properties, Shopify.moveAlong); } else { //document.location.href = '/cart'; } }; Shopify.addItem = function(id, qty, properties, callback) { var params = { quantity: qty, id: id, properties: properties }; $.ajax({ type: 'POST', url: '/cart/add.js', dataType: 'json', data: params, success: function(){ if(typeof callback === 'function'){ callback(); } }, error: function(){ } }); } function push_to_queue(variantID, quantity, properties,callback) { Shopify.queue.push({ variantId: variantID, quantity: quantity, properties: properties }); if(typeof callback === 'function'){ callback(); } } $('#add-helmet-panties').click(function() { $('.quantity-field:visible').each(function() { var thisVariant = $(this).prop('id'); var thisQuantity = parseInt($(this).val(), 10) || 0; var theseProps = { 'Base Colour': $('#base-colour').val() } push_to_queue(thisVariant, thisQuantity, theseProps, Shopify.moveAlong); }); });

Oddly, it is currently adding only 3 or 4 of the products to the cart at a time.奇怪的是,它目前一次只能将 3 或 4 种产品添加到购物车中。

I've really struggled with this!我真的很挣扎! Any advice would be appreciated.任何意见,将不胜感激。 I can provide more information if needed!如果需要,我可以提供更多信息!

It is simple... you want your click listener to simply iterate the NON-ZERO variants, and add them to the queue.这很简单……您希望单击侦听器简单地迭代非零变体,并将它们添加到队列中。 So call push_to-queue.所以调用push_to-queue。 Do not bother with the call to move along.不要打扰电话继续前进。 Instead, once the queue is all filled, then you call move along.相反,一旦队列全部填满,你就调用移动。

It is funny, I wrote the pseudo-code for this originally, and Caro at Shopify re-wrote it into this version, now public for almost ten years... neat.有趣的是,我最初为此编写了伪代码,Shopify 的 Caro 将其重新编写成这个版本,现在已经公开了近十年......整洁。

Also it seems your concept of the base colour property is off?此外,您对基色属性的概念似乎已关闭? It is being applied to all the variants.它正在应用于所有变体。 Why bother?何苦? Instead, set it as a cart attribute where the product has a base colour, once.相反,将其设置为购物车属性,其中产品具有基色,一次。 More efficient.更高效。 Makes more sense.更有意义。

 var length = {{ product.variants.size }}; $(document).ready(function () { $("#quantity-0").focus(); $("#submit-table").click(function(e) { e.preventDefault(); //array for Variant Titles var toAdd = new Array(); var qty ; for(i=0; i < length; i++){ toAdd.push({ variant_id: $("#variant-"+i).val(), quantity_id: $("#quantity-"+i).val() || 0 }); } function moveAlong(){ if (toAdd.length) { var request = toAdd.shift(); var tempId= request.variant_id; var tempQty = request.quantity_id; var params = { type: 'POST', url: '/cart/add.js', data: 'quantity='+tempQty+'&id='+tempId, dataType: 'json', success: function(line_item) { alert("Product Added to Cart"); moveAlong(); }, error: function() { //console.log("fail"); moveAlong(); } }; $.ajax(params); } else { document.location.href = '/cart'; } }; moveAlong(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form class="page-width"> <table> <tr> <th>Name</th> <th>Quantity</th> <th>Stock</th> </tr> {% for variant in product.variants %} <tr> <td>{{ variant.title }} - {{ variant.price | money }}</td> <td> <input type="hidden" value="{{ variant.id }}" id="variant-{{ forloop.index0 }}"/> <input type="number" value="0" id="quantity-{{ forloop.index0 }}"/> </td> <td>{{ variant.inventory_quantity }} in stock.</td> </tr> {% endfor %} </table> <div class="purchase-section{% if product.variants.size > 1 %} multiple{% endif %}"> <div class="purchase"> {% unless product.available %} <p>Sold Out</p> {% else %} <br /> <input type="submit" value="Add!" class="btn" id="submit-table"/> {% endunless %}<input type="reset" class="btn" value="Reset"> </div> </div> </form> </div> <script type="text/javascript" charset="utf-8"> //<![CDATA[ // Including jQuery conditionally. if (typeof jQuery === 'undefined') { document.write({{ "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" | script_tag | json }}); document.write('<script type="text/javascript">jQuery.noConflict();<\\/script>'); } //]]> </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM