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.

Discount a specific product category if cart total reaches a certain amount

I'm trying to set up a coupon code that says "if cart total is $X, then make a certain product free, if it's in the cart".
I have the special product in it's own foxy category, and I have the coupon set up to only discount that category. That part is fine, except it's only watching the cart total for THAT category, not all categories. I need it to watch the total for all categories, and then apply the discount to that certain product category only.

Is there an established way to do this outside of coupon codes?

Thanks!
Tagged:
Comments
  • fc_adamfc_adam FoxyCart Team
    @matt633,

    Unfortunately that type of discount isn't possible with our current coupons set up. You're correct that to discount a certain product you can restrict it to certain categories (and in newer versions you can restrict it to certain product codes as well), but that also causes the tiers to only look at the products the coupon affects. We don't currently have a way to look at the total price or quantity of a larger subset of products than the coupon applies to.

    The best way to tackle what you're after there would be with some custom javascript - that checks for the presence of the product when that coupon is present. If the product isn't present, it would remove the coupon.
  • Hi @fc_adam - thanks for your help. I've looked through the documentation, and I've seen the bits on auto-adding a coupon, but nothing like what you mentioned. Do you have any existing examples?
  • fc_adamfc_adam FoxyCart Team
    @matt633,

    So there is two parts to it. First part is looping through the products, and the second would be to send off the JSONP request to remove the coupon. Could you confirm what store version you're working with?
  • Yes @fc_adam, it's 1.1
  • fc_adamfc_adam FoxyCart Team
    @matt633,

    So a basic example to help get you started would look like this:
    <script type="text/javascript">jQuery(document).ready(function() {
    var isPresent = false;
    var coupon = 0;
    for (var i = 0; i < fc_json.products.length; i++) {
    if (fc_json.products[i].code == "myproduct") {
    isPresent = true;
    }
    }
    if(fc_json.hasOwnProperty('coupons')) {
    jQuery.each(fc_json.coupons, function(i, coupon){
    if(coupon.name.search(/coupon name/i) > -1) {
    hasCoupon = coupon.id;
    }
    });
    }
    if (coupon > 0 && !hasProduct) {
    jQuery.getJSON('https://YOURSTORE.foxycart.com/cart?fcsid='+fc_json.session_id+'&cart=remove_coupon&coupon_code_id='+coupon+'&output=json&callback=?', function(data) {
    fc_UpdateCart();
    });
    }
    });
    </script>
    You'll need to update "YOURSTORE", the "myproduct" product code and "coupon name" to the coupon name - but including that in your cart template will force the coupon to be removed if the product isn't there.
  • Hi @fc_adam,

    We made a couple of changes to get this to work, but it's great. It only works on the cart (v1.1 checkout with custom coupon code does not refresh upon coupon changes), but the cart is all we need. Here's the updated code, in case anyone ever needs it.

    <script type="text/javascript">
    jQuery(document).ready(function() {
    var isPresent = false;
    var coupon_id = 0;
    var code = 0;
    //console.log(fc_json);
    for (var i = 0; i < fc_json.products.length; i++) {
    code = fc_json.products[i].code;
    if (code == "PRODUCTSKU") {
    isPresent = true;
    }
    }
    if(!isPresent){
    if(fc_json.hasOwnProperty('coupons')) {
    jQuery.each(fc_json.coupons, function(i, coupon){
    if(coupon.name == "COUPON"){
    coupon_id = coupon.id;
    }
    });
    }
    if(coupon_id > 0) {
    jQuery.getJSON('https://YOURSTORE.foxycart.com/cart?fcsid='+fc_json.session_id+'&cart=remove_coupon&coupon_code_id='+coupon_id+'&output=json&callback=?', function(data) {
    fc_UpdateCart();
    });
    }
    }
    });
    </script>
  • fc_adamfc_adam FoxyCart Team
    @matt633,

    Awesome - glad it was helpful for you!
Sign In or Register to comment.