Skip to content

Instantly share code, notes, and snippets.

@waqashassan98
Created May 14, 2019 11:30
Show Gist options
  • Save waqashassan98/8a32875e39b65cb79a3bca429e1e71bf to your computer and use it in GitHub Desktop.
Save waqashassan98/8a32875e39b65cb79a3bca429e1e71bf to your computer and use it in GitHub Desktop.
Adding Checkbox field to WooCommerce checkout to update billing fields
<?php
/**
* This function assumes that "fedex_is_insured" filter is enabled
* in WooCommerce Fedex Plugin. This was manually added by me by altering the Fedex plugin code.
* $this->insure_contents = apply_filters( 'fedex_is_insured', $this->insure_contents);
*/
function nb_update_insurance_for_fedex(){
$insurance = WC()->session->get( 'nb_insurance' );
if ( "1" == $insurance ) {
return true;
}
return false;
}
add_filter( "fedex_is_insured", "nb_update_insurance_for_fedex", 10);
/**
* Add Insurance Status in summary Tables
*/
// add_action( 'woocommerce_cart_totals_before_shipping', 'nb_display_cart_insurance_status', 20 );
add_action( 'woocommerce_review_order_before_shipping', 'nb_display_cart_insurance_status', 20 );
function nb_display_cart_insurance_status() {
$chosen = WC()->session->get('nb_insurance');
$chosen = ("1" == $chosen) ? "Insurance Added" : "Insurance Not Added";
echo ' <tr class="insurance-option">
<th>' . __( "Insurance" ) .'</th>
<td data-title="total-volume">'.$chosen.'</td>
</tr>';
}
/**
* Display Insurance selector checkbox on checkout page
*/
function nb_insurance_checkbox() {
$chosen = WC()->session->get('nb_insurance');
$chosen = ("1" == $chosen) ? true : false;
echo '<div id="checkout-insurance">';
woocommerce_form_field( 'nb_insurance', array(
'type' => 'checkbox',
'label' => __('Add Insurance.'),
'required' => false,
), $chosen);
echo '<p style="padding-left: 17px; font-weight: normal; font-style: italic;">Incredible Underwater LED Lighting, INC is not responsible for any purchase lost/damaged during shipping.</p>';
echo '</div>';
}
add_action( 'woocommerce_after_order_notes', 'nb_insurance_checkbox' );
/**
* Add Javascript for Insurance selector checkbox for pricing refresh
*/
function nb_insurance_cb_cart_refresh() {
if ( ! is_checkout() ) return;
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'input[name=nb_insurance]', function(e){
e.preventDefault();
var p = "false";
if($(this).prop("checked") == true){
p = "true";
}
jQuery.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'woo_get_ajax_data',
'nb_insurance': p,
},
success: function (result) {
$('body').trigger('update_checkout');
}
});
});
});
</script>
<?php
}
add_action( 'wp_footer', 'nb_insurance_cb_cart_refresh' );
/**
* Save Insurance Setting in Server
*/
function nb_insurance_cb_set_session() {
$cb = esc_attr($_POST['nb_insurance']);
if ("true" == $cb ){
WC()->session->set('nb_insurance', "1" );
}
else{
WC()->session->set('nb_insurance', "0" );
}
echo json_encode( WC()->session->get('nb_insurance'));
die();
}
add_action( 'wp_ajax_woo_get_ajax_data', 'nb_insurance_cb_set_session' );
add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'nb_insurance_cb_set_session' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment