Você está visualizando atualmente WooCommerce: Limit Daily Sales For Cheap Products (Anti-Spam)

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

  • Autor do post:
  • Categoria do post:Woocommerce
  • Tempo de leitura:5 minutos de leitura

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!

Sorry, humans! Unfortunately bots decided to purchase this product 3 times already today, so it’s not available any longer to anyone, stupid bots included! As you can see, the “Add to Cart” button is gone. Good bye, carding attacks.

PHP Snippet: Limit Daily Sales For Products $1-$9 @ WooCommerce Checkout

Note: feel free to change the product price range (in the first line of the function I exclude products outside the $1-$9 price value), and the daily sales threshold (in the second-last line of the function I set this to max 3 sales).

/** * @snippet Limit Sales To Avoid Carding Attacks @ WooCommerce * @how-to Get CustomizeWoo.com FREE * @author Rodolfo Melogli * @compatible WooCommerce 8 * @donate $9 https://businessbloomer.com/bloomer-armada/ */ add_filter( ‘woocommerce_is_purchasable’, ‘bbloomer_not_purchasable_after_daily_limit’, 9999, 2 ); function bbloomer_not_purchasable_after_daily_limit( $is_purchasable, $product ) { // CONSIDER ONLY PRODUCTS IN THE $1-$9 RANGE if ( $product->get_price() > 9 || $product->get_price() < 1 ) return $is_purchasable; // GET TODAYS ORDERS AND COUNT PRODUCT SALES $all_orders = wc_get_orders( array( 'limit' => -1, ‘date_created’ => date( ‘Y-m-d’ ), ‘return’ => ‘ids’, ) ); $count = array(); foreach ( $all_orders as $all_order ) { $order = wc_get_order( $all_order ); $items = $order->get_items(); foreach ( $items as $item ) { $product_id = $item->get_product_id(); if ( $product_id ) { $count[$product_id] = isset( $count[$product_id] ) ? $count[$product_id] + absint( $item[‘qty’] ) : absint( $item[‘qty’] ); } } } // LIMIT 3 DAILY SALES if ( $count[$product->get_id()] >= 3 ) return false; return $is_purchasable; }

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files – if you need more guidance, please take a look at my guide “Should I Add Custom Code Via WP Editor, FTP or Code Snippets?” and my video tutorial “Where to Place WooCommerce Customization?”

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

Follow @rmelogli

Post navigation