Synchronization via Hooks (eBay) by Fabien Serny
This article was written by Fabien Serny, and was fist published on June 22nd, 2011, on the PrestaShop blog.
To learn more about hooks, I advise you to read Julien Breux's article here: Understanding and using hooks
Sometimes we need to set up an automated task (also called "cron job"), especially for certain modules that synchronize their product catalog to a marketplace.
Let's take a look at the eBay module as an example. The simplest solution would have been to set up an automated task regularly resynchronizing its product catalog with the marketplace. However, some problems may arise with this solution:
- If you run the task daily, then inventory synchronization will not be in real time. This can become complicated for merchants who have many customers per day and a low quantity of certain products.
- If you run the task every 30 minutes, it quickly becomes resource intensive, especially if merchants have over 5000 products.
The best solution to solve this problem is to use hooks.
How do I use hooks?
If we study the problem, we realize we can simply synchronize the global catalog when installing the module, and then resynchronize only the products that are likely to have been changed.
In the eBay module, a button exists for the initial synchronization. Subsequent synchronizations occur only when a product is added, modified, or removed, or its quantity decreases following an order.
The hooks that can be used are:
adds a product
Updates the product
Updates a product's attribute
Deletes a product
for a new order
Let's look at, for example, the
addproduct hook, the
$params parameter that contains the
Product object, which will allow us to easily find the product has updated.
hookdeleteproduct methods are all the same, you just need to add a few lines:
With this method, inventories are synchronized in real time, and products that do not need resynchronization are excluded from the process.
Please note: For those interested in the operation of
hooknewOrder in this module, I suggest you go and look directly at the eBay module's code
And what about actions without a hook?
For actions without hooks (eg. importing eBay orders to PrestaShop), it is better to use an automated task. However, this is not always an option (if you're on a shared server for example).
There is still a little trick that simulates an automated task using your visitors. Just hook the module to the front (preferably with a hook called 'appeal to all pages.'), and then simulate an automated task inside.
The only issue is that if you do not have traffic on your site, the task is never executed.
Please note: You must also make sure that the task doesn't take too long; otherwise the client that has triggered the hook will be negatively affected.