首页 > 解决方案 > Woocommerce order->get_data() 抛出 PHP 致命错误:未捕获错误

问题描述

我有一个正在运行的函数,它首先检查帖子类型是否为“shop_order”,如果不是,则不应运行该函数。我正在使用return;来结束它,但是当我尝试在“产品”帖子类型中添加新帖子时,我收到以下错误:

 PHP Fatal error:  Uncaught Error: Call to a member function get_data() on boolean in.. 

为什么说该功能get_data()未捕获?post_type如果不是,则此代码不应运行到这一点shop_order。我什至打印出 $post_type 返回product

function woocommerce_process_shop_order ( $post_id, $post, $update ) {

    if (isset($post->post_status) && 'auto-draft' == $post->post_status) {
      return;
    }
        // Autosave, do nothing
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
        // AJAX? Not used here
        if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
        return;
    }
        // Check user permissions
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
          return;
    }

    // don't run the echo if the function is called for saving revision.
    if ( $post->post_type == 'revision' ) {
       return;
    }

        $post_type = get_post_type($post_id);
    error_log( print_r( $post_type, true ) );

   // terminate early if post type is not 'post' and if we are updating post object and not creating it.
   if ( "shop_order" != $post_type && !$update ) {
       return;
       error_log( print_r( 'not shop order', true ) );
   }

  $is_new = $post->post_date === $post->post_modified;

  if ( $is_new ) {

    $order = wc_get_order( $post_id );
    $order_data = $order->get_data();

  }
}

标签: phpwordpresswoocommercereturn

解决方案


推荐阅读