Skip to content

Instantly share code, notes, and snippets.

@Rahmon
Last active October 28, 2022 21:50
Show Gist options
  • Save Rahmon/ff0d8773b163a6bf4c8c990d3f2156f1 to your computer and use it in GitHub Desktop.
Save Rahmon/ff0d8773b163a6bf4c8c990d3f2156f1 to your computer and use it in GitHub Desktop.
Define a minimum order amount for WooCommerce
<?php
// Check the minimum amount when the order is processed.
add_action( 'woocommerce_checkout_process', 'custom_woocommerce_minimum_order_amount' );
// Check the minimum amount on the cart page.
add_action( 'woocommerce_before_cart', 'custom_woocommerce_minimum_order_amount' );
// Check the minimum amount on the mini cart.
add_action( 'woocommerce_before_mini_cart', 'custom_woocommerce_minimum_order_amount' );
/**
* Define a minimum order amount for WooCommerce
*
* @author Ramon Ahnert <https://profiles.wordpress.org/rahmohn/>
*/
function custom_woocommerce_minimum_order_amount() {
// Set this variable to specify a minimum order value.
$minimum = 50;
if ( 1 < did_action( 'woocommerce_before_mini_cart' ) ) {
return;
}
if ( WC()->cart->total < $minimum ) {
$message = sprintf(
// translators: %1$s cart total; %2$s minimum order amount.
__( 'Your current order total is %1$s — you must have an order with a minimum of %2$s to place your order' ),
wc_price( WC()->cart->total ),
wc_price( $minimum )
);
if ( is_cart() ) {
wc_print_notice( $message, 'error' );
} else {
wc_add_notice( $message, 'error' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment