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

e5njpo68  于 2023-01-16  发布在  PHP
关注(0)|答案(4)|浏览(123)

我正在开发一种新的运输方法,它可以根据包裹重量、体积和客户的邮政编码计算运费......我在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 );
pw9qyyiw

pw9qyyiw1#

// 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' ];
}
ax6ht2ek

ax6ht2ek2#

我认为这是在客户类中处理的
计费邮政编码
WC()->customer->get_postcode();

航运邮政编码
WC()->customer->get_shipping_postcode();

sauutmhj

sauutmhj3#

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

qnzebej04#

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

相关问题