Metafields and Ajax add to cart

Tourist
3 0 1

Customer has some information in metafields (using the Custom Fields app) that itheywant taken into account for an ajax side cart. I'm outside of my comfort zone for sure and i'm trying to understand what i'm missing here. 

 

I've got everything running smooth as butter but ONLY when i'm logged in as an admin to the store. Here's the code that pulls/sets up the info items for the side cart. I added added two 'NOTES' to show the error spots.

 

I've got a console.log in place to see what the 'response' returns:

https://d3yl9vpu0ostigrp-8607989841.shopifypreview.com/collections/new

 

Thanks, y'all!

 

$.each(cart.items, function(index, cartItem) {
    var minAmt = '1',
        qtyAmt = '1',
        cartID = cartItem.product_id;
    $.ajax({
        beforeSend: function(xhr) {
            activeAjaxConnections++;
        },
        type: 'GET',
        url: '/admin/products/' + cartID + '/metafields.json',
        success: function(response) {
            activeAjaxConnections--;

// NOTE: 'response' incorrectly returns html instead of the json data
// looks like login html to me
console.log( JSON.stringify(response) )

var blkFound = 'bulk_quantity', minFound = 'minimum_order_quantity', minAmt = '1', qtyAmt = '1', mF = response.metafields, // NOTE: this is where the error happens that kills my cart build mfLen = mF.length;
for (var i = 0; i < mfLen; i++) { var itm = mF[i], itmKey = itm.key, itmVal = itm.value; if (itmKey == minFound && itmVal !== '1' && itmVal !== 'undefined') { var qtyAmt = itmVal; } }
/* Hack to get product image thumbnail * - If image is not null * - Remove file extension, add _small, and re-add extension * - Create server relative link * - A hard-coded url of no-image */ if (cartItem.image != null) { var prodImg = cartItem.image.replace(/(\.[^.]*)$/, "_small$1").replace('http:', ''); } else { var prodImg = "//cdn.shopify.com/s/assets/admin/no-image-medium-cc9732cb976dd349a0df1d39816fbcc7.gif"; } // Create item's data object and add to 'items' array item = { id: cartItem.variant_id, line: index + 1, // Shopify uses a 1+ index in the API url: cartItem.url, img: prodImg, name: cartItem.product_title, variation: cartItem.variant_title, properties: cartItem.properties, itemAdd: cartItem.quantity + 1, itemMinus: cartItem.quantity - 1, itemQty: cartItem.quantity, qtyJump: qtyAmt, absMin: minAmt, price: Shopify.formatMoney(cartItem.price, settings.moneyFormat), vendor: cartItem.vendor }; items.push(item); // this was the last Ajax connection, do the thing if (0 == activeAjaxConnections) { gatherData(); } }, error: function(status) { console.warn('ERROR', status); activeAjaxConnections--; // this was the last Ajax connection, do the thing if (0 == activeAjaxConnections) { gatherData(); } } }) });
0 Likes