I am trying to set a max purchase total in my WooCommerce cart. (Background: I want to prohibit people, who use 100% coupons in this case, to add more products to their cart.)
This is the code I adapted from here :
// Weitere Einkäufe verhindern
add_action( 'woocommerce_check_cart_items', 'cldws_set_max_total');
function cldws_set_max_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set maximum cart total
$max_cart_total = 1;
// A maximum of 1 € is required before checking out.
$total = WC()->cart->totals;
// Compare values and add an error is Cart's total
if( $total >= $max_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>Bitte lege nicht mehr Stränge in den Warenkorb als du beim Crowdfunding bestellt hast.</strong> Die genaue Anzahl findest du in der E-Mail mit dem Gutscheincode. '),
'error' );
}
}
}
Here is what it should do:
After checking that this only runs on the cart and checkout pages and setting the max. totals value to 1 €, I query the cart's totals and compare them to this max value. If the total is bigger than the max. allowed total, it displays the error message.
I suspect that I am querying the wrong variable with "totals", however that one should be the variable that is the total cart value after discounts. I've also tried "get_cart_total()", but that didn't help. I've also looked at this, but don't get why replacing WC() for $woocommerce should help.
Any help is greatly appreciated, thanks!
Turns out replacing
WC()->cart->get_cart_total();
with
$woocommerce->cart->total;
actually does the trick. But I don't understand why. :-D
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.