Created
December 18, 2018 14:55
-
-
Save renjith-ph/1465ddfbe2f2101b2aa8de9a788dba5a to your computer and use it in GitHub Desktop.
Show hide accesspoint location based on chosen shipping methods.
This file contains 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
/** | |
* Show hide accesspoint location based on chosen shipping methods. | |
* Created at : 18 Dec 2018 | |
* PluginHive Plugins : https://www.pluginhive.com/plugins/ | |
*/ | |
add_action( 'wp_footer', 'conditionally_hidding_billing_custom_field' ); | |
function conditionally_hidding_billing_custom_field(){ | |
// Only on checkout page | |
if( ! is_checkout() ) return; | |
?> | |
<script> | |
jQuery(function($){ | |
var shipping_method = ['wf_shipping_ups:08']; // array of shipping methods to hide accesspoint location. | |
var sm = 'input[name^="shipping_method"]', | |
smc = sm + ':checked', | |
cbf = '#shipping_accesspoint_field'; | |
// Function that shows or hide imput select fields | |
function showHide( selector = '', action = 'show' ){ | |
if( action == 'show' ) | |
{ | |
$('#shipping_accesspoint').val(''); | |
$(selector).show( 200, function(){ | |
}); | |
} | |
else | |
{ | |
$('#shipping_accesspoint').val(''); | |
$(selector).hide( 200, function(){ | |
}); | |
} | |
} | |
if( shipping_method.indexOf($(smc).val())!='-1' ) | |
showHide( cbf, 'hide' ); | |
$( 'form.checkout' ).on( 'change', sm, function() { | |
if( shipping_method.indexOf($(smc).val())!='-1' ) | |
showHide( cbf, 'hide' ); | |
else | |
showHide( cbf ); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
// define the woocommerce_review_order_before_submit callback | |
function woocommerce_checkout_update_order_meta( $order_id ) { | |
$shipping_methods= ['wf_shipping_ups:08']; // array of shipping methods to hide accesspoint location. | |
$shipping_method=WC()->session->get( 'chosen_shipping_methods' ); | |
if(!empty($shipping_method)) | |
{ | |
$shipping_method=strtolower($shipping_method[0]); | |
if(in_array($shipping_method, $shipping_methods)) | |
{ | |
delete_post_meta($order_id, '_shipping_accesspoint'); | |
} | |
} | |
} | |
// add the action | |
add_action( 'woocommerce_checkout_update_order_meta', 'woocommerce_checkout_update_order_meta', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment