Both sides previous revisionPrevious revisionNext revision | Previous revision |
v:2.0:json [2019/09/09 05:38] – [Making a JSONP call to the cart] adam | v:2.0:json [2022/02/04 16:37] (current) – [Adding a Coupon or Session Value Automatically Using JSONP] adam |
---|
<code javascript> | <code javascript> |
FC.client.on('ready.done', function () { | FC.client.on('ready.done', function () { |
| // Automatically add a coupon |
if ($.isEmptyObject(FC.json.coupons)) { | if ($.isEmptyObject(FC.json.coupons)) { |
FC.client.request('https://'+FC.settings.storedomain+'/cart?coupon=YOUR_COUPON_CODE').done(function() { | FC.client.request('https://'+FC.settings.storedomain+'/cart?coupon=YOUR_COUPON_CODE').done(function() { |
}); | }); |
} | } |
| // Add a hidden session variable |
| FC.client.request('https://'+FC.settings.storedomain+'/cart?h:foo=bar'); |
}); | }); |
</code> | </code> |
This function hooks into the ''ready.done'' event to ensure that the FoxyCart JSON object has been loaded, and checks if the coupons object is empty. If it is, it then triggers the request to add a coupon to the cart, and removes any coupon errors in case it's expired or removed. This is just a simple example, but hopefully gives you an idea of how to get started. | This function hooks into the ''ready.done'' event to ensure that the FoxyCart JSON object has been loaded, and checks if the coupons object is empty. If it is, it then triggers the request to add a coupon to the cart, and removes any coupon errors in case it's expired or removed. This is just a simple example, but hopefully gives you an idea of how to get started. |
| |
If you want to run code like that on pageload, you'd need to wrap it in the [[.:javascript#accessing_the_fc_object_on_your_own_website|FC.onLoad() wrapper]], like this: | If you want to run code like that on pageload //of your own website//, you'd need to wrap it in the [[.:javascript#accessing_the_fc_object_on_your_own_website|FC.onLoad() wrapper]], like this: |
<code javascript> | <code javascript> |
var FC = FC || {}; | var FC = FC || {}; |