This issue is going to make me go postal pretty soon....
In Woocommerce Checkout , I needed to add a Custom field in the address.
This extra field is for function calculate_shipping( $package = array())
Now Clearly woocommerce / wordpress don't allow access to these custom fields within this function call.. don't understand why but we move along..
So the solution would be jQuery right? .. not so fast .. Every single example does not return the value back into the woocommerce environment .. not one ..
So on to the code I have so far down this rabbit hole for a single field value......
//We first add the field to the checkout form
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields');
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_area'] = array(
'label' => __('Area', 'woocommerce'),
'placeholder' => _x('Area', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide' ),
'clear' => true,
'priority' => 61
);
return $fields;
}
// we load the scripts and the ajax callback functions
add_action( 'wp_enqueue_scripts', 'so18550905_enqueue_scripts' );
add_action( 'wp_ajax_myaction', 'so18550905_wp_ajax_function' );
add_action( 'wp_ajax_nopriv_myaction' , 'so18550905_wp_ajax_function' );
function so18550905_enqueue_scripts(){
wp_register_script( 'ajaxHandle', plugins_url() . '/miguel-shipping/test.js', array('jquery'), '1.0', true );
wp_enqueue_script( 'ajaxHandle' );
wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin_ajax.php' ) ) );
}
function so18550905_wp_ajax_function(){
//this function should be called but it is not for some reason..
$testingitouthere=$_POST['my_billing_area'] ;
file_put_contents('blablablabla.txt', print_r($testingitouthere,true));
wp_die();
}
The JS Code:
jQuery(document).ready( function($){
$('#billing_area').change(function () {
var billing_area = $('#billing_area').val();
console.log(billing_area);
$.ajax({
url: ajax_object.ajaxurl, // this is the object instantiated in wp_localize_script function
type: 'POST',
data:{
action: 'myaction',
my_billing_area: billing_area
},
success: function( data ){
//Do something with the result from server
console.log( data );
}
});
return false;
});
});
The problem is that the function in the php called
so18550905_wp_ajax_function()
just never gets triggered.. I'm writing to file in that function (before someone asks) solely for testing only .. once I get to that point i'll carry on coding from there... The Jquery is logging to console aswell..
Try the following revisited code is based on your provided code that will add in a custom WC_Session the value of the Billing Area field through Ajax.
The code:
Code goes in function.php file of your active child theme (active theme). Tested and works.
Similar or related threads: