DavidTonarini
DavidTonarini

Reputation: 951

While using woocommerce_process_product_meta hook how to debug?

I need to store informations in a custom product type. I correctly created the form, and I'm using the hook woocommerce_process_product_meta to save the custom informations about this product. In the same function, I am doing a file upload, like this. The basic informations are saved, but the upload does not work.

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!!";
}

I know the directory exists and that the function is called, since the other options are correctly saved. However, the file simply isn't uploaded. I'm sure I could debug this myself, the problem is that I don't know how to display any notifications, so I don't know what is wrong. As you can see, in the sample above I tried to use an "echo", but I can't see that output. What can I use to display a notification when the product is saved, so I know what is going on?

Upvotes: 1

Views: 7328

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254212

To debug in Wordpress you should first read the following: Debugging in Wordpress.

Also read: Debugging WooCommerce PHP with Javascript console.log doesn't work

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…

Upvotes: 1

Peter HvD
Peter HvD

Reputation: 1661

Don't know if this is the answer, but your if statement has $_FILES["my__file"] but the other lines have $_FILES["my_file"] so there's a discrepancy in the naming there!

Upvotes: 0

Related Questions