I have an external database (not a Wordpress one) where my client's stock is managed and I need to check the product availability considering this database, not the wordpress' one. My inicial idea is to find where the product is parsed from the database and make an external query to the other database, filling whatever field is to be filled with the product quantity on stock and theoretically all the other validations would work as if this value was the one set on the wordpress database. Is this the best approach to the problem? If so, is there a hook/filter where I can attach my code? Which one? Thank you all in advance
Your approach is impractical in any site with decent traffic. In that case having real time updates from a DB is the best way to bring your site down. What you need is to check the availability when people at items to carts and then again before the actual checkout.
In addition you can update the current number of items every hour (or faster if you really want to try to be as accurate as possible) with a wordpress cron task, but this number is just to give the user a feeling if he should hurry and buy or he has time to think about it. Just remember that unless you are going to keep some ajax going to update the number, the user will very likely see a stale information as the kids called him (or any other distraction) and he returned only after half an hour later to look at the product.
If i correctly understand your question, then i would do it these way:
- create api on website ( or create website with api ) wich uses external database, wich you need to take data from
wp_schedule_eventcreate function wich will fire each hour or day or week to pull data from your api ( external db ). These part i didn't understood, so if you need, for ex., to pull data on post save, then use action
save_post, also u can make your own dashboard page ( even form ) wich, on submit or button press, will pull data from api.
- Use recived data to change post meta or options or what ever you need
Also, you might skip 1 step and do the connection to external database and pull data from there, but i reccomend api.