Skip to content

Instantly share code, notes, and snippets.

@yanknudtskov
Created April 22, 2018 17:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yanknudtskov/85ca39d75454f7ba4adf6b47e9168472 to your computer and use it in GitHub Desktop.
Save yanknudtskov/85ca39d75454f7ba4adf6b47e9168472 to your computer and use it in GitHub Desktop.
Add a handling fee for CVR Payment Gateway
<?php
if ( class_exists( 'WooCommerce' ) ) {
add_action( 'woocommerce_cart_calculate_fees', 'yanco_cvr_calculate_totals' );
function yanco_cvr_calculate_totals( ) {
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$current_gateway = '';
$fee_title = __('CVR Handling Fee', 'woocommerce-cvr-payment-gateway'); // Change the title to fit your needs
$cvr_handling_fee = 50; // Change the value to the amount you want to charge
$fee_tax_class = 'zero rate'; // Change this to the tax class you wish to use for the fee
if ( ! empty( $available_gateways ) ) {
// Chosen Method
if ( isset( WC()->session->chosen_payment_method ) && isset( $available_gateways[ WC()->session->chosen_payment_method ] ) ) {
$current_gateway = $available_gateways[ WC()->session->chosen_payment_method ];
} elseif ( isset( $available_gateways[ get_option( 'woocommerce_default_gateway' ) ] ) ) {
$current_gateway = $available_gateways[ get_option( 'woocommerce_default_gateway' ) ];
} else {
$current_gateway = current( $available_gateways );
}
}
if ( $current_gateway->id == 'yanco_wc_cvr_payment_gateway' ) {
WC()->cart->add_fee( $fee_title, $cvr_handling_fee, true, $fee_tax_class );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment