WooCommerce: Delete Customer After a Failed (Spam) Order

Once again, I’m trying to find WooCommerce anti-spam workarounds to avoid manual admin work like receiving hundreds of emails, deleting hundreds of fake orders and fake WordPress users.

In today’s “episode” I will try to clean the WordPress User database table after a failed order, because I’m really angry when “17PmU3MmKZS9ZAy7 17PmU3MmKZS9ZAy7” manages to register an account on Business Bloomer after a carding attack!

Please test this snippet on a dev/staging environment and not directly on the live site. Deleting a WordPress user is never a good idea, so you need to make sure everything is working as it should. Enjoy!

Continue reading WooCommerce: Delete Customer After a Failed (Spam) Order

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: Checkout Anti-Spam Honeypot

Here’s my second attempt to fight against WooCommerce spam, without installing a captcha plugin. A few posts ago I covered the My Account user registration spam, so today I want to tackle the WooCommerce Checkout, and try to “trick” spam bots.

Of course, this is a workaround and smart bots may understand you’re tricking them. So, feel free to test this first and let me know if it stops spam orders, card testing attacks, and tons of fake user registrations.

Same as the other post, I will be adding a custom hidden checkout field with an empty value. This won’t be visible to the customer, but will be to spam bots, which will try to post a value. My validation code will, however, generate an error if the custom field posts a value, and therefore should prevent most spam bots from going through.

Let me know if it works!

Continue reading WooCommerce: Checkout Anti-Spam Honeypot

WooCommerce: Display “Related Product Categories” @ Single Product Page

The WooCommerce Single Product page, on top of letting you add to cart the current item, also displays a list of related products and up-sells (when defined).

But what if you ALSO want to show a grid of “related product categories”, so that the customer can easily navigate to a category page instead of a single (related) product?

In this experiment, we will first calculate the current product’s categories, and then use a WooCommerce shortcode to output them as a grid, right below the related products section. Enjoy!

Continue reading WooCommerce: Display “Related Product Categories” @ Single Product Page

WooCommerce: Add to Cart Form @ Shop Page

We’ve already covered a similar customization on Business Bloomer: how to display Ajax quantity selectors on the WooCommerce Shop page. The problem there was that that only works for simple products, and only when Ajax add to cart is activated via the WooCommerce settings.

Today I’d like to experiment a little, and see what happens when we include the “add to cart template” (i.e. the one you see on the WooCommerce Single Product page) under each product on the WooCommerce Shop page.

This *should* work with all product types – especially variable products – because you will see the attribute dropdown selectors in such a case, as if you were on the single product page.

I haven’t tested this with grouped, bundle, and special product types, so feel free to share your findings in the comments below. Enjoy!

Continue reading WooCommerce: Add to Cart Form @ Shop Page

WooCommerce: Disable Payment Gateway By Product Type

Ever wondered if you could toggle payment gateways based on whether a given “product type” is in the WooCommerce Cart? For example, disable PayPal if there are simple subscription products, or enable Stripe only when there are variable products?

Well, with this simple code (and the 2 examples) you find below, you can manage to achieve just that. The second example regards the WooCommerce Subscriptions plugin, as this requires some plugin-specific functions to check for the product type. Enjoy!

Continue reading WooCommerce: Disable Payment Gateway By Product Type

WooCommerce: Get Product / Order Cross-Sells

Getting the list of cross-sells for a WooCommerce product is actually super easy (yes, it’s one line of PHP). But what if you need to “calculate” the list of cross-sells for an entire order, made of different products?

In this short tutorial for developers we’ll see both: how to get the cross-sell IDs for a product, and how to get the entire range of cross-sells based on what’s been purchased in a given order. Enjoy!

Continue reading WooCommerce: Get Product / Order Cross-Sells

WooCommerce: Anonymize All Users & Orders

Especially when you need to let other people (such as developers) log in to your WooCommerce website, you may want to protect the identity of your customers and your order details.

Of course, anonymizing your WooCommerce backend requires a complete database override – this change is 100% irreversible! Only run this code if you know what you’re doing.

The ideal workflow is the following: you give developers access to a staging/clone website version, you run this custom code to anonymize customers and orders, and have them do the changes. This is good for GDPR, CRPA and PIPEDA as well: third party people won’t see sensitive data.

One more note: I haven’t tested the code with thousands of customers and orders – feel free to leave a comment in case your (staging) website crashes. Enjoy!

Continue reading WooCommerce: Anonymize All Users & Orders

WooCommerce: See If Product ID Belongs To a Grouped Product

Grouped Products are part of WooCommerce core and let you “add similar (think materials, design, etc.) products to a single grouped product. This allows the customer to add products […] to their cart on one page instead of navigating to several different pages to do so” (WooCommerce docs).

It may happen during your coding career that you need to know whether a given Product ID is part of a Grouped Product (so, it’s a “child” of a Grouped Product) – the snippet below will help with that. Enjoy!

Continue reading WooCommerce: See If Product ID Belongs To a Grouped Product

WooCommerce: Allow CSV Product Export By Tag

Ok, this is a little hacky and pretty manual, but it happened to me in the past that the inbuilt WooCommerce CSV product export was just not enough.

In other words, the default product export that you can find by clicking the “Export” button on the “Products” WordPress Dashboard screen, gives you the option to export all products to a CSV. You can also refine the list by product type and product category, so that you can export specific products only.

What’s missing there is a “product tag” filter, so this workaround will let you do just that – define a product tag slug in the snippet below, and your export list will be automatically filtered by that.

Of course, you could find a dynamic way of doing that, but for now we’ll keep this manual and hardcoded into the PHP function. Enjoy!

Continue reading WooCommerce: Allow CSV Product Export By Tag

WooCommerce: Search Products By Custom Field (Backend)

In WooCommerce, there are two kinds of search: the customer one (frontend) and the admin one (backend). We’ve already covered how to let customers search into custom field values on top of the default product title and description, so this time we’ll talk about the backend search.

Let’s say, as a WooCommerce store admin, that you’ve added a product custom field (e.g. “gtin“), and you want to make sure the backend search also returns products where “gtin” is equal to the search term. The snippet below will help you do that. Enjoy!

Continue reading WooCommerce: Search Products By Custom Field (Backend)

WooCommerce: Pre-defined Add to Cart Quantity Selectors

I seriously spent more than usual trying to write a decent title. Still, I’m not 100% sure I’ve explained it well – so here’s some more context.

The WooCommerce Single Product Page add to cart form features a quantity input and an add to cart button. Super simple. Customers can define a quantity and add the current product to the cart.

Now, let’s imagine you want to change this experience based on your business requirements, and instead of the quantity input and add to cart button you want to show 3 buttons: “Add 1x to the cart“, “Add 2x to the cart“, “Add 3x to the cart.

And if you can match this with a bulk quantity discount functionality, you can even change the messaging to e.g. “Add 1x to the cart“, “Add 2x to the cart and save $X“, “Add 3x to the cart and save $Y“…

So, let’s see how to hide the default add to cart form, and instead show buttons that allow the customer to add to cart a pre-defined product quantity (for simple products). As per this screenshot:

Continue reading WooCommerce: Pre-defined Add to Cart Quantity Selectors

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: “Load More Related Products” Ajax Button @ Single Product Page

As you know, the WooCommerce Single Product Page displays a set amount of related products (usually 4). But despite the fact that you can customize the number of related products via code, there is no setting that allows you to have a “LOAD MORE” button instead.

My goal is therefore to show as many Related Products as the user wants without reloading the page, so that they can find out more potential matches and increase the chances to place an order.

I must say this took me the whole morning and it’s not yet finished. There are two small bugs: (1) the “Load More” button does not hide once there are no more related products and (2) once the current product’s related products are finished (so, after clicking on the load more button 1 or more times), the Ajax keeps showing the last related product as opposed to show none. Feel free to contribute if you wish to help!

Having said that, let’s see how to implement an Ajax “load more” feature. You can also reuse this on different projects (e.g. “load more blog posts”), because once you get to understand how Ajax works then you can do lots of cool stuff without refreshing the page.

Enjoy!

Continue reading WooCommerce: “Load More Related Products” Ajax Button @ Single Product Page

WooCommerce: Disable Related Products Shuffle

By default, the WooCommerce Single Product page features a Related Product section. Here, you’ll find products that are related to the current product, based on product categories and product tags in common.

All good so far, but we need to make a few more notes: whenever WooCommerce “calculates” the list of Related Products, it searches for 15 of them (unless otherwise specified via custom code). Then, it shuffles them. And finally it gives you the first 5 of them (unless otherwise specified via custom code). At this stage, these are sorted by “rand” (unless otherwise specified via custom code).

This is because WooCommerce wants people to see different related products each time a single product page is loaded. It’s potentially good, but also it may get messy when, as a store owner, you may want to direct people to the same related products over and over again (i.e. always show the same set of related products).

And in order to do that, we need to do 2 changes: disable the shuffle, and disable the “rand” sorting. In this way, you should be able to show the same Related Products to all customers. Let’s see how this is done!

Continue reading WooCommerce: Disable Related Products Shuffle

WooCommerce: Get Total Sales By Product Category

It’s easy enough in WooCommerce to get/calculate product sales. What’s difficult, on the other hand, is calculating the total amount of sales for a specific category, because there is no core function that already does that.

Why sales by category – you may ask? Well, to me, that’s a very important metric. For example, I sell both consulting and non-consulting products on this same website, so it’s important for me to keep track of category sales year-on-year, especially when my goal is reducing 1-to-1 client work while increasing scalable product sales such as courses, plugins and memberships.

In this quick tutorial, we will first get the “WooCommerce orders that contain a target product category”, and after that we will loop through the array to calculate the total sales for that specific category. Sounds difficult? No worries – just copy and paste the snippets below.

Continue reading WooCommerce: Get Total Sales By Product Category