Accessing external existing databases
Is it possible to use WooCommerce to access multiple existing external databases rather than storing data in a WooCommerce database for querying and inserting data?
Also, can WooCommerce link to an existing external payment system?
If so, is there any relevant documentation on how to achieve these?
I think it is possible to use an external database for storing the data for close to everything, but it really depends on your use case. If you want to do it for products or orders specifically, there are plenty of hooks available to take out the data and do whatever you want with it.
There is no real documentation about this part, since it is such a wide area of things you can do. If you have specific questions, I would be happy to answer them.
About the external payment system: We have a Payment Gateway API, which enables you to use an external payment system. In fact, a couple of our payment gateways already do this, so we know it works pretty well.
I’ve been looking into using an external database for storing products.
But i was unable to find the right hooks for this.
For example, woocommerce uses the WP loop to show a listing of products.
But you can only hook pre-query, whereas i want to hook post-query, where i want to collect my products from an external database and offer them back as wordpress posts/products.
Do you have any examples on how to hook products/categories ?
Pre query hooks allow you to change the query before it has been executed which is the preferred method. This way you will not do a query where you’ll not doing anything with the data it collects, only to fire a new query after that to get the data you need.
This is no different from how products work, since they all are queried like posts.
But i can’t pass arguments to the query that would be used to query another external database right?
How would you see that work?
Sorry, I have no idea what the exact problem you are having is. The moment where you filter has nothing to do with parameters. Maybe the hooks you’ve chosen do not match the parameters that you need, but that is another discussion.
Please provide examples of issues where you run into. Be a bit more specific.
Since i’ve already hijacked the thread i’ll elaborate on what i’m trying to achieve:
What i have:
– basic WordPress install with woocommerce installed on a local database
– External database with products/categories/productinfo. for ease, let’s say it’s just one table where each product is a row.
What i want:
– To show the categories/products i have in the external database to show up in my woocommerce site.
For woocommerce they should be external products, with just a title, image, link to another site.
How i thought to achieve this:
For example on a category page, woocommerce loops through the posts(products) to show them. I want to keep using the wp_query for the loop and th eloop in general, keeping the woocommerce templates intact.
So i thought hooking the wp_query, and instead of getting intrenal produts, i have some of my own code making a connecting to the other database, getting the produts, and converting them to objects wordpress/woocommerce can use in the loop.
Hope you can give me some pointers, much appreciated,
This is a bit complicated, because it involves a non-WordPress database where you want to query your products from. You basically have two options here:
1) Find a way to make products out of your external database products, on the fly, so you can query them at once.
2) Keep your WooCommerce products and the products from your external database in sync, for example hourly via a cron. Or make a trigger/sync script for when something changes in the external database, so it updates the WooCommerce products.
Short term, the first solution might be the quickest to pull off, but the second will give you a more robust solution. The second is also a cleaner way to do this, in my opinion and it will leave most of the WooCommerce template files and queries as they are. It’s just a complete different approach to this all.
I think it all depends on how complicated your external database is, but I think the second way is the one you should work towards.
But remember, both solutions are really complicated pieces of code. I don’t know how experienced you are, but it will take quite a bit of custom coding…
Thanks for the quick reply,
Unfortunately scenario 2 is a probably a no-go for us, since the number of products is quite large (around 600.000-800.000). I haven’t tested the performance, but with the way WP queries it will probably give headaches.
That’s why i want to query/select as specific as i can and go for scenario one. I know i can code it, although it would take a lot of time like you said. But the issue is that i don’t want to core-hack WP or woocommerce. So i was trying to find hooks where i could place my code without affecting the internals as much as i could.
The initial simple thought was: Hook the “get categories” , hook “get posts/products” functions. get the products and return them as objects, and won’t let WP/Woocommerce know..
I don’t think there is a problem in running that amount of products through a WordPress/WooCommerce install. I’ve seen websites powered by WooCommerce having a total of over 100.000 products, so I see no reason why you would run into much more issues then they have. It’s all about scaling your code and I believe WooCommerce is doing that very well. If you have any direct concerns, we can look at that.
For the hooks, you would have to look at
pre_get_postsand other similar hooks, but I’m afraid you’ll run into various other problems when taking that route. The more I think about the first proposal I did, the more problems I see there.
Next to that, if you’re worried about performance in the first idea I gave you, I would also start to worry about the performance issues in the first way to do it. Accessing an extra database and forming objects from that database based on external data, all doing it on the fly. You’ll have to find really good ways to cache that and eventually will end up with some sort of local data store which is pretty close to the second idea…
- The topic ‘Accessing external existing databases’ is closed to new replies.