我有一个正在运行的函数,该函数首先检查帖子类型是否为“ 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();
}
}