olken
olken

Reputation: 51

Woocommerce: get the shiping postal code to calculate shipping cost

I'm developing a new shipping method that calculates the rate based on the package weight, volume and customers zip code... Ive had no problem retrieving the cart information on the calculate_shipping function, but when I try to retrieve the zip code from customer it seems to return an empty value. how can I retrieve this information on runtime in order 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 );

Upvotes: 5

Views: 15023

Answers (4)

Aftab
Aftab

Reputation: 417

// add this in your calculate_shipping function
public function calculate_shipping( $package ) {
     //remove the first bracket at the end of $postal_code
     $postal_code = $package[ 'destination' ][ 'postcode' ];
} 

Upvotes: 10

Manvendra Singh Rana
Manvendra Singh Rana

Reputation: 11

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

Upvotes: 1

Domain
Domain

Reputation: 11808

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 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);

Upvotes: 0

helgatheviking
helgatheviking

Reputation: 26309

I think this is handled in the customer class

billing post code

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

or

shipping post code

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

Upvotes: 7

Related Questions