Last active
May 29, 2023 08:54
-
-
Save Asikur22/46456dd566d19c3c685d00590213d7da to your computer and use it in GitHub Desktop.
Remove Shipping Method/Rate based on Checkout Fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'woocommerce_package_rates', function ( $rates ) { | |
if ( isset( $_POST['post_data'] ) ) { | |
parse_str( $_POST['post_data'], $post_data ); | |
if ( isset( $post_data['billing_custom_field'] ) ) { | |
switch ( $post_data['billing_custom_field'] ) { | |
case 'Jeg_henter_selv': | |
unset( $rates['local_pickup:5'] ); | |
break; | |
case 'Levering_til_adresse': | |
unset( $rates['local_pickup:10'] ); | |
break; | |
} | |
} | |
} | |
return $rates; | |
}, 99 ); | |
add_action( 'woocommerce_checkout_update_order_review', function () { | |
foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ) { | |
WC()->session->set( 'shipping_for_package_' . $package_key, true ); | |
} | |
WC()->cart->calculate_shipping(); | |
} ); | |
add_action( 'wp_footer', function () { | |
if ( is_checkout() ) { | |
?> | |
<script> | |
jQuery( document ).ready( function ( $ ) { | |
$( 'input[name="billing_custom_field"]' ).on( 'change', function () { | |
$( 'body' ).trigger( 'update_checkout' ); | |
} ); | |
} ); | |
</script> | |
<?php | |
} | |
}, 99 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment