accessing FC object for event listening

SammyJSammyJ Member
in Help edited March 2015
Hi all, first post here, so apologies if this has already been answered on another thread. Feel free to just link if that's the case.

I'm attempting to listen for the cart-submit event, but I can't seem to access the FC object globally. I'm loading Foxycart in the <head>:

<script src="//cdn.foxycart.com/davidphelps/loader.js" async defer></script>

then in the footer calling my main.js file, which tells me FC isn't defined:

FC.client.on('cart-submit', function(params, next){

});


Seems like it must have to do with the async attribute b/c once the page loads, FC is defined in the console
Tagged:
Comments
  • brettbrett FoxyCart Team
    https://wiki.foxycart.com/v/2.0/javascript#accessing_the_fc_object_on_your_own_website

    That's probably what you're after, but if not, let us know :)
  • Hmm, closer. Now FC is available, but the cart-submit event is still not firing. Or at least the listener isn't seeing it, even though items are being added to the cart (and all the correct data is being sent). I tried the cart-submit and cart-submit.done events...
  • Disgusting code, but here's what I've found:


    var checkFC = setInterval(function() {
    if(typeof FC === 'object'){
    $(document).trigger('FCLoaded');
    FC.onLoad = function () {
    FC.client.on('ready.done', function () {
    FC.client.on('cart-submit.done', function() {
    console.log('added');
    });
    });
    };

    } else {
    console.log('FC not Loaded');
    }
    }, 60);


    All of the onLoad and readys and timeouts are just to isolate the steps of what is loading when. This is working a little bit, but cart-submit doesn't fire until I activate the sidecart. The element that has data-fc-id="minicart-quantity" is updating properly, but cart-submit doesn't happen at the same time.
  • brettbrett FoxyCart Team
    @SammyJ, I might be misunderstanding what you're after, but this should work:

    var FC = FC || {};
    FC.onLoad = function () {
    console.log('onLoad');
    FC.client.on('ready.done', function () {
    FC.client.request('https://'+FC.settings.storedomain+'/cart?h:foo=bar');
    console.log('ready.done');
    });
    FC.client.on('cart-submit.done', function() {
    console.log('added');
    });
    };

    Maybe the confusion is on the cart-submit and what it's doing? What's your use case. Maybe that'd help.
  • This is not working for me in Angular5 can you please help in this regard? @brett
  • fc_adamfc_adam FoxyCart Team
    @muasif80,

    I'm sorry to hear the code isn't working for you. I just replied on your other thread, if it's ok, let's continue the discussion there as it sounds like it might be the same issue to look into
Sign In or Register to comment.