![](/img/trans.png)
[英]Set WooCommerce billing postcode from product custom input field for guests
[英]Set postcode from a field for guests and customers in WooCommerce
主要思想是驗證客人的郵政編碼並根據它顯示不同的消息。 我使用此線程設置運輸代碼: Set shipping postcode early before add to cart in WooCommerce
我構建了一個有效的 AJAX 請求,該請求采用登錄頁面中的輸入值。
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");
}
});
})
});
然后將該值傳遞給 PHP function 應該將postcode
值添加到客人 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();
}
我還構建了一個簡碼 function 來顯示客人的 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' );
問題是得到 AJAX 響應的 PHP function 似乎沒有將postcode
設置為客人的 session customer_data
。 我試過直接在短代碼中設置postcode
(使用相同的方法)並且它有效。
你能幫我弄清楚問題出在哪里嗎? 我也很難調試 - 我怎么知道 session customer_data
已經改變了?
謝謝你。
編輯:我已經將客人的 session customer data
發送到 AJAX 響應,我得到了這個:
array(26) { ["id"]=> string(1) "0" ... ["postcode"]=> int(44500) ... }
這意味着數據是在 AJAX 響應之后存儲的。 當我重新加載頁面並嘗試再次獲取客人的 session customer data
時,問題似乎沒有存儲此數據。
您需要在WC_Customer
Object 上使用可用的 setter 和 getter 方法,例如:
WC()->customer->get_billing_postcode()
或WC()->customer->get_shipping_postcode()
WC()->customer->set_billing_postcode()
或WC()->customer->set_shipping_postcode()
現在你的代碼中有一些錯誤。 我已經重新訪問了您的所有代碼,如下所示:
// 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 );
}
}
// Shortcode
add_shortcode( 'postcode-field', 'shortcode_postcode_field' );
function shortcode_postcode_field(){
return '<p class="form-row postcode-field on" id="postcode-field_field" data-priority="">
<label for="postcode-field" class="">' . __("Postcode", "woocommerce") . '
<span class="optional">(optional)</span>
</label>
<span class="woocommerce-input-wrapper">
<input type="number" class="input-text" name="postcode-input" id="postcode-input" placeholder="85000" value="">
</span>
<button id="postcode-submit" name="postcode-submit" class="button alt">' . __("Check your postcode", "woocommerce") . '</button>
<br><div class="postcode-message" style="display:none"></div>
</p>';
}
// Jquery (Ajax sender)
add_action( 'wp_footer', 'postcode_field_js_script' );
function postcode_field_js_script() {
?>
<script type="text/javascript">
jQuery( function($) {
var postcode = '';
$('#postcode-input').on("input change", function () {
postcode = $(this).val();
});
$("#postcode-submit").on('click', function () {
$.ajax({
type: 'POST',
url: '<?php echo admin_url('/admin-ajax.php'); ?>',
data: {
'action': 'postcode_receiver',
'postcode': postcode
},
success: function (response) {
$('.postcode-message').html(response).show(300);
// console.log(response);
},
error: function (error) {
$('.postcode-message').html(error).show(300);
// console.log(error);
}
});
});
});
</script>
<?php
}
// Php (Ajax receiver) - Check and set the postcode - return message (notice)
add_action('wp_ajax_postcode_receiver', 'postcode_receiver');
add_action('wp_ajax_nopriv_postcode_receiver', 'postcode_receiver' );
function postcode_receiver(){
if( isset($_POST['postcode']) ) {
$postcode = sanitize_text_field($_POST['postcode']);
if ( $postcode > 0 ) {
WC()->customer->set_shipping_postcode($postcode);
WC()->customer->set_billing_postcode($postcode);
$saved_postcode = WC()->customer->get_shipping_postcode();
echo sprintf( '<span style="color:green;">' . __("Your postcode %s has been registered successfully.", "woocommerce") . '</span>', '"' . $saved_postcode . '"' );
} else {
echo '<span style="color:red;">' . __("Check your postcode input please.", "woocommerce") . '</span>';
}
wp_die();
} else {
echo '<span style="color:red;">' . __("A problem has occurred, try later.", "woocommerce") . '</span>';
wp_die();
}
}
代碼進入活動子主題(或活動主題)的 functions.php 文件。 測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.