使用woocommerce_process_product_meta hook 时如何调试?

While using woocommerce_process_product_meta hook how to debug?

我需要在自定义产品类型中存储信息。 我正确地创建了表单,并且我正在使用挂钩 woocommerce_process_product_meta 来保存关于该产品的自定义信息。 在同一个函数中,我正在做一个文件上传,就像这样。基本信息保存了,但是上传不了。

add_action( 'woocommerce_process_product_meta', 'save_my_custom_settings' );
function save_my_custom_settings( $post_id ){
echo "test";
//Save options
update_post_meta( $post_id, 'my_custom_option', esc_attr( $_POST[ 'my_custom_option' ] ) );

    /*
    * Upload the file
    */
    if ( $_FILES["my_file"] ) {
        $path_parts = pathinfo( $_FILES["my_file"]["name"] );
        $extension = $path_parts['extension'];
        $stored_file_name = time() . '.' . $extension;

        $storage_dir = dirname( __FILE__ ) . '/downloads';

        $target_file = $storage_dir . '/' . $stored_file_name;

        if ( !move_uploaded_file($_FILES["my_file"]["tmp_name"], $target_file) ) {

            echo "Sorry, there was an error uploading your file.";
        } else echo "File transfer ok";

        /*
         * Store information on the file
        */  
        update_post_meta( $post_id, 'my_file_name', $stored_file_name );
    } else echo "File missing!!";
}

我知道目录存在并且调用了函数,因为其他选项已正确保存。但是,文件根本没有上传。 我确定我可以自己调试,问题是我不知道如何显示任何通知,所以我不知道出了什么问题。如您所见,在上面的示例中,我尝试使用 "echo",但看不到该输出。 保存产品时,我可以使用什么来显示通知,以便我知道发生了什么?

要在 Wordpress 中进行调试,您应该首先阅读以下内容:Debugging in Wordpress.

另请阅读:

Now there is multiple tricks/ways to get the data. I use this sometimes like in your case:
For example you can use some temporary meta fields to store any debug data for the current order, with Wordpress function update_post_meta().
Then you will be able to output related data with get_post_meta() or look directly in database in wp_postmeta table for your temporary debug custom fields keys…

不知道这是否是答案,但是你的 if 语句有 $_FILES["my__file"] 但其他行有 $_FILES["my_file"] 所以这里的命名存在差异!