我在帐单上有一组带有送货和取货的收音机。我需要在表格上检查相关无线电时将运输方式从local_pickup更改为distance_rate_shipping。
我能够以编程方式使用 WooCommerce 设置运输方法中的代码来更改加载时的方法。我也尝试过 ajax,并且已经有一个使用无线电添加新的 WC 费用的功能设置,并在下面发布了该代码。
add_action('wp_ajax_woocommerce_apply_collect', 'calculate2', 10);
add_action('wp_ajax_nopriv_woocommerce_apply_collect', 'calculate2', 10);
function calculate2() {
if (isset($_POST['collect'])) {
global $woocommerce;
$district = $_POST['collect'];
$user_role = get_user_role();
if ($district === "Return") {
$val = 0;
} elseif ($district === "Collect" && $user_role != 'business'){
$val = 250;
}
session_start();
$_SESSION['val2'] = $val;
}
}
add_action('woocommerce_cart_calculate_fees', 'wpi_add_ship_fee2');
function wpi_add_ship_fee2() {
@session_start();
$user_role = get_user_role();
$customshipcost = $_SESSION['val2'];
if($customshipcost == 0) {
} else {
if(get_user_role() == 'business' || get_user_role() == 'administrator') {
WC()->cart->add_fee('Collection Fee', 0, true,'');
} else {
WC()->cart->add_fee('Collection Fee', $customshipcost, true,'');
}
}
}
我尝试将 ajax 函数和"WC((->session->set 代码放在"woocommerce_before_checkout_shipping_form"操作中,但无法使用这两种方法来更改运输方法。
谢谢
已经为此苦苦挣扎了 3 天,自己解决了......我使用 ajax 来确定选择了哪个无线电,并在无线电上触发了运输方式的更改,然后使用 $('body'(.trigger('update_checkout'(;在 ajax 上完成更新订单
$('input[type=radio][name=billing_deliverypickup]').change(function () {
billing_district = this.value;
if (this.value == 'Delivery') {
$( "#shipping_method_0_distance_rate_shipping" ).trigger( "click" );
}
else if (this.value == 'Pickup') {
$( "#shipping_method_0_local_pickup3" ).trigger( "click" );
}
var data = {
district: billing_district
};
$.ajax({
url: 'http://pixelshowcase.co.za/kegtails/wp-content/themes/kegtails/update.php',
type: 'POST',
data: data,
beforeSend: function() {
$(".deltype").html(billing_district);
},
complete: function(data) {
$('body').trigger('update_checkout');
}
});
return false;
});
希望这能帮助有一天遇到同样问题的人