You could install a free WordPress plugin to enable “LOGIN / LOGOUT” menu links – or you could use a super simple snippet. Once again, the less plugins you use the better, especially if you can substitute them with a few lines of code.
In this case study, I have added the “My Account” page to the navigation menu and renamed it to “ACCOUNT” in the menu settings. However, I want that label to change to “LOGIN” if the user is logged out. Enjoy! Continue reading WooCommerce: Rename “My Account” Menu Label If Logged Out
We already saw a lot of examples e.g. hiding checkout fields if a product is virtual or hiding checkout fields if a checkbox is manually checked… but this time I want to hide a field dynamically – automatically – based on whether another checkout field is empty or no value is entered in it.
In this example, I will demonstrate how to hide the “billing phone” if “billing company” is empty or no information is entered. Of course, you also have to disable the mandatory attribute for the billing phone from the Customizer settings, otherwise checkout validation will fail. You can readapt this snippet to any other checkout field combo – just identify the IDs and change jQuery accordingly. Enjoy! Continue reading WooCommerce: Dynamically Hide / Show Checkout Field
There are many plugins that would allow you to do this in bulk or that would give you advanced features… but what if you just need to hide one product ID for users who are visiting your WooCommerce website from a specific country code?
This is also called “geolocation” – remember this won’t work unless you’ve enabled geolocation inside the WooCommerce general settings. Other than that, get your product ID, find out the target country 2-letter code, and this snippet will do the trick. Enjoy! Continue reading WooCommerce: Hide Product Based on IP Address / Geolocation
A nice way to avoid user frustration is to never let them pick a product / variation that is out of stock, only to realize later they can’t purchase it.
A variable product comes with a “select dropdown” on the single product page, from which customers can pick their favorite variation. Problem is that ONLY after selecting this they will find out about price, stock status and may be able to add to cart.
Today, we’ll completely disable (grey-out) those select dropdown options (variations) that are out of stock, so that users don’t waste time and only pick one of those that are in stock. Enjoy! Continue reading WooCommerce: Disable Out of Stock Variations @ Variable Product Dropdown
You can apply this simple jQuery snippet to any piece of WooCommerce content.
In this example we’ll see how to truncate the WooCommerce single product short description and place a “read more” link to reveal the hidden content… however you can also apply this to the long description, a custom product tab, the product gallery, and whatever can be truncated.
Enjoy! Continue reading WooCommerce: Truncate Short Description With “Read More” Toggle
You could use a popular plugin called Widget Logic, or instead you could keep it simple with a few lines of PHP. Here’s a snippet for you in case you need to conditionally hide a certain sidebar widget given a condition e.g. if you’re on the Cart page.
Of course, you can use any of the available WooCommerce conditional tags and make this more complex, but in this example we’ll keep it simple and check if we’re looking at the Cart page (thanks to the is_cart() conditional). Enjoy! Continue reading WooCommerce: Conditionally Hide Widgets
Because “split” might not be the correct term, let me explain this better.
Let’s imagine your WooCommerce cart table is sorted by A>Z (with my WooCommerce cart sorting snippet for example). If your business model and/or UX requires it, then you might need to “add a cart table row” to communicate the fact those items belong to that letter:
- Item 1 Title: “AAA”
- Item 2 Title: “ACC”
- Item 3 Title: “BDD”
- Item 4 Title: “BEE”
Once again, this might sound incomprehensible so you’d better look at the screenshot below. Enjoy! Continue reading WooCommerce: “Split” Cart Table (A>Z Headings)
This time around we’ll take a look at some SQL. As you know, WooCommerce orders (same as WooCommerce products) are stored in the WordPress database.
Instead of using complex PHP loops and conditionals, sometimes knowing a bit of database “SQL SELECT” can help. I took some inspiration (because I don’t know everything by heart) from the “wc_customer_bought_product()” WooCommerce function, which contains some SQL to check if a user has purchased a given product.
I’ve played a little with the same SQL SELECT call, and managed to return the list of user email addresses who have purchased a specific product ID. If you’re ever going to need this, enjoy! Continue reading WooCommerce: Get List of Users Who Purchased a Product ID
When you’re doing custom PHP work, this snippet will come handy. It’s a quick way to get a sublist of product IDs based on product meta criteria – in this case we’ll get a list of products that have “_downloadable” set to “yes” (which, translated in English, means they are “downloadable”).
Of course, you can edit this snippet to get any sublist of product IDs, for example in stock products, custom field value products, below/above sales number, and so on. Whatever is stored as a custom field can be used. Enjoy! Continue reading WooCommerce: Get List of Downloadable Products
We already saw how to hide Order Notes on the WooCommerce checkout page. This time around, however, our goal is to “move” them – and specifically remove them from their default position (under the shipping form) and add them back under the billing form.
As you can imagine, this is a combo snippet: (1) we remove them (and we’ll use the snippet as per the link above) and (2) we create a new billing field. Finally, (3) we also need to “save” this new field value into the original order notes custom field meta.
If this is difficult to understand don’t worry – just copy/paste the snippet into your functions.php and see magic happen. Enjoy! Continue reading WooCommerce: Move Order Notes @ Checkout
The default WooCommerce Dashboard Products page (/wp-admin/edit.php?post_type=product page) shows the list of products in a table. Default fields are: Image, SKU, Stock, Price, Categories, Tags, Featured and Date.
Sometimes, these columns are not enough and you need more. For example, you might want to quickly take a look at a product custom field, such as “visibility” (whether the product is hidden or not).
So, here’s the snippet for that. Of course, you can adapt it to show your own custom field, an ACF field or whatever product-related information you require. Continue reading WooCommerce: Additional Products Table Column @ Admin
You can add first and last name to the WooCommerce registration form (easy, no?). Or maybe a custom radio field. And why not, a file upload input – which is able to load an image from the user’s browser, assign it to the form, and add the image to “Media” in your WordPress install.
And today we’ll see exactly that. Unfortunately the “woocommerce_form_field” function does not allow (yet, maybe) to generate file input fields, so we’ll just use basic HTML. Enjoy! Continue reading WooCommerce: File Upload @ My Account Registration Form
If you’ve enabled customer registration on the My Account page, you will know that a new user is automatically logged in as soon as they successfully register.
This is great – however in certain cases it could be “dangerous” (for example, if you need to manually approve each user).
As I was searching through the WooCommerce plugin code, I found a great action filter that can immediately stop this from happening – I love one-liners! So, here’s the quick fix. Continue reading WooCommerce: Deny Automatic Login Upon Registration @ My Account
WooCommerce picks related products on the Single Product Page based on product categories and/or product tags. Related products are very important to the shopping experience, and sometimes this is not enough – what if you want to automatically show certain products based on different criteria?
So, here’s a quick snippet to e.g. get related products with the same product title of the current one. A very strange example, but you can use this as reference in case you want to get products based on different criteria. The get_posts() function, in fact, can be customized to get products with a given stock, certain price range, same custom field value, search term, and so on. Continue reading WooCommerce: Custom Related Products
By default, the WooCommerce Checkout page redirects you back to the (empty) Cart page in case there are no products in the Cart.
Now, there are times when you still need users to see the Checkout page and avoid this redirect. And thankfully, WooCommerce provides us with two filters that we can use to immediately disable this default behavior. Enjoy! Continue reading WooCommerce: Show Checkout Even If Cart Is Empty
Here’s a super quick snippet you can use to rename the WooCommerce checkout “state” field label for both billing & shipping.
Of course you can learn by example and apply the same snippet to other checkout fields, all yopu need to change is the field ID. Enjoy! Continue reading WooCommerce: Rename “State” Label @ Checkout
I’m surprised WooCommerce doesn’t offer this field out of the box. Most ecommerce websites actually require a shipping phone to organize delivery and communicate with the end customer in case there are problems.
Thankfully, there is a hook (filter) for that. It’s called “woocommerce_checkout_fields” and can be used to remove, move or add checkout fields quickly. And here’s how to add, for example, a new shipping field called “shipping_phone”. Enjoy! Continue reading WooCommerce: Add Shipping Phone @ Checkout
We already talked about displaying the total number of sales for a given product ID, however what if you just want to calculate sales in the last week?
This is a much better option when you have a busy WooCommerce shop and want to increase your sales conversion rate.
So, here’s the snippet – paste to your functions.php and enjoy! Continue reading WooCommerce: Count Product Recent Sales
For tracking purposes, or maybe because your shop manager needs to be aware of this, saving the total weight of each order and displaying it on the single order admin page is quite simple.
That’s right – WooCommerce does not save this value by default. You either need to save it yourself into the “order meta” or recalculate the weight based on the order items and their quantities. Here, we’ll cover option one (saving is better than calculating in regard to performance).
Enjoy 🙂 Continue reading WooCommerce: Save & Display Order Total Weight
Let’s say you’re developing a custom sales page or a landing page. A great way to increase your conversion rate is by showing the number of purchases close to the “add to cart” button.
We’ve already seen how to do this on the single product page, but what if you need to show this on a custom page, and therefore you need a shortcode?
Well, this is super easy and I’m currently using the snippet below on my own website, and specifically in the pricing table of my #CustomizeWoo online course sales page. So, here you go – enjoy! Continue reading WooCommerce: Calculate Sales by Product ID (Shortcode)