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



我正在开发一种新的运输方法,该方法可以根据包裹重量,体积和客户邮政编码计算费率...我在calculate_shipping函数上检索购物车信息没有问题,但是当我尝试从客户那里检索邮政编码时,它似乎返回了一个空值。如何在运行时检索此信息以计算最终成本?

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

解决:

$customer_postcode = get_user_meta( get_current_user_id(), 'shipping_postcode', true );
// 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' ];
} 

我认为这是在客户类中处理的

帐单邮寄代码

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

送货邮政编码

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

如果用户已登录(对于注册用户),

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

最新更新