When running an online WooCommerce store, you may often want to give a special discount to some users or charge a fee only to some users. WordPress offers to separate different types of users via User roles. WooCommerce extends these roles & adds a few more roles that are needed for the smooth functioning of your online store.
This post allows you to set up your store such that you can charge a fee when users from a user role chose a particular payment method.
In today’s competitive market, customers seek more than just a standard shopping experience. They crave personalization and expect businesses to recognize their loyalty. By providing personalized flat or percentage-based discounts for users based on their user roles will help you meet customer expectations.
Where to Add Custom Code in WooCommerce?
It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install the Code Snippets plugin. You can then add the code as a new snippet via the plugin.
Solution: Adding Fees on the WooCommerce Checkout page based on Payment Gateway & User Roles
Consider a use case: as a store owner you want to charge extra fees based on the payment gateway and user role. By utilizing this code snippet, you can add fees specifically for the “customer” role who chooses the “Direct bank transfer” option during their purchase.
In this use case, the code snippet adds a $10 fixed fee surcharge and a 5% based fee surcharge on the order subtotal for the payment mode “Direct bank transfer,” and for the user role “customer,” as shown below.
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee' ); function add_custom_fee( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; ## SETTINGS: $user_roles = array( 'customer' ); // Defined user roles // Targeted payment method Ids $payment_ids = array('bacs'); $fixed_fee = 10; // Fixed Fee amount $percentage = 5; // Fee percentage $user = wp_get_current_user(); // Get current WP_User Object $chosen_payment = WC()->session->get('chosen_payment_method'); // Choosen payment method $cart_subtotal = $cart->subtotal; // Including taxes - to exclude taxes use $cart->get_subtotal() // For matched payment Ids and user roles if ( in_array( $chosen_payment, $payment_ids ) && array_intersect( $user->roles, $user_roles ) ) { $cart->add_fee( __('Fixed Fee'), $fixed_fee, true ); // Add fixed fee (incl taxes) } // For matched payment Ids and user roles if ( in_array( $chosen_payment, $payment_ids ) && array_intersect( $user->roles, $user_roles ) ) { $fee_cost = $percentage * $cart_subtotal / 100; // Calculation $cart->add_fee( __('Payment Fee (5%)'), $fee_cost, true ); // Add fee (incl taxes) } } add_action( 'wp_footer', 'custom_checkout_jquery_script' ); function custom_checkout_jquery_script() { if ( is_checkout() && ! is_wc_endpoint_url() ) : ?> <script type="text/javascript"> jQuery( function($){ $('form.checkout').on('change', 'input[name="payment_method"]', function(){ $(document.body).trigger('update_checkout'); }); }); </script> <?php endif; }
Output:
When an existing user with the role ‘customer’ selects the product and payment method option as “Direct bank transfer”, it will check based on these two conditions and calculate and display the additional fixed fee of $10 and the based fee of $5 for the T-shirt.
Code Explanation:
- The code adds an action hook called woocommerce_cart_calculate_fees and associates it with the function ts_add_custom_fee. This means that when WooCommerce calculates the fees for the cart, it will execute the function to add the custom fee.
- This function starts with one conditional check. It check ensures that the fee is not added in the admin area and not during AJAX requests.
- Next, the function defines some settings:
- $user_roles: An array of user roles for which the custom fee should be added. In this case, it includes ‘customer’.
- $payment_ids: An array of targeted payment method IDs for which the fee will be applied. In this case, it includes the ‘bacs’ payment method, which usually refers to bank transfers.
- $fixed_fee: This variable holds the fixed fee amount. In the provided code, it is set to 10, which means a fixed fee of 10 currency units will be added if the conditions are met.
- $percentage: The percentage value that will be used to calculate the fee. It’s set to 5%, meaning the fee will be 5% of the cart subtotal.
- The function then gets the current user object using wp_get_current_user() and the chosen payment method from the WooCommerce session using WC()->session->get(‘chosen_payment_method’).
- The subtotal of the cart (including taxes) is retrieved using $cart->subtotal.
- If the chosen payment method is in the targeted payment IDs list and the user has one of the specified user roles, the custom fee will be added. The fee amount is calculated as a percentage and fixed fee of the cart subtotal.
- The function checks if the chosen payment method is in the $payment_ids array and if the user’s role matches any of the roles specified in $user_roles. If both conditions are met, it adds a fixed fee to the cart using the add_fee method with the label ‘Fixed Fee‘ and the amount specified in $fixed_fee.
- The fee is added to the cart using the add_fee method of the cart object. The title of the fee is set to ‘Payment Fee (5%)‘, and the amount is $fee_cost (the calculated fee), and true is passed to indicate that taxes are included in the fee amount.
- The second action hook wp_footer is added, and it’s associated with the function custom_checkout_jquery_script. This function adds a jQuery script to the footer of the checkout page to trigger an update of the checkout whenever the selected payment method changes.
- The jQuery script listens for changes in the input element named “payment_method” within the checkout form. When the payment method is changed, the script triggers the update_checkout event, which forces WooCommerce to update the cart and apply any relevant fees.
Conclusion
There can be cases where you want to provide a discount to certain roles, for example, a wholesaler. In that case, you can supply a negative value to the $percentage variable in the above code snippet.
Let us know in the comments if the above code snippet works for you as well as if you want to see more use cases being covered for such instances.