We’ve already seen how to hide products based on IP addresses, hide products belonging to a specific category and even hide all products from the Shop page.
Today I want to publish another version of those, but this time we’ll use a custom field / ACF in our conditional logic, so that we can toggle the product visibility based on its value. Enjoy!
Continue reading WooCommerce: Hide Products Based On Custom Field / ACF Value
By default, the WooCommerce Shop page displays simple, variable, grouped, bundle and other product types. As you know, each variable product is made of one or more “single variations”, and these are only visible in the single product page.
Now, what if you want to display a grid of all “single variations” in a custom page / post? Well, a shortcode can be coded so that you can achieve just that. Enjoy!
Continue reading WooCommerce: Display All Single Variations (Shortcode)
This is a great customization for those WooCommerce store owners who are willing to accept donations, custom amounts, or need anyway that the customer enters a custom price on the product page for paying an invoice or a bill.
This is as simple as creating a simple product with $0 price, and after that using the snippet below to display an input field on the single product page, where customers can enter their custom amount. Enjoy!
Continue reading WooCommerce: Allow Customers To Define the Product Price
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
By default, WooCommerce does not display the product thumbnail inside the order emails.
With this little trick, however, you can revert this default behavior and indeed show images beside their product name inside the email order item table. Enjoy!
Continue reading WooCommerce: Show Product Image @ Emails
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
Ok, this is an unusual snippet today, but it may happen that for performance / security / conflict / conditional / privacy reasons you may need a certain WooCommerce user role to not see / load / use a given plugin.
Let’s think of an example: as an administrator, you wish to use a CRM plugin to sync your order data to an external software. This plugin, however, does not have the ability to exclude Shop Managers from accessing it, and you don’t want to install yet another plugin to define who can access and who can not.
Another case scenario: Shop Managers and Administrators wish to use a live chat plugin, but they want to restrict the live chat visibility to logged in customers only, while logged out customers should not see anything, hidden code included.
There are a million reasons why this could be helpful. So, let’s see how to actually deactivate a plugin (not disable its scripts – but actually deactivate it) with a handy piece of code. Test it and only then – enjoy!
Continue reading WooCommerce: Disable A Plugin For Customers / Shop Managers
Whenever we need to do custom WooCommerce work, remembering how to get “things” is the most time-consuming task. So, a Google search often helps us find the right and quickest solution.
For example, it’s easy to “get the current product’s categories“, but how can we get the “parent product categories” only? In today’s snippet we will see how this can be achieved in 7 lines of PHP. Enjoy!
Continue reading WooCommerce: Get Product Parent Categories
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
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
The WooCommerce plugin allows you to manage stock for each product, but you only have a single stock quantity field!
What if you have two warehouses and, as a store admin, need to manage the inventory for each location? Besides, what if an item is out of stock at location 1, but it’s in stock at location 2, and therefore the customer needs to be able to purchase it?
This amazing workaround will add a second input number in the product settings, redefine stock quantity and status on the frontend by summing up stock 1 + stock 2, and finally decrease stock 1 until it goes to 0, after which it will decrease stock 2.
This default behavior can be changed of course e.g. it’s possible to define from where the stock is reduced (by distance?) via additional code. Also, additional code can be written to make it compatible with variable products or custom product types, as well as make it work with refunds. Either way, enjoy!
Continue reading WooCommerce: Additional Stock Inventory Location
So, we all know that the “Store Address” fields under WooCommerce > Settings > General are used by other WooCommerce functions such as the initial setup wizard, currency switchers, language plugins as well as taxes and shipping calculations. Also, it may display on PDF invoices, WooCommerce emails and static pages.
This is all good and easy, but as usual businesses are not made equal. It could be that you need to show an additional address; for example, the “Warehouse Address”.
In this tutorial, we will add a new “Warehouse Address” section and address fields under the “Store Address” settings, and also see how we can easily retrieve this custom address so that you can display it anywhere. Enjoy!
Continue reading WooCommerce: Additional “Store Address” @ General Settings
It’s great that WooCommerce products clearly display their stock status and quantity on the single product page (and on the shop page, with this simple customization).
However, WooCommerce store owners often need to display the stock quantity in other sections of the website, such as the homepage, a blog post, a custom pricing table, and keep the quantity dynamic so that the text changes when there is a stock change.
We can therefore build a simple shortcode, that can automatically update the output, so that you never need to worry about changing that piece of content ever again. Enjoy!
Continue reading WooCommerce: Stock Quantity in a Shortcode
When an order that has previously reduced stock is “cancelled” or goes to “pending payment” because the payment failed or the store admin decided to change the order status, the wc_maybe_increase_stock_levels() function triggers and restores the order items’ stock quantity.
As usual, some WooCommerce entrepreneurs asked for a way to disable this automatic restocking given their custom setup. It often goes like that – you can’t really please everyone.
This is unless you’re a smart developer and can account for both options, thanks to a WordPress “filter“. Here’s a PHP one-liner that can immediately disable this default behavior, so that you can avoid the automatic restocking (and maybe doing it manually based on your business rules). Enjoy!
Continue reading WooCommerce: Disable Restocking @ Order Cancelled
There are certainly ways and plugins to turn WooCommerce into a quote engine, but today I want to share a super simple workaround that could be helpful to many.
In a nutshell, we’ll use the same WooCommerce cart/checkout flow, rename a few strings and buttons, enable an offline payment gateway (so there is no actual payment), let the admin revise the order and send back the final invoice, and finally get the customer to pay for their order.
Continue reading WooCommerce: Turn Checkout Into a Quote System
Especially when you do a lot of custom shipping, showing the shipping method title may not be a good idea. Honestly, the default “via ___” suffix beside the shipping cost doesn’t make much sense anyway.
So, let’s get rid of it. With a 1 liner. Enjoy!
Continue reading WooCommerce: Hide “Shipping Via ___” @ Thank You Page, View Order Page
How annoying is the fact you can only resend the “New Order Notification” from the single order admin page? What if you’re testing out and customizing email templates, and need to email yourself the “processing” or the “completed” notification, without having to place a new test order or switching order status twice to re-trigger the notification?
Well, today we will see how to add a “Resend whatever email” function under the “order actions” on the single order edit page. Of course, make sure you switch the billing email to yours, otherwise the customer will get these emails and not you. Enjoy!
Continue reading WooCommerce: Resend Any Order Email
You may wondering – “but I can already do that from the WooCommerce settings!“. Yes, that’s correct; go to WooCommerce Settings > Products > Inventory and set the “Hold Stock Minutes” value. After that period, unpaid orders will be marked as cancelled to make sure the stock goes back to the initial value.
The problem is – what if you don’t want to use the “Hold Stock Minutes” thing, and even better, what if you don’t use stock management at all? In that case, orders won’t be marked as cancelled automatically.
Also, what if you need to do conditional work e.g. you only want to cancel “failed” orders, while you want to keep “pending” ones as they are? Even in this case, the “hold stock” option won’t work, as you need to specify which order status you want to target and then run the cancel function.
Either way, enjoy!
Continue reading WooCommerce: Automatically Cancel Orders
On a default WooCommerce install, we add X products to cart by defining the add to cart quantity on the single product page.
But what if your shop sells spices? It’s likely that those quantities are actually pounds / kilograms / or whatever weight unit you require. And what if your WooCommerce business is entirely focused around Italian extra virgin olive oil (asking for a friend!), that you’d sell per liter?
In such custom cases, it’s nice to specify the unit beside the add to cart quantity, so that customers know how much and what they are buying. So, let’s see how to add an add to cart quantity suffix beside the quantity input field on the single product page. Enjoy!
Continue reading WooCommerce: Add to Cart Quantity Suffix
Go to a WooCommerce website. Search for a product. Get to the search results page, which displays from 0 to N products based on the search term, sorted by relevancy. Easy.
Now, let’s imagine you have a custom landing page for “Tables”, and you want people searching for “tables” to go to that page instead of the default search result page. Quite easy as well – thanks to a neat PHP snippet we will feature today.
Continue reading WooCommerce: Redirect Specific Product Search To Custom URL