Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce 2.1 Detect Chosen Shipping Method

I'd been using the following to decide if a checkout field needed to be complete or not...

if ($posted['shipping_method'] == "local_pickup_plus") {
}

Since updating to WooCommerce 2.1, my code no longer works.

I've tried to echo the value stored in $posted['shipping_method'] to see if I'm checking it against the correct value, but it appears nothing is stored in this variable anymore.

I've been looking for other methods of checking the chosen shipping method, but I'm not getting very far.

Any assistance would be greatly appreciated.

like image 929
user1503689 Avatar asked Mar 13 '14 15:03

user1503689


1 Answers

Been looking for this for a couple of hours, then decided to delve into the WooCommerce files...

This seems to be working for me:

 $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
 $chosen_shipping = $chosen_methods[0]; 

I'm using this to set a min total for local delivery by using it like this in my functions.php

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;


if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

 $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
 $chosen_shipping = $chosen_methods[0]; 

 $min_spend = 25;
 $cart_total = $woocommerce->cart->cart_contents_total;
 if (($cart_total < 25) AND ($chosen_shipping == 'local_delivery')) {   
    $surcharge = $min_spend-$cart_total;    
    $woocommerce->cart->add_fee( 'Delivery Surchage', $surcharge, true, 'standard' );
}

}

Hope this helps someone out.

like image 167
George Avatar answered Oct 04 '22 22:10

George