derekjwhitten Posted May 12, 2020 Share Posted May 12, 2020 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. Link to comment Share on other sites More sharing options...
Teemu Mäntynen Posted May 12, 2020 Share Posted May 12, 2020 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. 1 Link to comment Share on other sites More sharing options...
derekjwhitten Posted May 12, 2020 Author Share Posted May 12, 2020 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. Link to comment Share on other sites More sharing options...
Teemu Mäntynen Posted May 13, 2020 Share Posted May 13, 2020 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 ). 2 Link to comment Share on other sites More sharing options...
derekjwhitten Posted May 13, 2020 Author Share Posted May 13, 2020 That makes sense regarding the fingerprint, thanks. Is there a generic table I can use to save this data or should my module create a new table and then use the DB class to insert and update? 1 Link to comment Share on other sites More sharing options...
Teemu Mäntynen Posted May 13, 2020 Share Posted May 13, 2020 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. 2 Link to comment Share on other sites More sharing options...
derekjwhitten Posted May 17, 2020 Author Share Posted May 17, 2020 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! Link to comment Share on other sites More sharing options...
Teemu Mäntynen Posted May 17, 2020 Share Posted May 17, 2020 Thank you. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now