简体   繁体   English

Woocommerce:获取运输邮政编码以计算运费

[英]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. 我正在开发一种新的送货方式,根据包裹重量,数量和客户邮政编码计算费率...我在calculate_shipping功能上检索购物车信息没有问题,但是当我尝试从客户检索邮政编码时它似乎返回一个空值。 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 );

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

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

global $woocommerce;
$customer = new WC_Customer();
$woocommerce->customer->get_shipping_postcode();
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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM