I’m using a snippet code from Woocommerce Minimum Order Amount to set a minimum order total. But I would like to set different minimums per user role.
I have some custom user roles: wholesale_prices
, wholesale_vat_exc
, and distributor_prices
. I want to make the code to work based on use roles with different minimum amounts for each role.
Here is my code:
// Minimum order total add_action( 'woocommerce_check_cart_items', 'wc_minimum_order_amount' ); function wc_minimum_order_amount() { // Set this variable to specify a minimum order value $minimum = 300; 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' ); } }
Advertisement
Answer
Using WordPress conditional function current_user_can()
like:
add_action( 'woocommerce_check_cart_items', 'wc_minimum_order_amount' ); function wc_minimum_order_amount() { // minimum order value by user role if ( current_user_can('distributor_prices') ) $minimum = 3000; elseif ( current_user_can('wholesale_prices') ) $minimum = 1000; elseif ( current_user_can('wholesale_vat_exc') ) $minimum = 600; else $minimum = 300; // default 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' ); } } }
Code goes on functions.php file of your active child theme (or active theme). It should works.