Don't allow user to add items to cart based on items already in the cart


I have been asked to not allow users to add an item to the cart if another item is already in the cart. We are selling courses and sometimes the user will add courses that have schedule conflicts with another that they already added. Please let me know how I can complete this.
  • fc_adamfc_adam FoxyCart Team

    Sure thing! If you only ever want to allow a single item in the cart, a simple way you can achieve that is to include the empty parameter with your add to cart links/forms - which will empty the existing cart before adding the new product(s).

    In a link, you would append &empty=true and in a form you'd include an additional hidden input like this:
    <input type="hidden" name="empty" value="true" />

    I'm not sure how many products you have, or how they're managed on the store - but could that approach be easily implemented? There is also a javascript approach you could take if it wouldn't be easy to update all of the store products quickly to include the extra parameter - but if you're able to, including it would be the recommended approach.
  • I only need to do this for items that have conflicting schedules. So, I need to get the current items in the cart and check when the user adds a new item to the cart for conflicts.
  • fc_adamfc_adam FoxyCart Team
    edited January 12

    Thanks for clarifying - sorry for misunderstanding initially. That is possible too, with a little javascript added to your website. You'll hook into the cart-submit event to check the items already in the cart and compare it to the product being added.

    Do you have an example we could see of two products that would be considered conflicting if one was already in the cart and the other was attempted to be added? We can then give you an idea of what the javascript would look like to help get you started.
  • I would like to check the items by item code.

    If the code EP03301ST exists in the cart and the item.code EP03201ST is added, I would like to hide the "Add to Cart" btn and add a message.

    I have 5 items that I need to check if they are in the cart. There are two groups of items that only one item from each group can be added.


  • fc_adamfc_adam FoxyCart Team

    Thanks for confirming your requirements there. Something like the following should achieve what you're after, you'll include this with your own website's javascript:
    FC.client.on("cart-submit", function(params) {
    var groups = [
    ["EP03301ST", "EP03201ST" , "EP04501ST"],
    ["EP02301ST", "EP01901ST"]
    var check_for = [];
    var new_code = params.url.match(/code=(\w+)/);
    if (new_code) {
    for (var i = 0; i < groups.length; i++) {
    if (groups[i].indexOf(new_code[1]) > -1) {
    check_for = groups[i];

    if (check_for.length > 0) {
    for (var i = 0; i < FC.json.items.length; i++) {
    if (check_for.indexOf(FC.json.items[i].code) > -1) {
    // There is already a product from this group present in the cart
    // Consider showing some sort of feedback to the customer here
    return false;
    You can add additional groups of product codes in the groups array, and you'll probably want to give some sort of visual feedback to the customer, like with an alert() before the script does the return false;.
Sign In or Register to comment.