简体   繁体   English

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

[英]change Woocommerce shipping method based on radio buttons

I have a set of radios with Delivery and Pickup on the billing form.我在帐单上有一套带送货和取货的收音机。 I need the shipping method to change from local_pickup to distance_rate_shipping when the relevant radio is checked on the form.当在表格上检查相关收音机时,我需要将运输方式从 local_pickup 更改为 distance_rate_shipping。

I was able to use the code from Set shipping method programmatically Woocommerce to change the method on load.我能够以编程方式使用 Set shipping method Woocommerce中的代码来更改加载方法。 I also have tried ajax and already have a function setup that uses radios to add a new WC fee, have posted that code below.我也尝试过 ajax 并且已经有一个 function 设置,它使用无线电添加新的 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,'');
    }
  }
}

I tried putting the ajax function and the 'WC()->session->set code inside 'woocommerce_before_checkout_shipping_form' action But havent been able to use the two approaches to change the shipping method.我尝试将 ajax function 和 'WC()->session->set 代码放入 'woocommerce_before_checkout_shipping_form' 操作中,但无法使用这两种方法来更改运输方式。

Thanks谢谢

Have been struggling with this for 3 days and solved it myself... I used ajax to determine which radio was selected and trigger a change on the radios for the shipping method, then used $('body').trigger('update_checkout');已经为此苦苦挣扎了 3 天并自己解决了...我使用 ajax 来确定选择了哪个收音机并触发收音机更改运输方式,然后使用 $('body').trigger('update_checkout' ); on the ajax complete to update the order在 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;
});

Hope this helps someone one day who runs into the same problem希望这对有一天遇到同样问题的人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM