one item max per cart

I need to restrict the number of items in the cart to one unique SKU with quantity of one "1".

I have been using the following code to reset/delete the cart every time an item is added;

var FC = FC || {};
FC.onLoad = function () {
FC.session.reset();
FC.client.on("sidecart-show", function (params) {
FC.session.reset();
FC.client.request('https://xxxxxxxx.foxycart.com/cart').done(function(dataJSON) {
console.log( dataJSON.items );
});
});
};

This works until the user clicks Proceed to checkout then the cart shows empty. Does anyone know a better way or event handler? I am new to foxycart and just getting to know the options available. Thank you.
Comments
  • fc_marijafc_marija FoxyCart Team
    Hello @kingkong -

    If I'm understanding your needs correctly, you can pass the empty parameter along with your add-to-cart. From our docs:
    empty

    Description:
    This will clear the contents of the cart. If products are added in the same request, the cart will be emptied and then the products will be added.

    Accepts:
    true: Empties the cart, including all custom session values, coupons, customer address and prepopulation values, shipping selections, etc.
    reset: Does all of the above but also resets the cookie. The use case for this is uncommon, so in general you should use the true instead of the reset value.
    For an add-to-cart link, it'd look something like this: https://xxxxxxxx.foxycart.com/cart?name=product&price=10&empty=true

    For an add-to-cart form, you'd add this to your form element:
    <input type="hidden" name="empty" value="true" />
    Hope that helps.
  • kingkongkingkong Member
    edited February 7
    Yes, that did work. To go straight to the checkout I added:

    input type="hidden" name="cart" value="checkout"
Sign In or Register to comment.