
A client had Carding attacks on his WooCommerce store. The result was very small orders that all end up failed. To stop this carding attack I added a minimum order. This effectively stopped Carding attacks and it also a good way to add a minimum order to a store.
Inspired by Business Bloomer – WooCommerce define a minimum order amount.
I then worked with ChatGPT on a new code.
- Set a $30 minimum order amount.
- If below minimum order amount disable checkout button.
- If someone enters the checkout page without having a minimum order in place they will be redirected to the cart page.
The final result of the minimum order subtotal amount code.
Added to a Code snippet plugin.
// Set the minimum order amount (based on subtotal)
function custom_wc_minimum_order_amount() {
return 30;
}
// Show error notice if subtotal is below minimum
function custom_wc_minimum_order_notice() {
if ( is_admin() || ! is_cart() || defined( 'DOING_AJAX' ) ) return;
$minimum = custom_wc_minimum_order_amount();
$total = WC()->cart->get_subtotal();
if ( $total > 0 && $total < $minimum ) {
// Avoid duplicate notices
$notices = wc_get_notices( 'error' );
foreach ( $notices as $notice ) {
if ( isset( $notice['notice'] ) && strpos( $notice['notice'], 'minimum order amount' ) !== false ) {
return;
}
}
wc_add_notice(
sprintf(
'Your current order total is %s — the minimum order amount is %s.',
wc_price( $total ),
wc_price( $minimum )
),
'error'
);
}
}
add_action( 'woocommerce_cart_loaded_from_session', 'custom_wc_minimum_order_notice' );
add_action( 'woocommerce_before_cart', 'custom_wc_minimum_order_notice', 5 );
// Disable "Proceed to checkout" button if below minimum
function custom_wc_disable_checkout_button_below_minimum() {
if ( ! is_cart() ) return;
$minimum = custom_wc_minimum_order_amount();
$total = WC()->cart->get_subtotal();
if ( $total > 0 && $total < $minimum ) {
?>
<style>
.checkout-button {
pointer-events: none;
opacity: 0.5;
cursor: not-allowed !important;
}
</style>
<?php
}
}
add_action( 'woocommerce_proceed_to_checkout', 'custom_wc_disable_checkout_button_below_minimum', 20 );
// Block checkout access if subtotal is below minimum
function custom_wc_block_checkout_if_below_minimum() {
if ( is_admin() || ! is_checkout() || defined( 'DOING_AJAX' ) ) return;
if ( WC()->cart && ! WC()->cart->is_empty() ) {
$minimum = custom_wc_minimum_order_amount();
$total = WC()->cart->get_subtotal();
if ( $total < $minimum ) {
wc_add_notice(
sprintf(
'Your current order total is %s — the minimum order amount is %s.',
wc_price( $total ),
wc_price( $minimum )
),
'error'
);
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
}
add_action( 'template_redirect', 'custom_wc_block_checkout_if_below_minimum', 5 ); // Priority 5 = early
CSS code used
.woocommerce-error {
border: 1px solid #d63638;
background-color: #fcebea;
color: #a00e0e;
border-radius: 4px;
list-style: none;
}
.woocommerce-error li {
list-style: none;
position: relative;
}
.site-main .entry-content ul li, .site-main .entry-content ol li {
position: relative;
padding-left: 30px !important;
}