Jump to content

Multiple calls to getOrderShippingCost on every page


Recommended Posts

I have followed the guide on this page to create a module for a shipping carrier. The module makes a call to the carrier's API to calculate shipping costs.

It's working fine and on checkout I'm getting the calculated value back in the cart, however the code is called 4 times on every page load, not just the checkout pages.

Is there some additional logic I need to include in the module to prevent this?

I have copied the code from the guide as is, the only change being that I've replaced return 777 with the code that calls the API.

Share this post


Link to post
Share on other sites

Yes, the multiple method calls to getOrderShippingCost() are a bit of a surprise at first.

For this exact reason I use caching in my shipping modules to minimize repeating of heavy computational tasks and network requests to remote APIs.

Make your API call once and save its outcome to database. Then between page requests use the db-cached data until anything important changes in the cart (products are added or removed, destination address changes etc.). During a single page request but between the repeated method calls to getOrderShippingCost() you may even wish to use an other level of caching to cache the output of the getOrderShippingCost() method.

Share this post


Link to post
Share on other sites

Thanks Teemu. Would it be enough to check the number of items in the cart to determine if something has changed? I'm assuming the postage module would be called at least every time an item is added or removed or a quantity is updated.

Share this post


Link to post
Share on other sites

It depends on what chances of the cart invalidate your cached shipping data. I typically use cart id, country code of the shipping address and id and quantity of each product of the cart to form a unique "fingerprint". Then if anything in the fingerprint changes, I renew the cached data.

The getOrderShippingCost() method is called every time a product is added or removed from the cart (well, actually the method is called multiple times, again :) ).

  • Like 1

Share this post


Link to post
Share on other sites

I have created an ObjectModel (ie. a separate Db-table) for the data i wish to cache between page requests. To cache just the return value of the getOrderShippingCost() during a single page request but between the repeated method call I have used PrestaShop's core class Cache to handle the caching.

  • Like 1

Share this post


Link to post
Share on other sites

Thank you very much Teemu. I've managed to implement my own version using your thumbprint approach stored in a new table created by my module.

I really appreciate your help here. Thank you!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More