Cart & Checkout JS Customization

zaczac Member
in Help edited November 2013
Hello. I have a two script tweaks needed to get our cart and checkout working properly now that we have added coupon codes. Due to the fact that our prices start with $0 and then we add from there, it is possible in some instances to add an item into the card with a $0 price and to prevent it we added:

Cart code:

/* <![CDATA[ */
function cart_quantity_check() {
// remove checkout if less than $0.01
if (fc_json.total_price < 0.01) {
fc_PreventCheckout();
// Display a notification message
var noticeText = "Please revise or add item(s) to cart in order to checkout.";

However, now that we are offering coupon codes / gift certificates the price may legitimately go to $0 and we want to allow that checkout only if a coupon code was used to get to $0. Therefore we need additional code to achieve that.


Secondly, we have the following custom code for checkout but now need to remove the shipping and handling regardless of location if a gift card only is in the cart. If there is a gift card and any other item the shipping still needs to be charged. So we need some more code for that.

Thanks in advance for the help!


Checkout code:

/* BEGIN CUSTOM SHIPPING LOGIC */


var country_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_country").val() : $("#customer_country").val());

switch (country_code) {
case "US":
if (fc_json.total_price>= 100) {
shippingCost = 0;
$("#fc_cart_foot_shipping").find("td.fc_col1").html('Free Shipping on Domestic Orders Over $100:');
} else {
shippingCost = 5.95;
}
break;
default:
shippingCost = 8.95;
}
var sub_tot = fc_json.total_price;

if(sub_tot <= 20) {
shippingCost = 0;
$("#fc_cart_foot_shipping").find("td.fc_col1").html('No Shipping Charges for Donations Only:');

}


/* END CUSTOM SHIPPING LOGIC */
Comments
  • fc_adamfc_adam FoxyCart Team
    For the cart code, you'll basically want to see if there is a coupons node in the JSON, like this:
    function cart_quantity_check() {
    // remove checkout if less than $0.01
    if (fc_json.total_price < 0.01 && !fc_json.hasOwnProperty('coupons')) {
    fc_PreventCheckout();
    // Display a notification message
    var noticeText = "Please revise or add item(s) to cart in order to checkout.";
    


    For the checkout, you'll need to loop through the products and check if the gift card is present, and if it's the only product. This code would do that:
    var only_giftcard_present = false;
    for (var p = 0; p < fc_json.products.length; p++) {
      if (fc_json.products[p].code == "giftcard") {
        only_giftcard_present = (fc_json.products[p].quantity == fc_json.product_count);
      }
    }
    

    And you could put this at the end of your custom shipping block to set it to 0 in that case:
    if(only_giftcard_present) {
    shippingCost = 0;
    $("#fc_cart_foot_shipping").find("td.fc_col1").html('No Shipping Charges for Gift Cards Only:');
    }
    
  • You're definitely on the holiday shopping list @fc_adam ;)

    Many thanks as always!
  • fc_adamfc_adam FoxyCart Team
    @zac - I wear a medium :)

    Just kidding - glad that worked as I didn't test it too thoroughly.
Sign In or Register to comment.