Skip to content

Instantly share code, notes, and snippets.

@Basilakis
Last active March 27, 2018 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Basilakis/edacbd2c8d327fa569c480d33a74870a to your computer and use it in GitHub Desktop.
Save Basilakis/edacbd2c8d327fa569c480d33a74870a to your computer and use it in GitHub Desktop.
Dynamic Fees for Shipping Per Payment GateWay for WooCommerce
add_action('init', 'cg_init');
function cg_init() {
add_action('woocommerce_cart_calculate_fees', 'cg_add_fee');
add_action('wp_footer', 'cg_footer', 9999);
}
function cg_footer() {
?>
<script type="text/javascript">
jQuery(function($) {
setInterval(function() {
$(".input-radio[name='payment_method']").off().change(function() {
console.log('triggered');
jQuery('body').trigger('update_checkout');
});
}, 500);
});
</script>
<?php
}
function cg_add_fee($the_cart) {
global $woocommerce;
$amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
if ( $amount < 50 && $woocommerce->customer->get_billing_country() == 'GR' ) {
if ($woocommerce->session->chosen_payment_method == 'cod') {
$woocommerce->cart->add_fee('Shipping Cost', '5', true, 'standard');
} else {
$woocommerce->cart->add_fee('Shipping Cost', '3', true, 'standard');
}
} else {
$woocommerce->cart->add_fee('Shipping Cost - Free Shipping', '0', true, 'standard');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment