I do a lot of integration work between e-commerce cart software packages and third party systems. Many e-commerce carts (Sunshop, VirtueMart, ZenCart, etc.) maintain their own database tables for storing orders, product, categories and everything the cart needs for the store to operate. Accessing data from these tables is straightforward; the data is normalized for a cart application, with no meta representation of data. Translation: In a typical cart database, you’ll find an order table, a product table, a customer table, a product category table, etc. It’s not difficult to extract details about what takes place in the cart.
WooCommerce has become a very popular e-commerce cart, riding on the coattails of WordPress’ great success. I’ve implemented WooCommerce a number of times and I think it’s an excellent, light-duty cart. As a WordPress plugin, WooCommerce benefits from other, 3rd party plugins for expanding the cart’s functionality. But what happens when you have to deal with a one-of-a-kind integration between Woo Commerce and a system for which there is no plugin solution? We quickly find ourselves digging into the WordPress database architecture, since WooCommerce uses a blend of it’s own tables and WordPress post and taxonomy tables to store data.
Below are some basic MySQL queries for getting WooCommerce products, categories, orders, and order items from the database. All of the queries assume the default “wp_” prefix to the database tables, so you may need to modify those prefixes if your tables are named differently. I’ve also left some example product and order numbers in place to complete the syntax of the queries. I’ve highlighted those values in the examples. Change to suit your particular use.
These are raw MySQL queries. I use them in SQLyog, an excellent MySQL admin tool. If you’re writing a PHP script for your own application needs, you can adapt these queries into your code. Lots of example of how to do that at php.net.
Query #1: Return a list of product categories
SELECT wp_terms.* FROM wp_terms LEFT JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id WHERE wp_term_taxonomy.taxonomy = 'product_cat'
Query #2: Get the category for a specific product
SELECT wp_term_relationships.*,wp_terms.* FROM wp_term_relationships LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id WHERE post_type = 'product' AND taxonomy = 'product_cat' AND object_id = 167
In above example, we’re getting the category for product 167; substitute your desired product ID, which you can find in the WooCommerce Products admin menu area.
Query #3: Return a list of product attributes for a specific product
SELECT wp_posts.ID,wp_posts.post_title,wp_postmeta.* FROM wp_posts LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE post_type = 'product' AND wp_posts.ID = 167
In above example, we’re getting the attributes for product 167; substitute your desired product ID, which you can find in the WooCommerce Products admin menu area.
Query #4: Return a list of line item details for a specific order
SELECT wp_woocommerce_order_itemmeta.*,wp_woocommerce_order_items.* FROM wp_woocommerce_order_items JOIN wp_woocommerce_order_itemmeta ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id WHERE order_id = 7373 ORDER BY wp_woocommerce_order_itemmeta.meta_key
In above example, we’re getting the line item details for order 7373; substitute your desired order ID, which you can find in the WooCommerce Orders admin menu area.