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!
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 the following code to add a notice on the cart page if the order has less than 1kg.
// Add notice on cart when order is less than 1kg
add_action('woocommerce_check_cart_items','check_cart_minweight');
function check_cart_minweight(){
global $woocommerce;
$minweight = $woocommerce->cart->cart_contents_weight;
if( $minweight < 1 ){
wc_add_notice( sprintf( __( 'You have selected %sKg, add more items, orders over 1kg get free shipping', 'woocommerce' ), $minweight ), 'success' );
}
}
The problem is that if the customer does not get over 1kg (many times they do not) and proceeds to checkout, the notice does not clear on checkout. Any advice to prevent this?
There are several options. For example, you can ensure that your code is not executed on the checkout page:
function action_woocommerce_check_cart_items() {
// NOT for checkout page
if ( is_checkout() ) return;
// Get total weight
$total_weight = WC()->cart->get_cart_contents_weight();
// Less
if ( $total_weight < 1 ) {
wc_add_notice( sprintf( __( 'You have selected %sKg, add more items, orders over 1kg get free shipping', 'woocommerce' ), $total_weight ), 'notice' );
}
}
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10 );
Another option is to make sure that the checkout page cannot be reached before the condition is met:
function action_woocommerce_check_cart_items() {
// Get total weight
$total_weight = WC()->cart->get_cart_contents_weight();
// Less
if ( $total_weight < 1 ) {
wc_add_notice( sprintf( __( 'You have selected %sKg, add more items, orders over 1kg get free shipping', 'woocommerce' ), $total_weight ), 'error' );
// Remove proceed to checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10 );
There is a function that does this here: https://wp-kama.com/plugin/woocommerce/function/wc_clear_notices
<?php wc_clear_notices(); ?>
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'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