SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Michigan
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to optimize script

    I'm building a web application right now that uses google checkout. As part of this, I need to calculate the shipping cost. This requires asking the fedex server how much it costs to ship the item. On average it takes a minimum of 1 second x 4 methods = 4 seconds to calculate these costs. This is a long time to leave the user sitting there before loading a page. Does anyone know of any reliable methods I could use to say delegate this job to another script so that this event can be triggered and the data stored so that it can be quickly retrieved without the user ever seeing these 4 seconds?

  2. #2
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bjk2007 View Post
    I'm building a web application right now that uses google checkout. As part of this, I need to calculate the shipping cost. This requires asking the fedex server how much it costs to ship the item. On average it takes a minimum of 1 second x 4 methods = 4 seconds to calculate these costs. This is a long time to leave the user sitting there before loading a page. Does anyone know of any reliable methods I could use to say delegate this job to another script so that this event can be triggered and the data stored so that it can be quickly retrieved without the user ever seeing these 4 seconds?
    What about making the requests in the background while they are viewing the cart, or entering their information?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Javascript comes to mind (w/XHR / Ajax). I'm not sure of the structure of your pages but you could fire an XHR request to a PHP script (on your server) as soon as the required data exists on the cart page (presumably weight, source and destination postcode).

    That responder script can then log the order number, shipping cost and enough other data to identify the order contents to your database.
    When you process the checkout consult your local database for the shipping cost, if it exists use it, otherwise fall back to asking fedex at checkout time. (users without JS will not have this data logged)

    This way a separate script can retrieve the shipping cost in the background before the user submits the form.
    You'll just need to pay close attention to expiring/removing cached shipping costs when the cart contents or shipping destination changes.

  4. #4
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Michigan
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore
    Javascript comes to mind (w/XHR / Ajax).
    AJAX isn't an option. I can't rely on javascript to initiate the processing because the user might have javascript disabled or may intentionally block the script from running to prevent the proper shipping cost from being calculated.

    Quote Originally Posted by triexa
    What about making the requests in the background while they are viewing the cart, or entering their information?
    Right, that would be what I'm trying to do, but I'd rather not make it look like the page is still loading, which would be the effect doing that in the same script would make. I would need to delegate the job to a subordinate script or something and then terminate the parent so that the job would be complete, but I'm not sure how I could do that.

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AJAX isn't an option. I can't rely on javascript to initiate the processing because the user might have javascript disabled or may intentionally block the script from running to prevent the proper shipping cost from being calculated.
    When you process the checkout consult your local database for the shipping cost, if it exists use it, otherwise fall back to asking fedex at checkout time. (users without JS will not have this data logged)
    I suggested falling back to your current method where no shipping data is found in the DB. This means users with JS disabled would just get the functionality that you have now. They'd have to wait, but the shipping would still be included.

    For the 85-90% of users with JS enabled they'd get the benefit of pre-fetched shipping costs.
    Disabling JS would not result in free shipping.

  6. #6
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Michigan
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess that would work. That still results in about 5 or 6 seconds before the user gets passed to the google server if they have Javascript disabled since it already takes about a second or two to get the redirect url from google. The ideal situation would be not to force those 10% of users to wait that long, but I guess they may have to live with it.

    If anyone else has a good answer I'd appreciate it.

  7. #7
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Without having a look at the fedex site, is there any charts that you can get with all the postage rates on, which you could use to compile a script which calculates the postage and every so often (say 30 days) checks the fedex site for any change in postage rates.

  8. #8
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Michigan
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That was one of my original solutions, but then I realized there were way too many variables to make this practical, including weight, number of parcels, zip code of receipient, etc. Plus, this could start to get impractical if the list of zip codes to maintain data on gets too large.

  9. #9
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,190
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    How many of your users do you believe will put any major notice on that they are delayed a few seconds?

    Most probably there is more importent issues than this that you could spend time improving. I would do as Mike says, prefetch if the user has javascript enabled, if not let them wait; Most people will not put any notice on such a slight delay anyway.

  10. #10
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I second the suggestion put forth already, i.e. prefetch the shipping costs for user who have JavaScript and make the others wait - a few seconds isn't that big a deal from a user's perspective.

    Another option would be to forego querying FedEx altogether and instead present a shipping rate that you know will cover your costs; call the rest the "handling" part of "shipping & handling". I know for a fact that many websites do this - I order an item, pay the shipping costs, and when I get the parcel if it's one that has postage printed on it then every single time I've paid them more to ship it to me than they paid to have it shipped. If waiting a few seconds is intolerable to you, then I do believe this would be your only other option.

    Although a third potential idea would be to keep a list of all the zip codes that have ever had orders shipped from your site and cache FedEx's shipping info for those zip codes; when a user from a new zip code arrives, they'll have to wait while you fetch the shipping info, but future users from that zip code will benefit from the cached information. Refresh this information every 30 days or whatever, and you've eliminated the wait for the vast majority of your users, including all repeat customers.
    PHP questions? RTFM
    MySQL questions? RTFM


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •