【问题标题】:WC() is giving empty results in WooCommerce 3.7WC() 在 WooCommerce 3.7 中给出空结果
【发布时间】:2019-12-28 15:03:51
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

  • 请不要根据我的回答更改您的问题代码。您应该保留原始问题代码。您可以在我的答案下方的评论区中给我一个反馈,我将不胜感激。
  • @LoicTheAztec 将 woocommerce 从 3.5.3 更新到 3.7 后,我面临许多问题,例如为客户、运输、购物车等获取空 wc() 对象……而不是仅在客户中……紧急帮助需要。
  • 所以这是一个你应该在 Github Woocommerce 中报告的错误......所以正如我在答案末尾评论的那样,你最好恢复到稳定的 WooCommerce 3.6.x 版本。 3.7 版相当新,并且在新的主要版本发布时仍然存在很多错误。此外,在更新生产网站之前,您应该始终在舞台网站上测试主要更新
  • @LoicTheAztec 我有一段时间没有更新 woocommerce 了......而且我的 3.6.5 版本中也存在问题......仅在通过 rest api 调用时......你能告诉我吗像以前一样使用 wc() 有什么问题
  • Sorry rest Api 非常复杂,具体取决于您使用的版本和使用方式……现在一般WC() 主要用于前端。您可以尝试旧方法,即:global $woocommerce;$woocommerce->customer$woocommerce->cart。例如,我注意到有些方法适用于new WC_Customer( $user_id );,但不适用于WC()->customer...

标签: php wordpress object methods woocommerce


【解决方案1】:

在您的代码中,使用您的 $customer 变量代替 WC()->customer,该变量已经是 $customer = new WC_Customer( $customer_id ); 定义的 WC_Customer 对象。

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

要完成,您需要使用WC_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 个月。

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 2023-03-20
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多