I'm trying to Set a minimum order amount of $25. So far i found this code, which seems to be fine for blocking the checkout if minimum is not reached, but the subtotal that it is using is with the tax included and I need to exclude the tax in the total.
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 25;
if ( WC()->cart->subtotal < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
), 'error'
);
}
}
}
You're looking for
WC()->cart->subtotal_ex_tax
This will give you the WooCommerce Cart subtotal excluding tax
Source: https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html#48-49
Related
This is my code I saw this in stack overflow but it's not working anyone please help me?
<?php
$current_screen_user = wp_get_current_user();
if( in_array( 'wholesale', $current_screen_user->roles ) ) {
$minimum = 251; // Set the minimum amt.
$cart_amt = WC()->cart->subtotal; // cart sub_total, this is actual total excluding discounts and shipping.
if ( $cart_amt < $minimum ) {
if( is_cart() ) {
//Added notices for cart page.
wc_print_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( $cart_amt )
), 'error'
);
} else {
//Added notice msg for checkout page.
wc_add_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( $cart_amt)
), 'error'
);
}
} else {
do_action( 'woocommerce_proceed_to_checkout' );
}
} else {
do_action( 'woocommerce_proceed_to_checkout' );
}
?>
I don't know how to call woocommerce cart global variable. I try many ways but it's not working main logic is -- >> if the cart value is less than 250 then hide the checkout button and else it is greater than 250 then show the proceed to checkout button.
I am using a snippet to set minimum order value on WooCommerce store. However, I am having some issues if coupon is applied.
For example, minimum order value is 500 and customer applies a coupon with a value of 500 - This would give a subtotal of 0 and therefore the system won't allow the customer to buy because the subtotal is less than minimum order value.
I am therefore trying to rewrite the minimum order value snippet so it won't set a minimum if a coupon is applied.
After doing some research I found that I should check if coupon is applied by using !empty($woocommerce->cart->applied_coupons). However, this doesn't seem to work. What am I missing ??
// MINIMUM ORDER AMOUNT
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 50;
if ( (WC()->cart->total <= $minimum) && !empty($woocommerce->cart->applied_coupons) ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
}
}
}
You can calculate the order total amount is less than or equal to minimum amount, if it returns true means order total is 500 or less than 500, then you can show the error message.
if ( (WC()->cart->total <= $minimum) && !empty($woocommerce->cart->applied_coupons) ) {
// PRINT ERROR MESSAGE
} else {
// DO YOUR CODE
}
Hope it works!
Maybe somthing like this?
// MINIMUM ORDER AMOUNT
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set variables
$minimum = 500;
global $woocommerce;
if ( (WC()->cart->total <= $minimum) && isset($woocommerce->cart->applied_coupons) ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'NB! Vi sender ikke bestillinger ud under %s, din nuværende total er %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'NB! Vi sender ikke bestillinger ud under %s, din nuværende total er %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error'
);
}
}
}
I want to have a minimum order amount in my WooCommerce store. The following code is perfectly showing a notice if the amount isn't reached but the checkout is still possible. How to disable checkout-button when the minimum amount isn't reached?
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 50;
if ( WC()->cart->total < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
}
}
}
To set a minimum order amount you can use woocommerce_check_cart_items action hook this way:
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
// HERE Set minimum cart total amount
$minimum_amount = 250;
// Total (before taxes and shipping charges)
$cart_subtotal = WC()->cart->subtotal;
// Add an error notice is cart total is less than the minimum required
if( $cart_subtotal < $minimum_amount ) {
// Display an error message
wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($minimum_amount) ) . '</strong>', 'error' );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If customer update the cart changing quantities or removing items, The behavior will be updated too.
Related answer: Woocommerce set minimum order for a specific user role
function disable_checkout_button() {
// Set this variable to specify a minimum order value
$minimum = 50;
$total = WC()->cart->cart_contents_total;
if( $total < $minimum ){
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
echo '<a style="pointer-events: none !important;" href="#" class="checkout-button button alt wc-forward">Proceed to checkout</a>';
}
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button', 1 );
// Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set minimum cart total
$minimum_cart_total = 500;
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->subtotal;
// Compare values and add an error is Cart's total
// happens to be less than the minimum required before checking out.
// Will display a message along the lines of
// A Minimum of USD 500 is required before checking out. (Cont. below)
// Current cart total: USD 6
if( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A minimum order value of %s is required before checking out.</strong>'
.'<br />Your current value is: %s',
wc_price( $minimum_cart_total ),
wc_price( $total ) ),
'error' );
}
}
}
I only permit oders with a minimum value of 15€ on my site, but want to make an exception for one product. I would really appreciated if someone knows how to help me on this. The coding for the minimum order value is below. Anyone know how I can adapt this to exclude one product via a product ID?
add_action( 'woocommerce_check_cart_items', 'wc_set_min_total' );
function wc_set_min_total() {
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Setting the minimum cart total
$minimum_cart_total = 15;
$total = WC()->cart->subtotal;
if( $total <= $minimum_cart_total ) {
wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
.'<br />Current cart\'s total: %s %s',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
}
}
Updated - The following code will avoid checkout under a defined minimal cart amount except for a define product ID:
add_action( 'woocommerce_check_cart_items', 'min_cart_amount' );
function min_cart_amount() {
## ----- Your Settings below ----- ##
$min_amount = 15; // Minimum cart amount
$except_ids = array(37, 53); // Except for this product(s) ID(s)
// Loop though cart items searching for the defined product
foreach( WC()->cart->get_cart() as $cart_item ){
if( in_array( $cart_item['variation_id'], $except_ids )
|| in_array( $cart_item['product_id'], $except_ids )
return; // Exit if the defined product is in cart
}
if( WC()->cart->subtotal < $min_amount ) {
wc_add_notice( sprintf(
__( "<strong>A Minimum of %s is required before checking out.</strong><br>The current cart's total is %s" ),
wc_price( $min_amount ),
wc_price( WC()->cart->subtotal )
), 'error' );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
Question: how to call on all coupons starting with 'nm' that exempt the minimum cart purchase requirement.
I added this code to my functions.php file in wordpress to make it so that a customer has to have a minimum purchase of $15 in their cart before they can complete their purchase.
// SET CART TO MINIMUM ORDER OF 15
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 15;
// No minimum purchase if a specific coupon code is used
if ( WC()->cart->has_discount( 'nm' ) ) {
return;
}
if ( WC()->cart->total < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'You must have an order with a minimum of %s to place
your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'You must have an order with a minimum of %s to place
your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error'
);
}
}
}
However, when I have certain coupon codes for free shipping and/or free product, I don't want the minimum requirement. I added the following in the above code.
// No minimum purchase if a specific coupon code is used
if ( WC()->cart->has_discount( 'nm' ) ) {
return;
}
This works fine if I'm using a coupon called 'nm', but I will have various coupons and I would like it to utilize any coupons that start with 'nm'. How do I do that?
Thanks for your help!