Php WC()在WooCommerce 3.7中给出空结果

Php WC()在WooCommerce 3.7中给出空结果,php,wordpress,object,methods,woocommerce,Php,Wordpress,Object,Methods,Woocommerce,调用WC()不会在WC()->customer中返回所需的值,并给出空结果 我使用的示例代码是: $customer = new WC_Customer( $customer_id ); $country = $customer->get_shipping_country(); if ( $country ) { WC()->customer->set_location( $country, $state, $postcode, $city ); WC

调用
WC()
不会在
WC()->customer
中返回所需的值,并给出空结果

我使用的示例代码是:

$customer = new WC_Customer( $customer_id );

$country  = $customer->get_shipping_country();

if ( $country ) {

    WC()->customer->set_location( $country, $state, $postcode, $city );

    WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
}

任何帮助都将不胜感激。

在您的代码中,不要使用
WC()->customer
变量,该变量已经是
$customer=new WC\u customer($customer\u id)定义的
WC\u customer
对象

另外,
$state
$postcode
$city
变量未在代码中定义(
$postcode
$city
是可选的,因为它们是用空值预定义的,请参阅源代码和)

要完成此操作,您需要使用
WC\u data
save()
方法保存数据

因此,您的代码将是:

$customer = new WC_Customer( $customer_id );

$country  = $customer->get_shipping_country();
$state    = $customer->get_shipping_state();

if ( $country ) {

    $customer->set_location( $country, $state );
    $customer->set_shipping_location( $country, $state );

    $customer->save(); // Save the data to the database
}
这应该更好的工作了

注意:在安装WooCommerce主要版本之前,您应该等待至少一到两个月,因为在以后的次要更新版本中总会有漏洞需要解决。这样可以避免许多问题和浪费时间
因此,您最好恢复到稳定的WooCommerce 3.6.5版本,并至少等待2个月


在代码中,不要使用
WC()->customer
变量,该变量已经是
$customer=new WC\u customer($customer\u id)定义的
WC\u customer
对象

另外,
$state
$postcode
$city
变量未在代码中定义(
$postcode
$city
是可选的,因为它们是用空值预定义的,请参阅源代码和)

要完成此操作,您需要使用
WC\u data
save()
方法保存数据

因此,您的代码将是:

$customer = new WC_Customer( $customer_id );

$country  = $customer->get_shipping_country();
$state    = $customer->get_shipping_state();

if ( $country ) {

    $customer->set_location( $country, $state );
    $customer->set_shipping_location( $country, $state );

    $customer->save(); // Save the data to the database
}
这应该更好的工作了

注意:在安装WooCommerce主要版本之前,您应该等待至少一到两个月,因为在以后的次要更新版本中总会有漏洞需要解决。这样可以避免许多问题和浪费时间
因此,您最好恢复到稳定的WooCommerce 3.6.5版本,并至少等待2个月


请不要因为我的回答而改变你的问题代码。你应该保留你原来的问题代码。您可以在我回答下面的评论区给我反馈,我将不胜感激。@LoicTheAztec在将woocommerce从3.5.3更新到3.7后,我面临许多问题,如获取空wc()对象、客户、发货、,购物车等。不仅仅是在客户中。需要紧急帮助。因此,这是一个您应该在Github Woocommerce中报告的错误…因此,正如我在回答的末尾所评论的,您最好返回到稳定的Woocommerce 3.6.x版本。版本3.7是相当新的,当一个新的主要版本发布时,仍然有很多错误。另外,在更新生产网站之前,您应该始终测试stage网站上的主要更新。@LoicTheAztec我已经有一段时间没有更新woocommerce了……我的3.6.5版本中也存在这个问题……仅在通过rest api进行的调用中……您能告诉我使用wc()有什么问题吗与以前一样,RESTAPI非常复杂,这取决于您使用的版本和使用方式……现在一般来说,
WC()
主要用于前端。您可以尝试旧方法:
global$woocommerce$woocommerce->customer
$woocommerce->cart
进行编码>。例如,我注意到一些方法与
newwc\u客户($user\u id)一起工作但不能与
WC()->customer
…请不要更改您的问题代码以回应我的回答。你应该保留你原来的问题代码。您可以在我回答下面的评论区给我反馈,我将不胜感激。@LoicTheAztec在将woocommerce从3.5.3更新到3.7后,我面临许多问题,如获取空wc()对象、客户、发货、,购物车等。不仅仅是在客户中。需要紧急帮助。因此,这是一个您应该在Github Woocommerce中报告的错误…因此,正如我在回答的末尾所评论的,您最好返回到稳定的Woocommerce 3.6.x版本。版本3.7是相当新的,当一个新的主要版本发布时,仍然有很多错误。另外,在更新生产网站之前,您应该始终测试stage网站上的主要更新。@LoicTheAztec我已经有一段时间没有更新woocommerce了……我的3.6.5版本中也存在这个问题……仅在通过rest api进行的调用中……您能告诉我使用wc()有什么问题吗与以前一样,RESTAPI非常复杂,这取决于您使用的版本和使用方式……现在一般来说,
WC()
主要用于前端。您可以尝试旧方法:
global$woocommerce$woocommerce->customer
$woocommerce->cart
进行编码>。例如,我注意到一些方法与
newwc\u客户($user\u id)一起工作但不与
WC()->客户一起使用…