Rnguy February 2016

Product ID code transferring improperly over to Shopify checkout page

I currently have an online store that uses shopify's checkout page. The information regarding the customers shopping cart is all done through my site built around node.js and then sends the cart information over to the shopify checkout page where they're able to pay for the order.

Previously the shopping cart would send over the product ID's and quantity number of each product to Shopify in the form of a URL once the "checkout" button was pressed in the following format:

shop.mysite.com/cart/product1:qty,product2:qty,product3:qty,product4:qty

However, now Shopify is no longer allowing for a 0 value of "qty" and lands you on an error 410 page. I now need to change my code to reflect when the productID has a zero quantity, it won't show up in the URL. Current code for the button is as follows:

function updateShopifyCart(){
        var cBBM = ",product2:";
        var co = ":";
        var caHref = 'http://shop.mysite.com/cart/product1:';
        var cMicro = ',product3:';
        var cPower = ',product4:';

        var bbQ = getStoredCartItemAmount('Product1name');
        var bbmQ = getStoredCartItemAmount('Product2name');
        var musbQ = getStoredCartItemAmount('Product3name');
        var powersQ = getStoredCartItemAmount('product4name');

        var cHref = caHref + bbQ + cBBM + bbmQ + cMicro + musbQ + cPower + powersQ;
        $("#checkoutButton").attr("href", cHref);
    }

I'm guessing a simple if;else statement would probably be able to solve this, however I'm not sure exactly how to implement that for every productID.

Any help appreciated, thanks! :)

Answers


Marco Altieri February 2016

I would suggest to refactor your code because otherwise it would be very difficult to write the conditions that you need:

function updateShopifyCart(){
    var products = ["product1", "product2", "product3", "product4"];
    var caHref = "http://shop.mysite.com/cart/";

    var url = caHref;
    var first = true;
    for (var i = 0; i < products.length; i++) {
        var prod = products[i];
        var qty = getStoredCartItemAmount(prod);
        if (qty > 0) {
           if (!first) {
               url += ",";
           }
           first = false;
           url += prod + ":" + qty;
        }
    }

    $("#checkoutButton").attr("href", url);
}

Post Status

Asked in February 2016
Viewed 2,070 times
Voted 11
Answered 1 times

Search




Leave an answer