根据单选按钮更改Woocommerce送货方式

问题描述 投票:0回答:1

我在结算表格上有一套带有送货和取货的收音机。当在表单上检查相关无线电时,我需要运送方法从local_pickup更改为distance_rate_shipping。

我能够使用Set shipping method programmatically 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->设置代码放在'woocommerce_before_checkout_shipping_form'动作中但是还没有能够使用这两种方法来改变运输方法。

谢谢

php ajax woocommerce radio shipping
1个回答
0
投票

一直苦苦挣扎3天并自己解决了......我用ajax确定选择了哪个无线电并触发了对于运输方法的无线电更改,然后使用$('body')。触发器('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;
});

希望这有助于某一天遇到同样问题的人

© www.soinside.com 2019 - 2024. All rights reserved.