更新客户Woocommerce结账前的地址页面
问题描述:
我想在主页上弹出一个简单的模型,让用户在结帐/购物车页面之前选择他们的运送国家。更新客户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');
}
答
更新插入:一些其他更改成功测试:
- jQuery的:当页面当选择字段被加载时,'US'默认值现在通过ajax设置。
- 选择字段:只需要2个值(默认值:'US'|其他:'GB')。您可以添加许多其他人。
- 该国家现在正确设置(开票国家是必要的)。
1)jQuery代码:
有什么特别的事与WordPress与jQuery
,就是肖特兰$
没有工作...所以为了让你的工作jQuery代码需要启动搭配:jQuery(document).ready(function ($) {
jQuery(document).ready(function ($) {
var select = $('.yo-lang-select'),
value = select.val();
function ajax_update(value){
$.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);
}
});
}
ajax_update(value);
$('body').on('change', select, function() {
value = select.val();
ajax_update(value);
});
});
2)HTML选择字段:
<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>
</select>
3)PHP函数:
- 现在你得到正确的响应和Ajax是工作的罚款。
- 的另一个问题是使用
set_country_befor_cart_page
挂钩函数在woocommerce_add_to_cart
在你的PHP脚本的AJAX内嵌因为它永远不会,永远不会得到$lang
值。 - 我已经加入了结算国家真正定国多为结账...
所以,而不只是使用这种方式:
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'];
WC()->customer->set_country($lang);
WC()->customer->set_billing_country($lang); // Updated HERE Too
WC()->customer->set_shipping_country($lang);
echo json_encode($lang);
die();
}
代码放在的function.php文件您活跃的儿童主题(或主题)或任何插件文件。
这一次,这是测试和工作。
- 作为您的选择字段在弹出加载,你也许应该需要做一些ajustements jQuery中,而不是处理此事件。
- 可能是您应该需要限制您的脚本与
if (! is_user_logged_in()) {
,为非登录/注册的客户只有。
我添加的形式和钩检查之前得到它在店铺页面的代码,希望它能帮助,感谢您的反馈 – Akarob
我现在又增加了,谢谢 – Akarob