Foxy Forum Status

We're no longer responding to questions via our forum, but we will keep it up for historical reasons. If you can't find the answer you're looking for, please visit our knowledge base or contact us. If there's enough interest in the future, we may bring the forum back.

Fetch coupon details by name or code

Hey guys,

If I need to get specific coupon details, it looks like I need to know the coupon ID, which I don't see anywhere aside from the api. *I do see an ID for the coupon that might look like 2860358, but fetching that via the api returns an error. Do I have to get all coupons and then page through looking for the target coupon by name? Or?...

the back story:
Client wants a custom sidecart. I built one using ReactJS, which was a fun exercise. Now they want to handle coupons a bit differently. They use quantity_percentage category discounts 3-15|5-20. But if a coupon will discount by a lesser or equal value than the category discount, they want to reject the coupon since discounts aren't combinable.
So, I figured I'd grab the coupon via the api, check the discount_type, compared the discounted value to the category discount value, and proceed from there. I'm probably thinking about this the wrong way though.

The only other way I could think to do this was:
1. add coupon to cart, but don't update the cart display.
2. compare the coupon Amount to my current discounts total
3. if coupon amount is lesser or equal, remove it from the cart and display a warning/info message of some kind.

That might be simpler in the short term, but I'm expecting to run into unexpected results... or a changing set of requirements.

Thanks for your input, as always.

  • fc_adamfc_adam FoxyCart Team

    Thanks for providing the overview there - sounds like an interesting set up.

    You don't need to know the coupon ID to be able to access it with the API - you can filter by coupon attributes too - so if you know the coupon's name, you can look for it using that. For example, you could append something like ?name=My+Coupon to the coupons URI to find your coupon called "My Coupon". You can also filter by a specific coupon code too, like this: ?zoom=coupon_codes&coupon_codes:code=my_code.

    That said - handling it in javascript might potentially be more straight-forward, along the lines of what you described. You could hook into the "cart-coupon-add.done" event, and perform your checks based on the calculated discount and the existing product/category level discounts that have been applied. Then if it needs to be removed, send off a cart request to do that. There may be a flash of the coupon being added, but that operation will be quicker than having to send off a request to an endpoint on your side to check with the hAPI and then come back with a response.

    As an aside - is your dev site online? I'd love to see your custom cart set up if you don't mind whispering the link!
  • Thanks @fc_adam. One step closer to fully understanding the awesome power behind your API.

    As soon as I push this to dev, I'll whisper the link to you.

  • I also have some weird bug that I'm working on where sometimes the render method won't output the actual cart items that exist in state. Can't figure out what I'm doing wrong...
  • Okay, a little more clarity to the weirdness. When I open the checkout in a new tab, I see that the item was added, but it doesn't appear in FC.json.items after a "successful" call to FC.client.request. When I log the dataJSON in the .done callback, I don't see the new item there either. What could I possibly be doing wrong?
  • fc_adamfc_adam FoxyCart Team

    Thanks for whispering the link to your site - it looks great!

    I was able to replicate what you're describing I think - I clicked add to cart, and your custom sidecart loaded, but it's not showing any products, even though one was successfully added.

    Looking at the network requests on the page - there were two placed at the same time. One was the cart add request, and one was a plain cart request - essentially just returning the cart. I'm guessing that in the instances where the cart doesn't seem to add the product, there has been a specific race condition where the plain cart request processed before the add to cart request did, but it returned after the cart request. This meant that the final state of the cart JSON object was that of the empty cart, rather than the one with the product added.

    If you can get that second plain cart request to not be run, it should get the cart behaving as you expect. Could you confirm where you're handling the javascript for handling the add to cart request and showing your custom cart?
  • @fc_adam, ah! I figured it out. The FC config file was submitting the product form for me. I decided to skip loading the config altogether and simply re-implement the .client.request method on my side since that's the only piece I was really using (and the session). seems to be working pretty well now, though could use a little polish around the edges. Let me know what you think. Thanks for your help!
  • fc_adamfc_adam FoxyCart Team

    I'm glad you were able to get it sorted! For what it's worth though - I would recommend sticking with loading the loader.js and instead hook into the events to make the cart submit action load in your custom cart. We can help with that if you'd like.
Sign In or Register to comment.