根据单选按钮更改woocommerce送货方式 [英] Change woocommerce shipping method based on radio buttons

查看:127
本文介绍了根据单选按钮更改woocommerce送货方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我能够使用以编程方式设置送货方式Woocommerce [ ^ ]在加载时更改方法。我也尝试了ajax并且已经有一个功能设置,它使用无线电添加新的WC费用,已经发布了以下代码。



我尝试过的:



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.

I was able to use the code from Set shipping method programmatically Woocommerce[^] to change the method on load. 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.

What I have tried:

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'动作中设置代码但是还没有能够使用这两种方法来改变运送方式。



感谢您的帮助



I also 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.

Thanks for your help

推荐答案

_POST ['collect'])){
global
_POST['collect'])) { global


woocommerce;
woocommerce;


district =
district =


这篇关于根据单选按钮更改woocommerce送货方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆