Woocommerce: get shipping zip code to calculate shipping cost

I am developing a new shipping method that calculates the speed based on the package weight, volume and zip code of the customers ... Ive had no problem finding the basket information of the calculate_shipping function, but when I try to get the zip code from the customer it seems to be returns blank. how can I get this information at runtime to calculate the final cost?

public function calculate_shipping( $package ) { 
    GLOBAL $woocommerce; 
    $items = $woocommerce->cart->get_cart(); 
    $cliente = $woocommerce->customer;  
    $peso = $woocommerce->cart->cart_contents_weight; 
    $customer_postcode = $woocommerce->customer->get_shipping_postcode(); 
}

      

Solved:

$customer_postcode = get_user_meta( get_current_user_id(), 'shipping_postcode', true );

+3


source to share


4 answers


// add this in your calculate_shipping function
public function calculate_shipping( $package ) {
     $postal_code = $package[ 'destination' ][ 'postcode' ] );
} 

      



+4


source


global $woocommerce;
$customer = new WC_Customer();
$woocommerce->customer->get_shipping_postcode();

      



+2


source


I think this is handled in the client class

billing postal code

WC()->customer->get_postcode();

or

sending postal code

WC()->customer->get_shipping_postcode();

+1


source


global $woocommerce; 
$customer = new WC_customer();
$customer_id = $customer->ID

$get_customer_shipping_pincode = get_user_meta($customer_id,"shipping_postcode",true);
$get_customer_billing_pincode = get_user_meta($customer_id,"billing_postcode",true);

      

if the user is logged in (for register user),

$get_customer_shipping_pincode = get_user_meta(get_current_user_id(),"shipping_postcode",true);
$get_customer_billing_pincode = get_user_meta(get_current_user_id(),"billing_postcode",true);

      

0


source







All Articles