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

时间:2014-11-14 17:46:45

标签: php wordpress woocommerce shipping

我正在开发一种新的送货方式,根据包裹重量,数量和客户邮政编码计算费率... 我在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 );

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

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

结算邮政编码

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

发货邮政编码

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

答案 2 :(得分:0)

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