skip to Main Content

With the code below, I am able to change the display of specific shipping methods full labels on WooCommerce cart and checkout pages:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_labels', 10000, 2 );

function custom_shipping_labels($label, $method){   
    $shpmethod = $label;

    if(strpos($shpmethod, 'Express Shipping') !== false){
        $shpmethod = str_replace('Express Shipping',' test express lbl',$shpmethod);
    }
    elseif(strpos($shpmethod, 'Free Standard Shipping') !== false){
        $shpmethod = str_replace('Free Standard Shipping',' test free lbl',$shpmethod);
    }
    return $shpmethod;       
}

Now I need to access the customer selected county inside that custom function hooked in woocommerce_cart_shipping_method_full_label filter hook.

Is it possible to get the customer selected country in that hooked function?

2

Answers


  1. For that you can use WC_Customer::get_shipping_country()

    https://docs.woocommerce.com/wc-apidocs/class-WC_Customer.html

    function custom_shipping_labels( $label, $method ) {
        // DEBUG
        //echo '<pre>', print_r($label, 1), '</pre>';
        //echo '<pre>', print_r($method, 1), '</pre>';
    
        // Get shipping country
        $shipping_country = WC()->customer->get_shipping_country();
    
        // DEBUG
        echo $shipping_country . '<br>';
    
        if ( $shipping_country == 'BE' ) {
            echo 'Yeey' . '<br>';
        }
    
        if( strpos( $label, 'Express Shipping') !== false) {
            $label = str_replace( 'Express Shipping',' test express lbl', $label );
        } elseif( strpos( $label, 'Free Standard Shipping') !== false) {
            $label = str_replace( 'Free Standard Shipping',' test free lbl', $label );
        }
    
        return $label;      
    }
    add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_labels', 10, 2 );
    
    Login or Signup to reply.
  2. To get the selected shipping country you can use one of those:

    For testing purpose, here with the code below, selected (or customer) billing country code is displayed with each shipping method full label:

    add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_labels', 10000, 2 );
    function custom_shipping_labels($label, $method){
        // Get selected shipping country value (country code) 
        $shipping_country = WC()->checkout->get_value('shipping_country');
    
        // If shipping country is empty, we get it from customer data
        if( empty( $shipping_country ) ) {
            $shipping_country = WC()->customer->get_shipping_country();
        }
        return $label .' ('.$shipping_country. ')';
    }
    

    Now in your code you can use $method->label to target each defined shipping method label string, which is more efficient. So your code will be:

    add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_labels', 10000, 2 );
    function custom_shipping_labels($label, $method){
        // Get selected checkout shipping country value (country code) 
        $shipping_country = WC()->checkout->get_value('shipping_country');
    
        // If shipping country is empty, we get it from customer data
        if( empty( $shipping_country ) ) {
            $shipping_country = WC()->customer->get_shipping_country();
        }
    
        if ( $method->label == 'Express Shipping' ) {
            $label = str_replace( $method->label, __('test express lbl'), $label );
        }
        elseif ( $method->label == 'Free Standard Shipping' ) {
            $label = str_replace( $method->label, __('test free lbl'), $label );
        }
        return $label;
    }
    

    Code goes in functions.php file of your active child theme (active theme). Tested and works.

    Now you can use the selected shipping country as you wish.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search