我想在主页上弹出一个简单的模型,让用户在结帐/购物车页面之前选择他们的运送国家。更新客户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