Allow WooCommerce Gateway Percentage Fee To Work Also On Order Pay
I've been using this snippet for a while with no issue until I noticed that a customer was able to pay by credit card without being charged a fee. They managed to make a payment via the "order-pay" page.
Can anyone tell me why my code isn't working on the order-pay page? It works fine on the checkout page.
// Assign Credit Card Gateway Percentage Fee to Wholesaler Profiles
add_action('woocommerce_cart_calculate_fees', 'sm_credit_card_fee_role_gateway', 10, 1);
function sm_credit_card_fee_role_gateway($cart){
if (is_admin() && !defined('DOING_AJAX'))
return;
if (!(is_checkout() && !is_wc_endpoint_url()))
return;
if (!is_user_logged_in())
return;
$user = wp_get_current_user();
$roles = (array) $user->roles;
$roles_to_check = array('administrator', 'default_wholesaler', 'wholesaler-non-vat-registered', 'shop_manager');
$compare = array_diff($roles, $roles_to_check);
if (empty($compare)){
$payment_method = WC()->session->get('chosen_payment_method');
if ($payment_method == 'cardgatecreditcard'){
$percentage = 0.085;
$surcharge = (WC()->cart->cart_contents_total + WC()->cart->shipping_total) * $percentage;
$cart->add_fee( 'Credit Card Fee (8.5%)', $surcharge, true );
}
}
}
Answer
To allow your code to be active on Order pay, you may replace in your code:
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return;
by:
if ( ! ( is_checkout() && ! is_wc_endpoint_url('order-received') ) )
return;
Now your function code will also be executed on WooCommerce Order Pay endpoint…
But in Order Pay endpoint, the data is saved to an existing order, so the fee is already saved for this order when order was placed in checkout. The hook
woocommerce_cart_calculate_fees
affect only the cart object and doesn't have any effect on an exiting order in Order Pay endpoint.You will have to rebuilt something much more complicated specifically for Order Pay, involving certainly Ajax, to be able to remove or add your custom fee in the existing order, refreshing totals…
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?