<?php
/* Increase cart item prices based on payment method in WooCommerce BEGIN */
add_action( 'woocommerce_cart_calculate_fees','ts_add_discount', 20, 1 );
function ts_add_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$label_text=__("");
$service_charge=0;
// Mention the payment method e.g. cod, bacs, cheque or paypal
$cart_total = $cart_object->subtotal_ex_tax;
$chosen_payment_method = WC()->session->get('chosen_payment_method'); //Get the selected payment method
if( $chosen_payment_method == "paypal" ){
$label_text = __( "PayPal Service Charge" );
// The service charge to apply
$service_charge=200;
}
else if( $chosen_payment_method == "cod"){
$label_text = __( "Credit Card Service Charge" );
// The service charge to apply
/* $service_charge=3.5; */
$service_charge = (($cart_total / .965) - $cart_total); /* This is dividing to get a 3.5 percent of the price as a fee */
}
else if( $chosen_payment_method == "bacs"){
$label_text = __( "Direct Bank Transfer Service Charge" );
$service_charge=100;
}
else if( $chosen_payment_method == "cheque"){
$label_text = __( "Cheque Payment Service Charge" );
$service_charge=150;
}
else {
$label_text = __( "Credit Card Fee" );
$service_charge=0;
}
// Adding the service charge
$cart_object->add_fee( $label_text, $service_charge, false );
}
add_action( 'woocommerce_review_order_before_payment', 'ts_refresh_payment_method' );
function ts_refresh_payment_method(){
// jQuery
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
/* Increase cart item prices based on payment method in WooCommerce END */
Increase cart item prices based on payment method in WooCommerce.
This function increases the cart item prices based on the payment method enabled in WooCommerce.
Need help with this?
Hire me for a consultation and I'll walk you thru the process.
0 Comments