Discount based on quantity

in Help edited October 2009
I realize this has been talked about before, but no reasonable solution has been posted as of yet. I'm looking at config options under category settings as well as in coupon code options.

The goal is to have orders containing a QTY of 2 or more products discount by a flat rate, mitigating the flat rate shipping costs. Shipping is set to $5 flat rate, and we intend to discount $5 for orders with two or more products.

The feature in categories seems to work, but it's doing it to each product. Each item gets $5 off. We're trying to just trim $5 off the total cost of the order.

Any help? I'm under deadline and totally thought you guys supported this. My bad :)
Comments
  • brettbrett FoxyCart Team
    Hmm... that's a good question. The discounting we have is really "quantity" discounting, which applies to all the products under the discount. We're realizing (lately) that it's often beneficial to display the discount separately, so we'll keep that in mind.

    That said, I think you could do this with an incremental quantity discount... like this:
    incremental|2-5|3-0
    
    Awesome, I just tested that and it totally works. It won't actually show the discount as a -$5 discount, but rather as a $2.50 discount per product. But the math works out.

    Does that help?
  • I'm in a tricky situation and have the client on the phone about this right now.

    We need to consider instead then doing a coupon where they enter it and get X off their *TOTAL* order based on a total order sum. The problem I see is that ANY coupon discounts are added to the list PER PRODUCT.

    Am I missing something here or is there really no way at all to knock a flat rate off the entire order in any method??

    I praise you if you can respond to this now and are somehow still online ;-)
  • brettbrett FoxyCart Team
    Give this a vote here:
    http://requests.foxycart.com/pages/4162-general-requests/suggestions/359511-coupons-discounts-as-separate-line-items

    To answer your question, another option would be to set a coupon code (you can automatically add a coupon code to your order on your normal add-to-cart actions, fyi) that's a discount of an amount based on the total price. Enter a price threshold that's equaly to (or slightly less than) what two products would cost, but more than 1 product would cost. So if your products are $10 each, set the discount to 20-5, and you'll have a $5 discount if the order total is $20 or more.

    If your products are radically different prices you may have another problem, so let me know if you'll face that issue.
  • Brett -

    I saw your other post asking me to update this one. Sorta...it's darn close and technically calculates to the correct amount.

    It would be *nicer* if we didn't have it add a discount to each item totalling the correct discount, but rather instead having it apply a single line discount to the overall total of the order as a separate, single line item near where shipping is shown.

    Does this make sense? I don't want to show the same discount over every single item. It's not as intuitive as the other option.

    For now, though, at least we have the discount factoring into the order.
  • brettbrett FoxyCart Team
    Just to confirm, did you read this section of my previous comment:
    another option would be to set a coupon code (you can automatically add a coupon code to your order on your normal add-to-cart actions, fyi) that's a discount of an amount based on the total price. Enter a price threshold that's equaly to (or slightly less than) what two products would cost, but more than 1 product would cost. So if your products are $10 each, set the discount to 20-5, and you'll have a $5 discount if the order total is $20 or more.

    You can add a coupon code to the transaction when you do the add-to-cart, so the user wouldn't ever know they were getting a coupon. You'd just get one single discount as a line item in the cart.

    If that's not what you're looking for please let me know, but it seems like you may have missed that from my last post?
  • I have a habit of somehow ignoring things I should be paying attention to...whoops!

    That may be cool. What's the method of using Javascript to add the coupon on the fly, while at the same time detecting how many total items are in the cart at any given time?

    Thanks for your help. This customization of FoxyCart is new to me...haven't done it on this level before.
  • brettbrett FoxyCart Team
    Well, depending on how complex your site is, you may not need to bother with adding the coupon silently in the background. You can just add a coupon parameter to your add-to-cart link or form and call it a day:
    http://wiki.foxycart.com/getting_started:adding_links_and_forms#standard_attributes

    If that won't work for you, the answer would be to do a JSONP request:
    http://wiki.foxycart.com/docs:jsonp
    http://wiki.foxycart.com/docs:json
    That's slightly more complicated if you don't know javascript well, but jQuery makes it fairly easy. Check out the jQuery JSONP documentation and play around with it.

    Getting the number of items total can be done rather easily by just checking the JSON object that's loaded. If you need help, let us know.
  • I'm working with the coupon code, and I think it'll work - I'm just working on the JSON part. First things first:

    1) I have a category, let's say it is called "special-product".

    2) There is one product in this category with the code 'cse'.

    3) I created a $5 (price of shipping) coupon called 'free-special-product-shipping' and limited it to only the special-product category.

    4) If the value spent = 3 of the special products ($105), then I want to add the coupon. I want to check the cart and if there are three items for that cse product, add the coupon code.

    I'm using something like this:
    function fc_PreProcess(submitteddata, id) {
    
        $.getJSON('http://example.foxycart.com/cart?output=json&cart=view&callback=?' + fc_AddSession(), 		function(data){
    		$.each(data.products, function (i, product){
                            // check if product is cse.  if it is add the coupon.
    			if (product.code == 'cse' && product.quantity >= 3) {
                                    $.getJSON('http://example.foxycart.com/cart?cart=update&coupon=free-special-product-shipping&callback=?' + fc_AddSession(), function());
                            }
    		})
    	});
       
    	return true;
    }
    

    I haven't really tested this yet, but I am thinking it will work.

    My problem is that if I have 2 cse products in the cart, and am adding the third - the above will return false when I check to see if the quantity is 3. I know I have the submitted form data in the 'submitteddata' variable - is there an easy way to parse/unserialize that to see if

    a) one of the items being added is a cse
    b) the quantity

    So basically I need a way to parse/unserialize the submitted data variable, and it would be helpful to know if the second getJSON line I have will idneed add the coupon to the cart.

    Thanks
  • brettbrett FoxyCart Team
    I think an important thing to realize is that a coupon can be in your cart but not active... so you can just add the coupon whenever you want, but unless coupon's criteria is met it won't apply. So you could just add it whenever, and have the coupon say either 105-5 (discount by amount based on price) or 3-5 (discount by amount based on quantity). It won't actually take the price off unless the criteria is met.

    Does that help?
  • gougeawaygougeaway Member
    edited October 2009
    I tried that originally, but it would pop up an error if the coupon wasn't valid yet. I'll try it again after clearing my cookies/cache, though, and see if that had something to do with it.

    Just checked, and when I add it, it's fine, but when I add the third item I get "Coupon Invalid: This coupon has already been added to this order."

    So I'd like to avoid that, if at all possible.

    Probably not the end of the world if it is there, but if I can use the javascript from above, that would be a bit cleaner. I'd just have to add something in to check for the existence of the coupon I guess, and not add it if it already exists (which might be easier than the method I currently am using).
  • brettbrett FoxyCart Team
    Ah, right. I'm sorry about that. I was actually testing something else and saw that error, and though, "Oh, shoot. Can't do it like that."

    So, back to the JSONP approach, what I'd do is on the JSON load (fc_BuildFoxyCart() is fired when it's ready), check the JSON for the existence of the coupon code, and if it's not there, add it via a JSONP request.

    That way you don't have to check for products in the cart or anything. Just add the coupon if it's not there. Else, don't do anything. I think that should do it for you. Let me know if that has problems as well.
  • gougeawaygougeaway Member
    edited October 2009
    Thanks Brett,

    That's what I ended up doing. Once it occurred to me that I could just use JSON to check for the coupon it was a lot easier. This seems to be working. Here's the code, in case someone else has the same issue:
    function fc_BuildFoxyCart() {
    	// check for coupon
        $.getJSON('http://example.foxycart.com/cart?output=json&cart=view&callback=?' + fc_AddSession(), 			
    	function(data){
            if (!data.coupons) {
              	 $.getJSON('http://example.foxycart.com/cart?cart=update&coupon=free-special-product-shipping&callback=?' + fc_AddSession(), function(){});
    		}
    
    	});
       
    	return true;
    }
    
  • brettbrett FoxyCart Team
    Thanks gougeaway.
    I've added that to our wiki page that references interesting javascript solutions on the forum:
    http://wiki.foxycart.com/docs:advanced:forum#add_to_cart_form_hacks
  • Great - just for future reference, is there a way to format code in the forum output so that it recognizes tab indents?
  • brettbrett FoxyCart Team
    Unfortunately, not at this point, but once we do upgrade things on the forum the original post information will be formatted properly. The tabs are there, they just aren't output. So... eventually ;)
  • Hello,

    I have been using the above hack to add a coupon silently in the cart. What do you need to change to make it work in v7 with the new javascript calls?

    this is what I have been using successfully in v6:
    function fc_BuildFoxyCart() {
    // check for coupon
    $.getJSON('http://j15.foxycart.com/cart?output=json&cart=view&callback=?' + fc_AddSession(),
    function(data){
    if (!data.coupons) {
    $.getJSON('http://j15.foxycart.com/cart?cart=update&coupon=J1560MORE&callback=?' + fc_AddSession(), function(){});
    }
    

    Thanks for the help, I'm obviously a complete n00b when it comes to javascript.

    Cheers,

    Alex
  • fc_adamfc_adam FoxyCart Team
    @zeligprod, the following should be the same for you, just replace what you pasted above with this:
    fcc.events.cart.postprocess.add(function(){
    // check for coupon
    $.getJSON('http://j15.foxycart.com/cart?output=json&cart=view&callback=?' + fcc.session_get(),
    function(data){
    if (!data.coupons) {
    $.getJSON('http://j15.foxycart.com/cart?cart=update&coupon=J1560MORE&callback=?' + fcc.session_get(), function(){});
    }
    }
    });
    

    Basically a change to the function name, and how the session is added.
  • Great!

    Thank you very much,

    Alex
  • I want to make a coupon automatic on my web site, where do you put this code? what needs to be edited to make it work for my site? www.mxbeltbuckle.com

    function fc_BuildFoxyCart() {
    // check for coupon
    $.getJSON('http://example.foxycart.com/cart?output=json&cart=view&callback=?' + fc_AddSession(),
    function(data){
    if (!data.coupons) {
    $.getJSON('http://example.foxycart.com/cart?cart=update&coupon=free-special-product-shipping&callback=?' + fc_AddSession(), function(){});
    }

    });

    return true;
    }
  • brettbrett FoxyCart Team
    Try something like this:
    function fc_BuildFoxyCart() {
    // check for coupon
    $.getJSON('http://mxbeltbuckle.foxycart.com/cart?output=json&cart=view&callback=?' + fc_AddSession(),
    function(data){
    if (!data.coupons) {
    $.getJSON('http://mxbeltbuckle.foxycart.com/cart?cart=update&coupon=free-special-product-shipping&output=json&callback=?' + fc_AddSession(), function(){});
    }
    
    })
    return true;
    }
    
    

    Replace the "free-special-product-shipping" with your coupon code and put it at the bottom of your site template, or whatever page should be automatically adding the coupon. Note that this will work for the store version you have, but not v0.7.0+.
  • Hey Bret,
    What would be the new code for v0.7.2? I essentially want to have the coupon automatically be added at the checkout page if the amount any products added is greater than 1 (2 or more gets $5 off)
  • nevermind. Just found it in the doc.s
    fcc.events.cart.ready.add(function(){
        // check for coupon
        $.getJSON('http://example.foxycart.com/cart?output=json' + fcc.session_get() +'&cart=view&callback=?', function(data){
            if (!data.coupons) {
                $.getJSON('http://example.foxycart.com/cart?cart=update' + fcc.session_get() +'&coupon=free-special-product-shipping&callback=?', function(){
                    // console.log('Coupon added.');
                });
            }
        });
        return true;
    });
    
  • Although I'm still a tad confused how I would make it check whether the quantity of the cart is greater than 1
  • Also.... although I could easily use a category discount to do this. I do not want to go this route since the user cannot easily see they are receiving a discount. I like how the coupons appear against the total instead of an weird looking calculated discount number on the side of each product.
  • lancelance Member, Community Support Member
    You can access the cart's JSON object in the cart and get product count using:

    fc_json.product_count
Sign In or Register to comment.