第80行的woocommerce / includes / wc-notice-functions.php上的成员函数get()on null

时间:2017-10-19 19:37:52

标签: php wordpress woocommerce

我正在制作一个Woocommerce送货插件,其中我有一个类方法,其中包含以下自定义do_action

的触发器
try {
    $client = new SoapClient( plugin_dir_path( dirname( __FILE__ ) ) . 'includes/wsdl/Test.wsdl', ['trace' => true, 'exceptions' => true ]  );
    $results = $client->__soapCall('CreateSomml', ['parameters' =>  ['Details' => $bill->__toArray() ]]);
} catch (SoapFault $e) {
    wc_add_wp_error_notices(new WP_Error('soap', $e->getMessage(), isset($client)? $client->__getLastRequest() : $bill->__toArray() ));
    // print $client->__getLastRequest();
    // throw $e;
}

尝试连接到Web服务并捕获任何SoapFault并添加错误通知,但在运行代码时遇到错误

  

致命错误:在第80行的..... / wp-content / plugins / woocommerce / includes / wc-notice-functions.php中调用null成员函数get()

基于文档导致

function wc_add_notice( $message, $notice_type = 'success' ) {
    if ( ! did_action( 'woocommerce_init' ) ) {
        wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
        return;
    }

    $notices = WC()->session->get( 'wc_notices', array() );

    // Backward compatibility
    if ( 'success' === $notice_type ) {
        $message = apply_filters( 'woocommerce_add_message', $message );
    }

    $notices[ $notice_type ][] = apply_filters( 'woocommerce_add_' . $notice_type, $message );

    WC()->session->set( 'wc_notices', $notices );
}

Line 80 is return

我认为可能是

WC()->session->set( 'wc_notices', $notices );但不确定错误的原因。

此操作正在从后端管理员管理中触发。帽子可能是问题?

我使用Wordpress 4.8和Woocommerce 3.2.1运行PHP 5.6.27

更新:

一直试图找到错误,我能想出的就是

  

致命错误:未捕获错误:在/var/www/wordpress/wp-content/plugins/woocommerce/includes/wc-notice-functions.php:80中调用null上的成员函数get():堆栈跟踪:# 0 /var/www/wordpress/wp-content/plugins/woocommerce/includes/wc-notice-functions.php(198):wc_add_notice('Invalid users Cr ...','error')#1 / var / www /wordpress/wp-content/plugins/wc-trinicargo-shipping/includes/class-wc-trinicargo-shipping-create-waybill.php(80):wc_add_wp_error_notices(Object(WP_Error))#2 / var / www / wordpress / wp-includes / class-wp-hook.php(296):Wc_Trinicargo_Shipping_Create_Waybill-> create()#3 /var/www/wordpress/wp-includes/class-wp-hook.php(323):WP_Hook-> apply_filters('',Array)#4 /var/www/wordpress/wp-includes/plugin.php(453):WP_Hook-> do_action(Array)#5 / var / www / wordpress / wp-content / plugins / wc-trinicargo-shipping / includes / class-wc-trinicargo-shipping-create-waybill.php(68):do_action('wc_trinicargo_c ...')#6 / var / www / wordpress / wp-content / plugins / wc -trinicargo航运/管理/谐音/ wc-trinicargo-shipping-init-methods.php(163):第80行的/var/www/wordpress/wp-content/plugins/woocommerce/includes/wc-notice-functions.php中的Wc_

为什么会话没有开始?我该如何开始呢?

1 个答案:

答案 0 :(得分:1)

所以对于任何寻求解决方案的人-问题是WC()->会话在这里尚不可用。我遇到了尝试在“ admin_post”操作中添加通知的问题。在这里初始化一个空会话对我有用:

WC()->session = new WC_Session_Handler();
WC()->session->init();

这两行之后,我能够成功使用wc_add_notice。希望对别人有帮助。