为了账号安全,请及时绑定邮箱和手机立即绑定

WooCommerce 自定义插件:WC_Customer 对象实例周围的错误

WooCommerce 自定义插件:WC_Customer 对象实例周围的错误

PHP
哔哔one 2023-04-21 15:51:06
我构建了一个插件,但遇到以下问题:WooCommerce 仪表板(在管理端)不会加载数据。它挂起并失败。我已经跟踪了问题代码:中的问题if ( is_admin() ) {     //removed   } else if ( !$this->is_login_page() && !wp_doing_ajax() ) {    $public = new Public();}这是导致问题的公共端代码!is_admin 或 wp_doing_ajax 都不能阻止它发生。在公共方面,我打电话add_action( 'init', array('Dynamic_Rules', 'dynamic_rule_tax_exemption') );在免税功能中,我特别有这段代码导致了问题:$woocommerce = WC();$user_country = $woocommerce->customer->get_billing_country();$woocommerce->customer->set_is_vat_exempt(true);所以我只能推测发生了什么,也许 WC() 以某种方式将所有内容发送到无限循环中,这就是仪表板不加载数据的原因。我不知道为什么 is_admin() 和 wp_doing_ajax() 不能阻止这种情况发生。也许我在 init 上调用该函数是错误的,但我还能在哪里调用它呢?
查看完整描述

1 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

很难找出您的问题可能是什么……请注意“您的问题应该更新为包括所需的行为、特定问题或错误,以及重现问题所需的最短代码。”


您可以尝试的可能是:


$customer = WC()->customer;


if( ! is_a( $customer, 'WC_Customer' ) {

    global $current_user;


    if( $current_user > 0 ) { 

        $customer = new WC_Customer( $current_user->ID );

    }

}


if( is_a( $customer, 'WC_Customer' ) {

    $billing_country = $customer->get_billing_country();


    if( ! $customer->is_vat_exempt() ) {

        $customer->set_is_vat_exempt( true );

    }   

} else {

    // Some code to throw an error or debug trace

}

我希望这会解决你的问题。如果不是,您需要以某种方式将用户 ID传递给您的代码。



查看完整回答
反对 回复 2023-04-21
  • 1 回答
  • 0 关注
  • 81 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信