Filtering WooCommerce Orders June 29 2017
The post Filtering WooCommerce Orders appeared first on SkyVerge.
Filtering your WooCommerce orders gives you a way to find the transaction records you’re looking for faster. When we’re working on threads in the help desk, this is useful to us as developers as well so we can find the orders relevant to the issue quickly.
One of the most helpful filters for debugging is to filter orders by payment method used, as we’ll use this for payment gateway support, or when seeing if an issue occurs with one type of gateway (like an off-site gateway) but not another. Since we recently built a helper plugin to do just that, I’ll go over a couple useful plugins for filtering your WooCommerce orders today.
Filter WooCommerce Orders by Payment Method
For the merchants in the crowd, you can download the code above as a plugin by clicking “Download .zip” in the top-right, then installing it into your store just like any other plugin. For the developers, we’ll go a bit more in-depth as to what this is doing shortly.
When using this plugin, you’ll see that a new filter is added to your orders list, letting you choose which payment methods to show:
If you select one to filter your orders, you’ll see that only orders placed with that payment gateway will be shown rather than the complete order list.
That’s all there is to it! You can now see only PayPal orders, only credit card orders, or even better, if you perhaps allow cash on delivery or another non-standard / paid “up front” method, you can check for these orders easily!
Behind the scenes
For the developers, let’s have a look at what this plugin is actually doing. You’ll notice it uses 2 hooks:
-
restrict_manage_posts
— lets us insert content before the “Filter” button in any posts lists. Since we only want to filter orders, notice that the callback hooked in here uses the global$typenow
to check for the right post type list, then it outputs my selector. -
request
— lets us adjust query variables to adjust returned results. In our case, since payment method is stored as post meta, we’ll add a coupleWP_Query
custom parameters for the payment method meta key and the value we’re filtering for.
When we combine these two hooks, we can (1) add the selector for the filter, and (2) when “Filter” is clicked, get the URL parameter from our selector to adjust the orders query using our filter.
This could prove really useful if you have your own custom meta keys for which you’d like to be able to filter orders, such as delivery date, fulfillment provider, or other data.
Filter WooCommerce Orders by Coupon
Now let’s take a slightly more complex example from a developer perspective, but still a useful tool for merchants. We can filter orders by the coupon used, which can be nice for checking orders from a promotion or if a coupon requires further action from the merchant.
You can download and install this plugin by clicking “Clone or Download”, then “Download ZIP” to get a plugin file you can install. When added to your site, this will add a new filter listing all published coupons in your store. You can then choose which coupon you’d like to filter for, and your orders list will adjust accordingly.
Behind the scenes
This sort of query is more complicated to adjust because coupon usage is not stored on the order object itself, which is what we’re filter for. Instead, it’s stored as an order item. Since order item data is stored in a separate database table, we’ll need to use SQL to JOIN
these tables to filter a query like this.
As a result, you’ll see that we don’t use the request
hook here, but rather substitute 2 different hooks instead:
-
posts_join
— lets us join another table onto the posts query. We’ll use this to check thetypenow
, and join the order items table to the orders table if filtering orders (and of course, ensure our filter is actually being used so we don’t do this unnecessarily). -
posts_where
— this is what basically takes the place of therequest
hook now, letting us directly modify the SQL query to add aWHERE
condition, checking for the data we’ve added in our previousJOIN
.
Since fees, products, shipping methods, and coupons are all stored as order line items, this sort of filtering could let you filter by shipping method selected, whether a particular fee was chosen or not (e.g., getting all orders that opted for expedited handling or shipping insurance), and more.
In either case, this scratches the surface of what you can do to filter WooCommerce orders in your shop admin, helping merchants find the specific orders they need, or pointing developers towards what they’d need to modify to get their own filtered order lists.
The post Filtering WooCommerce Orders appeared first on SkyVerge.