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.

Prevent Adding To Basket If Product In Specified Category Has Already Been Added

Hi guys,

As the title suggests....is there a way of preventing a product from category "Blue Widgets" being added to the basket if a product from category "Red Widgets" is already in the basket?

Thanks!

Chris
Comments
  • fc_jedfc_jed FoxyCart Team
    @JoeBloggs

    Very interesting requirement. May I ask what version of FoxyCart are you currently running? It would be awesome if you're running v2.0, currently testing the solution to your issue, as I type this reply.
  • fc_jedfc_jed FoxyCart Team
    @joeBloggs

    This code is for v 2.0:

    <script>
    FC.client.on('cart-submit', function(params, next){
    var red_widgets_exist = false;
    if(!jQuery.isEmptyObject(FC.json.items)) {
    jQuery.each(FC.json.items, function(i, item){
    if(item.category == "Red Widgets") {
    red_widgets_exist = true;
    }
    });
    }
    if(params.data.category == "Blue+Widgets" && red_widgets_exist) {
    next(false);
    }
    else {
    next(true);
    }
    });
    </script>
    Place this as a custom footer in your store admin's Template Configuration

    It works by restricting adding Blue Widgets if there are Red Widgets in the cart. However, if Blue Widgets are added first, it won't remove it from the cart if Red Widgets are added.

    The important thing to do it to test the code first. Feel free to write customization for removing Blue Widgets when Red Widgets are added. The snippet is just meant to get you off to a running start.
  • Hi @fc_jed

    Thanks for the code...I'm using v2.0
    Will this code work if using the full page checkout instead of the sidebar? I can't seem to get the .on('cart-submit' event to work.

    Thanks,

    Chris
  • fc_jedfc_jed FoxyCart Team
    @JoeBloggs

    The cart-submit event triggers whenever something is being added to the cart. I'm not for sure if it would work on the full-page cart but when I tested it, it prevents the product from being added to the cart - so it doesn't invoke the sidecart at all. Do you use the fullpage cart by default?
  • fileeefileee Member
    edited July 2016
    Hey there, i need to pull this out from the archive again, because we have the same problem currently.

    We have different subscriptions(premium, professionell) and only want to allow the user to add one of them into the cart. Quantity_max is fine for restricting the individual item, but in order to make sure we always only have one type of subscription in the basket, we need the script above (and remove the existing product when finding one).

    However it does not work. We have a fullscreen config, without a sidebar and i tried to include the script in the custom footer. It is not executed there when a new product is added. Actually it seems that custom logic is always only executed AFTER the page is loaded.

    When i include the script on our main page it is executed, however, the FC.json.items array is not updated and does not reflect the actual state. It only is correct when i reload the page. So the user can in theory just add both subscription after one another. Actually even that does not work as it seems the next(true) call is not triggering the add event when you have a fullscreen shop(no new tab is opened, nothing happens)

    Any ideas? I will now try it with the sidebar config, but it would be really great if we can make it work without it.
  • fileeefileee Member
    edited July 2016
    btw: this is the script i'm using:

    (fullpage config):

    FC.client.on('cart-submit', function(params, next) {
    if(params.data.category == "subscription" && !jQuery.isEmptyObject(FC.json.items)) {
    jQuery.each(FC.json.items, function(i, item){
    if(item.category == "subscription") {
    FC.client.request('https://'+FC.settings.storedomain+'/cart?cart=update&quantity=0&id='+item.id+'').done(function(dataJSON) {
    FC.json = dataJSON;
    next(); <-- not working at all
    });
    return false;
    }
    });
    }else{
    next();
    FC.client.updateCart(); <--Updating too early, should happen after next() is done

    }
    });
  • fileeefileee Member
    edited July 2016
    i already tried to update the current json by setting FC.json = dataJSON or calling updateJSON() or updateCart(). It seems next() alone is not triggering to update the json. However if i put updateCart() after next(), it is triggered to early. Is there a next().done() callback?

    If i put the not working 1. next() OUTSIDE after the FC.client.request() it works ;) Not inside....
  • fc_adamfc_adam FoxyCart Team
    @fileee,

    Thanks for providing those details and your updates for what you've tried.

    The cart-submit event won't be called on the full page cart, because at the point the full page cart has loaded the product has already been added. You'd would instead want to run it on your own website so that it processes as add to carts are actioned.

    You could run something similar on ready.done on the full page cart - but the logic would need to switch a little. First checking if there are more than one subscription and then if there is deciding which to delete and which to keep.

    For the code you pasted - could you try removing the return false; line? I believe that may be prematurely ending the event queue which is meaning when it tries to call next(); there is no event queue to run. You shouldn't need to call updateCart or anything like that - the normal add to cart functionality should handle it.

  • fileeefileee Member
    hey Adam,

    Thanks for your response. I didn't yet tried it to include the script only on the fullpage. But for the last part of your message i tried to remove the return false; statement and it still does not work.

    He is definitely not updating the cart, and that is the problem for both cases. If i use a setTimeout with the updateCart() call, then both use cases work (adding something after deleting the old subscription AND adding a fresh one for an initial cart).
  • fileeefileee Member
    edited July 2016
    just to be clear, this is working now but of course an ugly hack:



    FC.client.on('cart-submit', function(params, next) {
    if (params.data.category == "subscription" && !jQuery.isEmptyObject(FC.json.items)) {
    jQuery.each(FC.json.items, function(i, item) {
    if (item.category == "subscription") {
    FC.client.request('https://' + FC.settings.storedomain + '/cart?cart=update&quantity=0&id=' + item.id + '').done(function(dataJSON) {
    });
    next();
    setTimeout(function() {
    FC.client.updateCart();
    }, 2000);
    return false;
    }
    });
    } else {
    next();
    setTimeout(function() {
    FC.client.updateCart();
    }, 2000);
    }
    });
    };
  • fc_adamfc_adam FoxyCart Team
    @fileee,

    One thing I did just realise, is that if the product is a subscription, but there isn't one currently there - there isn't a next call. And while you should only ever have 1 product to remove - if there happened to be multiple, you'd have multiple remove calls which would make the add take even longer. With that in mind, give this a try:
    FC.client.on('cart-submit', function(params, next) {
    if (params.data.category == "subscription" && !jQuery.isEmptyObject(FC.json.items)) {
    var subs = [];
    jQuery.each(FC.json.items, function(i, item) {
    if (item.category == "subscription") {
    subs.push(item.id);
    }
    });
    if (subs.length > 0) {
    var remove_string = "&";
    for (var i = 0; i < subs.length; i++) {
    remove_string += (i+1) + ":quantity=0&" + (i+1) + ":id=" + subs[i];
    }
    FC.client.request('https://' + FC.settings.storedomain + '/cart?cart=update' + remove_string).done(function(dataJSON) {
    next();
    });
    } else {
    next();
    }
    } else {
    next();
    }
    });
Sign In or Register to comment.