BadAddy
BadAddy

Reputation: 376

WP WooCommerce get country from checkout dropdown for hook

I am trying to get a 3rd party shipping price via their own API. They need the country, weight and service. I have the HTTP request sending, with hardcoded values. But when I try to get the actual values I seem to hit a wall when it comes to Country.

enter image description here

When the user changes the country it would need to resend for a price, currently I am looking for the default which in this case is United Kingdom.

I am, however, unable to get that value using the following hooks:

woocommerce_shipping_fields

woocommerce_checkout_get_value

This is the current code, and here it gets the weight dynamically:

add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $url  = 'http://********/shipping/read.php';
    $args =  array(
        'body' => array(
            'weight' => $cart->get_cart_contents_weight(),
            'location' => 'United Kingdom',
            'service' => 1
        )
    );
    $data = wp_remote_post( $url, $args );
    $p = json_decode($data['body']);
    //print_r($p);

    $fee = $p->Data->rate;
    // Setting the calculated fee based on weight
    $cart->add_fee( __( 'Shipping Rate' ), $fee, false );
}

What needs to happen as a start, is to get the current country which is pre-loaded (defaulted). Then if a user ever changes this, for it to interrogate the API again with the new country, and apply that new price.

None of the hooks I tried above work with getting me any actual value, and idea's what the correct filter is?

Thanks Addy

Upvotes: 3

Views: 7055

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253783

You should try to use first:

WC()->customer->get_shipping_country()

… as it will be updated on customer change and it's filled by the auto-detect location feature of woocommerce.

You can also try to use one of those:

$shipping_counrtry = WC()->session->get('customer')['shipping_country'];

or

$package = WC()->shipping->get_packages()[0];
$custome_shipping_country = $package['destination']['country'];

You can also use a combination like:

$customer_shipping_country = WC()->customer->get_shipping_country();

if( empty($custome_shipping_country) ){
    $package = WC()->shipping->get_packages()[0];
    if( ! isset($package['destination']['country']) ) return $passed;
    $customer_shipping_country = $package['destination']['country'];
}

So in your code:

add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $custome_shipping_country = WC()->customer->get_shipping_country();

    if( empty($custome_shipping_country) ){
        $package = WC()->shipping->get_packages()[0];
        if( ! isset($package['destination']['country']) ) return $passed;
        $customer_shipping_country = $package['destination']['country'];
    }

    $url  = 'http://********/shipping/read.php';
    $args =  array(
        'body' => array(
            'weight' => $cart->get_cart_contents_weight(),
            'location' => $custome_shipping_country,
            'service' => 1
        )
    );
    $data = wp_remote_post( $url, $args );
    $p = json_decode($data['body']);
    //print_r($p);

    $fee = $p->Data->rate;
    // Setting the calculated fee based on weight
    $cart->add_fee( __( 'Shipping Rate' ), $fee, false );
}

This code goes on function.php file of your active child theme (or theme).

It should works.

I use Shipping instead of billing as shipping country will be filled with billing data if customer doesn't provide shipping data.

Upvotes: 5

Related Questions