Click here to Skip to main content
15,886,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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,'');
    }
  }
}


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
Posted
Updated 6-Mar-18 4:08am

1 solution

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'); on the ajax complete to update the order

$('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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900