WooCommerce: Retrieve Paid Orders Only

The wc_get_orders function is the recommended method for fetching orders in WooCommerce due to its flexibility, safety, and adherence to best practices. It offers several advantages over using custom WP_Query or database queries. It’s also HPOS compatible.

You can specify various arguments to filter orders based on properties like status (pending, completed, on-hold, etc.), customer (ID, email), date range, order IDs, payment method, total, custom fields and more.

Today, I’d like to talk about paid orders – at the moment there is no way to filter out free orders with a wc_get_orders parameter, so we need to find a workaround.

My solution uses the “payment_method” parameter, which accepts a payment method ID (cheque, bacs, afterpay, paypal, stripe, woocommerce_payments, ppcp-gateway, klarna_payments, etc. based on the payment gateway plugins you use). Clearly, an order that comes with a specific payment method is a paid order.

Unfortunately you can only select one payment method (as opposed to an array), so we also need to do some array merging. Enjoy!

Continue reading WooCommerce: Retrieve Paid Orders Only

WooCommerce: Set Different Currency For Manual Orders

Business Bloomer website sells in USD only for the time being, as that seems to be the standard for WordPress plugin / products stores.

However, I prefer to invoice clients in EUR (I also switch PayPal and Stripe keys on the go). Which means, I needed to find a way to “set” a different currency whenever I create a manual order (which, by default, is in USD). So, here’s the fix!

Continue reading WooCommerce: Set Different Currency For Manual Orders

Cart item data from an external API?

I would like to save metadata in each item in the cart (collecting item data from an external API) and I would like this data to be maintained in the order.

I’m not interested in viewing it because it’s a metadata that serves me in the backend to process the order in the internal ERP.

Are there already any example snippets that can help me?

WooCommerce: Create Order From Contact Form Submission

So, I’ve built my own event management system for WooCommerce.

My objective was to embed a form on the event registration page, and then programmatically create a free WooCommerce order for that customer – so I can track the number of attendees (orders) and follow up with email automations (customers).

You can already see the system in action on the How to Contribute to WooCommerce Core” event page: you can clearly see an email input and a “Register Now” button inside the “You’re invited” section. That’s the form – actually a Fluent Forms plugin contact form.

And then there is a simple snippet that hooks into the Fluent Form submission, and conditionally generates a WooCommerce order.

In this post, you’ll learn about a quick way to create a Fluent Forms form on your WordPress site, about the “fluentform/validate_input_item_input_email” hook, and finally about the wc_create_order() WooCommerce function that, of course, let us generate an order automatically. Enjoy!

Continue reading WooCommerce: Create Order From Contact Form Submission

WooCommerce: Order Number Prefix / Suffix

As you know WooCommerce uses the “order ID” (which is also the ID of the order post in the database) as the order number. This displays in the “WooCommerce” > “Orders” table, on each line of the order, under the “Order ” column, as well as the order “quick view” window, the single order page and the customer’s My Account page.

But what if you need to add a prefix or a suffix to this number, so that this is in line with your business or invoice requirements?

Here’s the fix – enjoy!

Continue reading WooCommerce: Order Number Prefix / Suffix

WooCommerce: Get Order Tax Percentage

You’d think WooCommerce had an easy way to get the order tax rate percentage, the same way as you can get almost everything inside the “order object” with a single line of PHP. Well, that’s not the case unfortunately.

The reason is that probably each order can have multiple tax rates, and that tax is not only applied to product prices, but also optionally to shipping, fees and more.

So, how do we get the list of tax rates in a WooCommerce order? Here’s a quick snippet that you can use – enjoy!

Continue reading WooCommerce: Get Order Tax Percentage

WooCommerce: Limit Daily Sales For Cheap Products (Anti-Spam)

We could call this the “WooCommerce Anti-Spam Without a Plugin” series, while I attempt to fight against bad humans and very bad bots who love attacking the Business Bloomer checkout page with spam orders and fake user registrations.

My first attempts were (1) My Account registration anti-spam honeypot, (2) Checkout anti-carding-attack honeypot, and (3) Reducing the number of admin emails, but I can tell that (2) didn’t work, and I got another carding attack on a $9 product last weekend. Bots are smart.

Today, I’d like to share another anti-spam snippet that I’m currently testing on Business Bloomer. Most carding attacks, in fact, end up with the purchase of a single product in the $1-$9 range – which means that limiting the daily sales for specific, inexpensive, products may do the trick.

My code counts the times each product has been purchased during the day – and if a carding attack occurs, the product won’t be purchasable any longer until the end of the day. Because we’re talking about cheap products, it’s no problem for me to disallow legit sales as well for 24 hours. Use at your own risk, of course.

We already covered how to “Limit Sales Of A Product Per Day“, but this time I’d like to apply that to an array of products – and specifically all those that are under $10. Enjoy!

Continue reading WooCommerce: Limit Daily Sales For Cheap Products (Anti-Spam)

WooCommerce: Allow Order Editing For Custom Order Status

WooCommerce admins are allowed to “edit” an order only when this is in the “on hold” or “pending payment” status. By “edit” I mean having the chance of modifying or adding products, fees, shipping and recalculating the totals, which are not allowed once the order has been placed (“processing“, “completed“, etc.).

However, there are many reasons why you’d want to have the right to edit a processing, completed, or custom status order – of course as long as you don’t end up changing the total, as customers already paid at that stage.

Think about the following scenarios:

  • you customized the items table and added a custom field, and you want to set the custom field value when the order is “processing
  • you need to edit the shipping method name AFTER checkout, and you want to be able to rename it when the order is “completed
  • you need to add a fee and a discount of equal amounts (so that the total stays the same) before completing the order

Either way, let’s enable the little “pencil icon” on a custom order status, so that the admin can customize the order whenever they wish!

Continue reading WooCommerce: Allow Order Editing For Custom Order Status

WooCommerce: Add Product To Order After Purchase

On Business Bloomer I sell a bundle of products, and I use no Bundles plugin for that. So the challenge was to programmatically add a list of products to the order upon purchase, once the bundle product is purchased.

This is an amazing way to save time for the customer, as they don’t need to manually add each product to the cart. In the background, after a successful purchase, some magic code (that you find below) adds products to the order, sets their price to $0.00 (so that the order total is not altered), and saves the order. Enjoy!

Continue reading WooCommerce: Add Product To Order After Purchase

WooCommerce: Disable Orders Table Row Hyperlink @ Admin

If you access the WordPress dashboard and go to WooCommerce > Orders, you will be presented with the WooCommerce “Orders Table” i.e. the list of customer orders. Each row comes with several columns, such as the order number, order date, order status, order billing and shipping addresses and more.

What’s annoying sometimes is that the whole row is a clickable link, so if you need to copy and paste some information you will end up actually clicking on it, and being redirected to the single order page.

There is a way to add a specific “class” to each table row, so that it becomes not clickable, and the only link that remains is the very first column table cell – the order number (as it should be by default!).

So, let’s see how easy it is to alter this default behaviour. Enjoy!

Continue reading WooCommerce: Disable Orders Table Row Hyperlink @ Admin

WooCommerce: Change Address Format For a Specific Country

We’ve already covered a little about address formats when we added a custom House Number field to the WooCommerce Checkout page.

Address formats define the way Billing and Shipping addresses are formatted for the end user. Addresses show on the order received / thank you page, on transactional emails and on some My Account pages, and look like this:

{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}

Now, what if you wanted to move the “company” before the customer name, or maybe have “address 1” and “address 2” on the same line, or move “postcode” to the last line? Well, thankfully there is a neat WooCommerce filter called “woocommerce_localisation_address_formats” that allows us to do just that – reformat any address for any country we wish. Enjoy!

Continue reading WooCommerce: Change Address Format For a Specific Country

WooCommerce: Bulk Re-Send All Customer’s Completed Order Emails

This is quite an uncommon WooCommerce requirement, but especially for downloadable products the Completed Order email is such as important one – and customers may end up asking you to resend them all their past order emails.

This is an interesting snippet, as it features important functionalities: getting orders by billing email, looping through the results and re-triggering the Completed Order email whenever a specific admin URL parameter is posted. Enjoy!

Continue reading WooCommerce: Bulk Re-Send All Customer’s Completed Order Emails

WooCommerce: Limit Sales Of A Product Per Day

Yes, “manage stock” is a nice feature to make sure you don’t oversell a given product based on the stock you have in your warehouse. However, what if you also need to have a “daily sales limit” – say you can’t sell more than 3 of a given product ID in a given day?

This is an interesting functionality that is also helpful for you to learn how to get today’s orders, how to loop through the orders to find a specific product ID and sum its quantities, and finally how to use the woocommerce_is_purchasable filter to set if a product can be purchased or not (which means, the add to cart may or may not show). Enjoy!

Continue reading WooCommerce: Limit Sales Of A Product Per Day

WooCommerce: Get Order Fees Total

It’s official – there is no way to get the fees total from an order with a simple PHP getter (not sure why – you can get lots of values such as totals, addresses, dates, URLs with one line of code except for this basic thing!).

So, we’ve got to fix this. Let’s say you have access to the $order object (on the thank you page, in the WordPress dashboard, inside an order email, etc.); here’s a few lines of PHP you can use to calculate the total amount of order fees. Enjoy!

Well, there is actually one line of PHP you can use to calculate the order total fees (despite I didn’t think so while I was writing this post) – I’ve now added it to the list of order “getters” here (where you can get lots of order values such as totals, addresses, dates, URLs with one liners).

So, in order not to waste this post, you still find below the original way to calculate order total fees (by looping through all order fees and adding up totals), as well as the one liner that can help you save time. Enjoy!

Continue reading WooCommerce: Get Order Fees Total

WooCommerce: Item Custom Field @ Edit Order Page

Alright, this title may not really help you understand what I mean, so let’s put it in another way. When, as an administrator, you edit an order and are in the Order Items table, you have the chance to edit the item quantity, subtotal and discounted price (see screenshot below).

Now, what if you also want to have the freedom to display and edit another custom field, so that it is saved inside the order once you hit the “Save” button?

This could be useful for custom setups – for example let’s imagine the admin has the necessity to also define the “shipped quantity”, so that they know exactly if a specific order has been entirely fulfilled or requires a second shipment to get completed.

Either way, see the screenshot below, play with the snippet, and see how it goes. Enjoy!

Continue reading WooCommerce: Item Custom Field @ Edit Order Page

WooCommerce: Bulk Replace Product Inside Existing Orders

Manual operations are always a nightmare for WooCommerce store owners. Thankfully, a bit of code can help and actions that would normally take hours can be executed in a few seconds via PHP.

Today, we’ll take a look at a very edge case, but this can still be helpful to understand the code and re-adapt it to other scenarios. If as a store owner you tend to replace products or product lines, it’s possible that you may need to replace the old products with the new ones inside existing orders, retroactively.

It’s a one-off operation that could take hours if it had to be done manually, based on the number of existing orders. With this simple snippet, however, you can edit an unlimited number of orders, and let the code replace ordered items. So, let’s see how this is done. Enjoy!

Continue reading WooCommerce: Bulk Replace Product Inside Existing Orders

WooCommerce: Add Custom Meta Box @ Order Admin

When you edit an order as an admin, you’re presented with the usual WooCommerce layout: order details on the left, order action on the right, the list of order items on the bottom left, and – possibly – additional meta boxes added by third party plugins (e.g. the PDF Invoice and Packing List plugin order meta box where you can define the invoice date and number).

On top of that, it’s also possible, of course, to define our own custom meta boxes, so that the administrator and/or shop managers can view (or even enter) additional information.

I’ve used it on a client website to show a custom field, but you can print anything you wish – even documentation for shop managers (how to complete the order for example).

So, here’s how we add a new “section” (meta box) to the single order edit page in the WordPress dashboard, and how we display some content in it. Enjoy!

Continue reading WooCommerce: Add Custom Meta Box @ Order Admin

WooCommerce: Disable Emails For a Single Order

This is a cool customization that can come useful for WooCommerce store admins, especially when they do manual order status changes via the Orders admin page.

As you know, each order status change triggers an order email (“processing”, “completed”, “on-hold”, etc.), and sometimes the store manager doesn’t want to resend them after each edit.

In this quick tutorial, we will see how to add a checkbox to the single order edit page, so that emails are disabled as long as the checkbox is kept checked. Enjoy!

Continue reading WooCommerce: Disable Emails For a Single Order