我想在主頁上彈出一個簡單的模型,讓用戶在結帳/購物車頁面之前選擇他們的運送國家。更新客戶Woocommerce結賬前的地址頁面
我做了一個小的選擇框彈出式模型,然後用AJAX運行這個函數來改變用戶的運送國家,但它沒有被更新。
//本地化
//*********** Ajax Change shipping country on press ***********/
function localize_array() {
$localize = array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('yo-ajax-call-ak'),
);
return $localize;
}
function ajax_change_shipping() {
wp_enqueue_script('ak-country-change', get_template_directory_uri() . '/inc/js/select-country.js', array('jquery',), '1.0', true);
wp_localize_script('ak-country-change', 'akselect', localize_array());
}
add_action('wp_enqueue_scripts', 'ajax_change_shipping');
// Ajax調用
$(document).ready(function() {
$('body').on('change', '.yo-lang-select', function() {
var select = $(this);
var value = select.val();
$.ajax({
url: akselect.ajax_url,
method: 'POST',
dataType: 'json',
data: {
action: 'get_the_defualt_lang',
value: value
nonce: akselect.nonce
},
success: function (respond) {
console.log(respond);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
}
});
});
});
//這是函數
add_action('wp_ajax_get_the_defualt_lang', 'get_the_defualt_lang');
add_action('wp_ajax_nopriv_get_the_defualt_lang','get_the_defualt_lang');
function get_the_defualt_lang() {
$lang = $_POST['value'];
add_action('woocommerce_add_to_cart', 'set_country_befor_cart_page');
function set_country_befor_cart_page() {
WC()->customer->set_country($lang);
WC()->customer->set_shipping_country($lang);
}
$respond = $lang;
echo json_encode($respond);
die();
}
//彈出形式
<div data-effect="mfp-zoom-out" id="shop-pop_1" class="mfp-hide">
<select class="yo-lang-select" data-type="lang" title="">
<option value="US">United States</option>
<option value="GB">United Kingdom (UK)</option>
<option value="US">Other Countries</option>
</select>
//使用掛機,可從該店鋪頁面
add_action('woocommerce_after_shop_loop', 'ak_store_popup');
function ak_store_popup() {
get_template_part('template-parts/popups/popup', 'store-main');
}
我添加的形式和鉤檢查之前得到它在店鋪頁面的代碼,希望它能幫助,感謝您的反饋 – Akarob
我現在又增加了,謝謝 – Akarob