Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 607
olken Avatar asked Dec 06 '22 23:12

olken


1 Answers

// 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' ];
} 
like image 120
Aftab Avatar answered Feb 26 '23 01:02

Aftab