Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 23 posts 79 karma points
    Nov 07, 2011 @ 17:11
    Tom
    0

    Cart Error - Javascript fired from .net events

     

    Firstly I'd like to say how awesome Tea Commerce is!

    We’ve now almost done everything we wanted in terms of functionality with Tea Commerce, but I’m running into a small issue which I wondered if you might be able to help me with..

    I have modified the javascript api method to change the shipping method when it is selected in the UI, and I can fire off events successfully, but we would like to also be able to fire off some javascript code when the shipping method is changed based on some calculations in the code behind – at the moment I’m just testing with an alert and response.write, however this causes a cart error that shows the alert code, plus what appears to be the order details, any ideas what might be causing this?

    Thanks

    Tom

     

  • Rune Grønkjær 1372 posts 3103 karma points
    Nov 07, 2011 @ 18:51
    Rune Grønkjær
    0

    Hi Tom,

    Thanks for the kind words.

    I will probably need to see some of your code to be able to help you. But what you might be able to do is to check the shipping method on every general event that is fired.

    On every page load, write the shipping method in the head of the page like the _pageId and _languageId. That will be your currently selected shipping method and you will know that if it is different to the one you get in the general Tea Commerce event, it will have changed, and then you can do whatever to the UI.

    That's an idea. Don't know if it fits your scenario. If not, I need to know more.

    /Rune

  • Tom 23 posts 79 karma points
    Nov 08, 2011 @ 10:44
    Tom
    0

    Hi Rune,

    That sounds like a valid solution, thanks, and I have also come up with a workaround for our situation (which may or may not be used), but knowing more may help us or someone else? :)

    The jquery I have added to change the shipping method on selection is:

    $("input[name=paymentMethod]").live("click", function () { var paymentMethod = jQuery('input[name="paymentMethod"]:checked').val(); TeaCommerce.setPaymentMethod(paymentMethod, { async: false }); });

    The events code in the dll (stripped down to barebones to test). 

    public void Initialize() { WebshopEvents.PaymentMethodChanged += WebshopEvents_PaymentMethodChanged; }

    void WebshopEvents_PaymentMethodChanged(Order order, PaymentMethod paymentMethod) { HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert(\"hello\");</script>"); }

    Any insight on this would be very much appreciated :)

    Tom

  • Rune Grønkjær 1372 posts 3103 karma points
    Nov 08, 2011 @ 11:13
    Rune Grønkjær
    0

    Hi Tom,

    I think you are complicating things a bit. You don't need any .NET code at all. Try something like this:

    1. In your Main Master you add, which will be the variable you use to keep track of the current shipping method chosen:
    var _shippingId = <umbraco:Item field="pageID" runat="server" xslt="teacommerce:GetOrderXml()/shipping/@id" />;

    2. In the teaCommerce_Advanced.js event "TeaCommerce.subscribeToOnCartUpdated" you write a bit of code to check the order everytime it have changed. If you have changed the shipping id on the server, we will know like this:
    if(data.Order.Shipping.Id != _shippingId){
      //do some stuff
    }

    That should do the trick.

    /Rune

  • Tom 23 posts 79 karma points
    Nov 08, 2011 @ 11:31
    Tom
    0

    Thanks Rune,

    I agree that for that example, it appears overly complex..
    however, the reason I was using .Net was to query a database based on which payment method was selected, and run some javascript/jquery functions based on the result of the query.. a little unconventional..but that's what our requirements are.

    Tom

  • Rune Grønkjær 1372 posts 3103 karma points
    Nov 08, 2011 @ 12:56
    Rune Grønkjær
    0

    That you might still be able to do in the javascript. You have access to both the selected shipping method and payment method. Just make the different scenarios in the javascript. If you need some extra information you can let the .net code add it to the order with an order property and use that in the javascript as well.

    /Rune

  • Tom 23 posts 79 karma points
    Nov 08, 2011 @ 14:48
    Tom
    0

    Agreed that is also another way of doing it, though I have for now, implemented the solution I came up with, which is based on razor and javascript, but would have liked to be able to do it all from the .Net code if possible. 

    Any ideas on why it would throw the error in the first place?

    Thanks again for your time!

    Tom

  • Rune Grønkjær 1372 posts 3103 karma points
    Nov 08, 2011 @ 14:55
    Rune Grønkjær
    0

    When the cart error comes up the server have somehow thrown an error. You can use firebugs console to see what request are made to the server and what response you recieve. In the response you will be able to see what kind of server error is thrown. OR you can see if no errors are thrown.

    Try that.

    /Rune

  • Tom 23 posts 79 karma points
    Nov 08, 2011 @ 15:07
    Tom
    0

    I get a 200 OK status from the server, and the response contains the script code that I posted earlier, plus the order details (as I pointed out before).

    Any help?

    Thanks,

    Tom

  • Rune Grønkjær 1372 posts 3103 karma points
    Nov 08, 2011 @ 15:09
    Rune Grønkjær
    0

    Then it might be because jQuery tries to parse your script as json. That would make it fail as well

    /Rune

  • Tom 23 posts 79 karma points
    Nov 08, 2011 @ 15:21
    Tom
    0

    Ah ok, any way I can avoid that happening?

    Tom

  • Rune Grønkjær 1372 posts 3103 karma points
    Nov 08, 2011 @ 16:01
    Rune Grønkjær
    0

    Yes, and now that I look more closely at your code I realize that you are writing to the response in one of our events. That would destroy any response that Tea Commerce returns. In the Tea Commerce events all you can do is manipulate the order. Therefore you must put your javascript in an order property as a string. It's really bad practice though. The server should not write javascript code. It should provide the data for the javascript and let the client side take care of the javascript.

    Bottom line is, you cannot do this:

    $("input[name=paymentMethod]").live("click", function () { var paymentMethod = jQuery('input[name="paymentMethod"]:checked').val(); TeaCommerce.setPaymentMethod(paymentMethod, { async: false }); });

    /Rune

  • Tom 23 posts 79 karma points
    Nov 08, 2011 @ 16:19
    Tom
    0

    Thanks Rune.

    I appreciate that it is bad practice, but it was a slightly unusual situation, which needed an unusual answer :)

    For now I will be sticking with the razor alternative.

    Thanks for your help!

    Tom

  • Rune Grønkjær 1372 posts 3103 karma points
    Nov 09, 2011 @ 12:56
    Rune Grønkjær
    0

    Hi Tom,

    That's cool. I hope you'll get it working.

    Please mark your own answer as the answer, or which one is most correct, for others to see when they come past this question.

    /Rune

Please Sign in or register to post replies

Write your reply to:

Draft