The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

FC 2 Migrating - converting JSON

What are the correct JSON references in V2 for :
fc_json.products.length
fc_json.products[p].category
fc_json.products[p].options["Format"]
var shipitem = 0;
for (var p = 0; p < fc_json.products.length; p++) {
if (fc_json.products[p].category == "carddecks") {
shipitem +=1;
}
if (fc_json.products[p].category == "cddvd") {
shipitem +=1;
}
if (fc_json.products[p].category == "SaladHostedDownloads") {
shipitem +=1;
}
if (fc_json.products[p].options["Format"] == "MP3 (audio)") {
shipitem -=1;
}
if (fc_json.products[p].options["Format"] == "PDF") {
shipitem -=1;
}
};
if (shipitem < 1) {
shippingCost = 0;
}
Background : I have products in Perch which are downloads as default, but can have option of shippable CD or DVD. So I need to check the format of a product ordered to overwrite shipping to nil if only a download. Above snippet worked in V1.1, now looking to add to FCV2
Comments
  • fc_adamfc_adam FoxyCart Team
    edited October 2015
    @timconsidine,

    Products are now at FC.json.items, and you'd access length and category the same way. Options is slightly different though - they lost the key value in 2.0. Your code would look like this:
    var shipitem = 0;
    for (var p = 0; p < FC.json.items.length; p++) {
    if (FC.json.items[p].category == "carddecks") {
    shipitem +=1;
    }
    if (FC.json.items[p].category == "cddvd") {
    shipitem +=1;
    }
    if (FC.json.items[p].category == "SaladHostedDownloads") {
    shipitem +=1;
    }
    for (var o = 0; o < FC.json.items[p].options.length; o++) {
    if (FC.json.products[p].options[o].name == "Format") {
    if (FC.json.products[p].options[o].value == "MP3 (audio)") {
    shipitem -=1;
    }
    if (FC.json.products[p].options[o].value == "PDF") {
    shipitem -=1;
    }
    }
    }
    };
    if (shipitem < 1) {
    shippingCost = 0;
    }
  • Thank you fc_adam
  • On reflection I decided to make use of product weights and go for a simple over-ride
      
    if (FC.json.total_weight = 0) {
    FC.customFlatRates.update(1, 0, 'Digital', 'Download');
    }
Sign In or Register to comment.