Woocommerce: Automatically add coupon code for wholesale roles

    
//black friday add_action('woocommerce_before_cart', 'apply_coupon_for_wholesalers'); function apply_coupon_for_wholesalers() { if (is_admin() && !defined('DOING_AJAX')) { return; } // Define the coupon code and user role $coupon_code = 'WHOLESALEBLACKF'; $user = wp_get_current_user(); $allowed_role = 'default_wholesaler'; // Check if the user has the specific role and coupon is not already applied if (in_array($allowed_role, $user->roles) && !WC()->cart->has_discount($coupon_code)) { WC()->cart->add_discount($coupon_code); wc_print_notices(); // Display any notices related to the coupon } }

Leave a Reply