【发布时间】:2019-09-24 02:11:11
【问题描述】:
信息
WooCommerce 版本:3.6.2
WordPress 版本:5.1.1
YITH WooCommerce 请求报价高级版 2.1.8
Booster Plus 4.3.1 - 按国家/地区划分的价格和货币模块
WooCommerce 1.0 版结账时更改数量
现状
1.YITH 手动报价以欧元创建,发货国家设置为罗马尼亚。
目标
在迷你购物车和结帐中获取正确的订单货币以获取报价,尽管客户访问国家/地区
更新
经过大量搜索,我一无所获。最后我决定安装Change Quantity on Checkout for WooCommerce 结帐插件。
我仍然卡住了。结帐页面刷新一次后,货币会更新。
代码
使用此处Update cart shipping data with AJAX in WooCommerce 的代码,我设法刷新了结帐页面,并且货币在加载时正确显示。
add_action( 'wp_footer', 'refresh_shipping_js' );
function refresh_shipping_js() {
// Only on checkout
if( is_checkout() && ! is_wc_endpoint_url() ):
?>
<script type="text/javascript">
jQuery( function($){
if (typeof wc_checkout_params === 'undefined')
return false;
var refresh = 'yes';
$.ajax({
type: "POST",
url: wc_checkout_params.ajax_url,
data: ({
'action': 'updating_shipping',
'refresh_shipping': refresh,
}),
success: function(response) {
if( response === '1' ) {
$(document.body).trigger('update_checkout');
console.log('Success: '+response); // For testing (to be removed)
} else {
console.log('Failled: '+response); // For testing (to be removed)
}
},
error:function(error) {
console.log('Error: '+error); // For testing (to be removed)
}
});
});
</script>
<?php
endif;
}
// function that gets the Ajax data
add_action( 'wp_ajax_updating_shipping', 'updating_shipping' );
add_action( 'wp_ajax_nopriv_updating_shipping', 'updating_shipping' );
function updating_shipping() {
if ( isset($_POST['refresh_shipping']) && $_POST['refresh_shipping'] === 'yes' ){
WC()->session->set('refresh_shipping', '1' );
} else {
WC()->session->set('refresh_shipping', '0' );
}
echo WC()->session->get('refresh_shipping');
die(); // Alway at the end (to avoid server error 500)
}
// Function that refresh session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
if ( WC()->session->get('refresh_shipping' ) === '1' ) {
foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
WC()->session->set( 'shipping_for_package_' . $package_key, false );
}
WC()->cart->calculate_shipping();
}
}
需要帮助
有没有任何更好的方法来实现这一目标?
所有帮助将不胜感激
【问题讨论】:
标签: php wordpress woocommerce