A win-win situation for store owners and customers arises only when the store can efficiently manage costs while providing fair and affordable pricing.
When a store is providing nationwide shipping and delivery, shipping charges may vary. Calculating state-specific shipping prices, customers will appreciate not being overcharged or undercharged for shipping.
This post will help you learn how to add state-specific and payment gateway-based additional fees in WooCommerce to address common issues like overcharging for shipping or lack of transparency in the checkout fees.
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 & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.
Preliminary Steps
1. Set up your desired shipping zone and the shipping methods. To set up navigate to WooCommerce->Settings->Shipping->Select a Shipping zone->Edit ->Add shipping region(s)
2. Choose the payment gateway to which you want to add the State Delivery Fee.
Solution: WooCommerce Checkout fees based on payment gateway & Specific States
Consider an online clothing store that sells trendy apparels and accessories, catering to customers from all over the United States and offering shipping to nearly 50 states. In such a scenario, it becomes impractical for the store owner to apply a standard shipping rate to all states. Instead, the store owner needs to configure specific states for which they want to add an extra charge in their WooCommerce settings, as demonstrated below.
The provided code applies a $2 delivery fee when the shipping state is set to ‘New York’ or ‘California,’ and the payment method ‘Cash on Delivery’ is chosen.
add_action( 'woocommerce_cart_calculate_fees', 'ts_add_checkout_fee_for_states_and_cod' ); function ts_add_checkout_fee_for_states_and_cod() { $chosen_gateway = WC()->session->get( 'chosen_payment_method' ); $shipping_state = WC()->customer->get_shipping_state(); // Add fee for specific payment gateway (e.g., 'your_chosen_payment_gateway') and states (New York and California) // Also, add fee for Cash on Delivery (COD) payment method if ( ( $chosen_gateway === 'your_chosen_payment_gateway' || $chosen_gateway === 'cod' ) && ( $shipping_state === 'NY' || $shipping_state === 'CA' ) ) { WC()->cart->add_fee( 'State Delivery Fee', 2 ); } } add_action( 'woocommerce_after_checkout_form', 'ts_refresh_checkout_on_payment_methods_change' ); function ts_refresh_checkout_on_payment_methods_change() { wc_enqueue_js( " $( 'form.checkout' ).on( 'change', 'input[name^=\'payment_method\']', function() { $('body').trigger('update_checkout'); }); "); }
Code Output
Scenario 1
Based on the scenario 1 & 2 images below, it is evident that the order is shipped to California and New York, and the customer has selected the Cash On Delivery (COD) payment gateway. Since the condition in the code is satisfied, the State Delivery Fee of $2 is added to the order subtotal.
Scenario 2
Scenario 3
In the following output, the order is shipped to New York. Despite meeting our predefined state region conditions, the State Delivery Fee of $2 is not applied as it does not meet the condition of the chosen payment method.
Scenario 4
Let’s assume that the order is shipped to a state that is not configured in our code. In the image below, the customer sets the state of Alaska as the shipping location and the payment method chosen is Cash On Delivery. It is because only one of our conditions is met and the extra charge is not added. Also, the customer gets an error message in the shipping field.
Code Explanation
- The code begins with an action hook using the woocommerce_cart_calculate_fees, and it’s set to trigger the function ts_add_checkout_fee_for_states_and_cod() during the cart fee calculation process in the WooCommerce store.
- The function ts_add_checkout_fee_for_states_and_cod() is defined and is responsible for adding an extra fee to the cart based on specific conditions.
- $chosen_gateway: This variable stores the payment method (gateway) the customer has chosen for the order. It retrieves this information from the WooCommerce session, which keeps track of the customer’s selections.
- $shipping_state: This variable stores the shipping state of the customer’s delivery address. It is obtained from the WooCommerce customer object, which holds details about the customer’s profile.
- The code checks if the customer has chosen a specific payment gateway (payment method) by comparing the value of $chosen_gateway with ‘your_chosen_payment_gateway’.
- The code also checks if the customer has chosen the “Cash on Delivery” (COD) payment method. This is done by using the logical OR operator (||) to combine two conditions: one for the specific gateway and the other for the COD method.
- Similarly, the code checks if the customer’s shipping state is either ‘NY’ (New York) or ‘CA’ (California) using the logical OR operator.
- If both conditions are true, meaning the customer has chosen either the specific payment gateway or the COD method AND their shipping state is either New York or California, the code executes the block inside the if statement.
- Finally, we use the WC()->cart->add_fee() method to add an extra fee to the cart. The fee is labeled as “State Delivery Fee” and its amount is $2.
- The woocommerce_after_checkout_form action ensures that when customers change their payment method selection during checkout, the function ts_refresh_checkout_on_payment_methods_change will be triggered.
- As a next step, the ts_refresh_checkout_on_payment_methods_change function is defined to refresh the checkout form when the payment method is changed.
- It uses JavaScript (jQuery) to listen for changes in the payment method input and trigger the update_checkout event, which refreshes the checkout form dynamically.
Conclusion
Implementing the above code adds a dynamic checkout fee of $2 for customers who select specific payment gateways (Cash on Delivery here) and specific shipping states (New York (NY) or California (CA)). With slight modifications to the code, you can also apply a percentage-based fee instead of a fixed amount.