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(); ?>
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 have some code to check amount of products in WooCommerce is divisible by 6 - however even though a notice is displayed the customer can still checkout with incorrect quantities.
How can I disable WooCommerce 'Place Order' checkout if the condition is not met?
// check that cart items quantities totals are in multiples of 6
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
global $woocommerce;
$multiples = 6;
$total_products = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$total_products += $values['quantity'];
}
if ( ( $total_products % $multiples ) > 0 )
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ) );
}
The hook woocommerce_check_cart_items needs an "error" notice to disable checkout (which was not the case). I have also simplified the code:
// Check cart items conditionally displaying an error notice and avoiding checkout
add_action( 'woocommerce_check_cart_items', 'check_cart_items_conditionally' );
function check_cart_items_conditionally() {
$multiple_of = 6; // <= Here set the "multiple of" number
if ( ( WC()->cart->get_cart_contents_count() % $multiple_of ) != 0 ) {
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiple_of ), 'error' );
}
}
Optional: To be sure that checkout is not accessible you can add the following that will redirect checkout to cart page if cart items total count is not a multiples of N:
// To be sure: redirect checkout to cart page if cart items count is not a multiples of N
add_action( 'template_redirect', 'redirect_checkout_to_cart_conditionally' );
function redirect_checkout_to_cart_conditionally() {
$multiple_of = 6; // <= Here set the "multiple of" number
if ( is_checkout() && ( WC()->cart->get_cart_contents_count() % $multiple_of ) != 0 ) {
wp_redirect( wc_get_cart_url() );
exit();
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Suppose someone adds 2 products to cart
The first product consists of 3 quantities
The following product consists of 2 quantities
The whole quantity is therefore equal to 5. However, I want to limit this to 4
I use the following for this, which seems to work for 1 particular product, but not for the combination of multiple products.
// Checking and validating when updating cart item quantities when products are added to cart
add_filter( 'woocommerce_update_cart_validation', 'only_six_items_allowed_cart_update', 10, 4 );
function only_six_items_allowed_cart_update( $passed, $cart_item_key, $values, $updated_quantity ) {
$cart_items_count = WC()->cart->get_cart_contents_count();
$original_quantity = $values['quantity'];
$total_count = $cart_items_count - $original_quantity + $updated_quantity;
if( $cart_items_count > 4 || $total_count > 4 ){
// Set to false
$passed = false;
// Display a message
wc_add_notice( __( "You can’t have more than 6 items in cart", "woocommerce" ), "error" );
}
return $passed;
}
Can someone walk me through how to do that?
To check the number of items during the checkout process you can use the woocommerce_check_cart_items action hook.
To remove the "proceed to checkout" button you can use remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
Processed into a complete answer, you get: (Explanation via comment tags added in the code)
function action_woocommerce_check_cart_items() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
// Max allowed items
$max_allowed = 4;
// Get number of items in the cart.
$items_in_cart = WC()->cart->get_cart_contents_count();
// If items in cart greater than allowed amounts, show error message
if ( $items_in_cart > $max_allowed ) {
// Notice
wc_add_notice( sprintf( __( 'Allowed amount is %s, You have %s items in the shopping cart', 'woocommerce' ), $max_allowed, $items_in_cart ), '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, 0 );
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' );
}
}
}
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!