The main idea is to validate a guest's postcode and show different messages according to it. I've used this thread to set the shipping code: Set shipping postcode early before add to cart in WooCommerce
I've built a working AJAX request that takes the value of an input in the landing page.
jQuery(document).ready(function ($) {
let postcodeField = jQuery("#postcode-field");
let postcodeVal;
postcodeField.on("change", function () {
postcodeVal = postcodeField.val();
});
jQuery("#ph_btn").on("click", function () {
var data = {
action: 'postcode_handler',
postcode: postcodeVal
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
// If you need it on a public facing page, uncomment the following line:
var ajaxurl = ph_script.ajax_url;
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: data,
success: function (result) {
// console.log(result);
},
error: function () {
console.log("error");
}
});
})
});
The value is then passed to a PHP function that is supposed to add the postcode value to the guest session customer_data.
function my_AJAX_processing_function(){
// Getting the postcode value
$postcode = intval($_POST['postcode'] );
//Check if the input was a valid integer
if ( $postcode == 0 ) {
echo "Invalid Input";
wp_die();
}
//Important: Early enable customer WC_Session
add_action( 'init', 'wc_session_enabler' );
function wc_session_enabler() {
if ( ! is_admin() && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
}
// Get an array of the current customer data stored in WC session
$customer_data = (array) WC()->session->get('customer');
// Change the billing postcode
$customer_data['postcode'] = $postcode;
// Change the shipping postcode
$customer_data['shipping_postcode'] = $postcode;
// Save the array of customer WC session data
WC()->session->set('customer', $customer_data);
// Sending a response to the AJAX request
echo($postcode);
wp_die();
}
I've also built a shortcode function to show the guest's session customer_data.
function shortcode_postcode_field(){
// Getting the customer data from the session
$customer_data = (array) WC()->session->get('customer');
// Get the billing postcode
// if ( isset( $customer_data['postcode'] ) )
$postcode = $customer_data['postcode'];
// Showing the customer data for debug reasons
var_dump($customer_data);
return '
<p class="form-row postcode-field on" id="postcode-field_field" data-priority="">
<label for="postcode-field" class="">Code postal
<span class="optional">(facultatif)</span>
</label>
<span class="woocommerce-input-wrapper">
<input type="number" class="input-text" name="postcode" id="postcode-field" placeholder="85000" value="">
</span>
<button id="ph_btn" style="color: black">Vérifier son code postal</button>
<p>Votre code postal est '.$postcode.'</p>
</p>
';
}
add_shortcode( 'postcode-field', 'shortcode_postcode_field' );
The issue is that the PHP function that get's the AJAX response doesn't seem to set the postcode to the guest's session customer_data. I've tried setting the postcode directly in the shortcode (with the same method) and it works.
Could you help me figure out where the problem lies ?
I also have got difficulties to debug - how can I tell that the session customer_data has been changed ?
Thank you.
EDIT: I've sent the guest's session customer data to the AJAX response and I got this:
array(26) { ["id"]=> string(1) "0" ... ["postcode"]=> int(44500) ... }
This means that the data is stored just after AJAX response. The issue seems that this data isn't stored when I reload the page and try to get the guest's session customer data again.