−Table of Contents
- type:
- snippet
- category:
- Add to cart form
- name:
- Automatically adding a coupon code
- versions:
- 0.6.0, 0.7.2, 1.0, 1.1
- reference:
- https://forum.foxycart.com/discussion/1993
- date:
- 2011-05-25
Automatically adding a coupon code
The code below is for automatically adding a coupon code in the background, on pageload.
v0.7.2+
fcc.events.cart.ready.add(function(){ // check for coupon $.getJSON('https://' + storedomain + '/cart?output=json' + fcc.session_get() +'&cart=view&callback=?', function(data){ if (!data.coupons) { $.getJSON('https://' + storedomain + '/cart?cart=update&output=json' + fcc.session_get() +'&coupon=free-special-product-shipping&callback=?', function(data){ // console.log('Coupon added.'); FC.json = data; }); } }); return true; });
v0.6.0
function fc_BuildFoxyCart() { // check for coupon $.getJSON('https://example.foxycart.com/cart?output=json&cart=view&callback=?' + fc_AddSession(), function(data){ if (!data.coupons) { $.getJSON('https://example.foxycart.com/cart?cart=update&output=json&coupon=free-special-product-shipping&callback=?' + fc_AddSession(), function(data){ fc_json = data; }); } }); return true; }
For further details, please see the forum link below.