【问题标题】:Woocommerce: get the shiping postal code to calculate shipping costWoocommerce:获取运输邮政编码以计算运输成本
【发布时间】:2015-01-12 04:39:50
【问题描述】:

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

【问题讨论】:

  • 你能告诉我们一些你的代码吗?你说它返回“一个空值”,我们没有看到代码就无法解决这个问题。
  • 当然是的:
    public function calculate_shipping( $package ) {
    GLOBAL $woocommerce;
    $items = $woocommerce->cart->get_cart();
    $cliente = $woocommerce->客户;
    $peso = $woocommerce->cart->cart_contents_weight;
    $customer_postcode = $woocommerce->customer->get_shipping_postcode();
    }

    $customer_postcode = $woocommerce->customer->get_shipping_postcode();是空的,我可以让它工作的唯一方法是如果我去结帐页面并返回购物车

标签: php wordpress woocommerce shipping


【解决方案1】:
// 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' ];
} 

【讨论】:

  • 这是正确的答案,get_user_meta(get_current_user_id(), 'shipping_postcode', true );实际上是一种错误的方法。
【解决方案2】:

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

帐单邮政编码

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

发货邮编

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

【讨论】:

  • 我也有同样的想法....但它似乎在您到达结帐页面之前不会加载客户运输数据...所以基本上现在我可以让它工作的唯一方法是如果我去结帐页面并返回购物车
【解决方案3】:
global $woocommerce;
$customer = new WC_Customer();
$woocommerce->customer->get_shipping_postcode();

【讨论】:

  • 请考虑编辑您的帖子,以添加更多关于您的代码的作用以及它为何能解决问题的说明。一个大部分只包含代码的答案(即使它正在工作)通常不会帮助 OP 理解他们的问题。
【解决方案4】:
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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 2012-12-29
    • 2018-05-31
    • 2017-05-07
    • 1970-01-01
    相关资源
    最近更新 更多