Santosh Khatri Santosh Khatri - 2 months ago 14
PHP Question

Adding programatically more downloadable files for products in WooCommerce

I am trying to uploaded one more downloadable files in woocommerce product. I have already one downloadable file in my product and want to add one more.

For this I am using following code:

if($_FILES){
$attachment_id = media_handle_upload( 'abe_update_epub', $post_id );
if ( is_wp_error( $attachment_id ) ) {
$errors = $attachment_id->get_error_messages();
foreach( $errors as $error ){
echo $error;
}
echo 'There was an error uploading the image';
} else {
// to get exiting file/Old file
$abe_file = get_post_meta($abe_post_id, '_downloadable_files', true);
foreach($abe_file as $abe){
$name = $abe['name'];
$url = $abe['file'];
}
// This is my new file which i want to upload also
$file_name = 'Epub Files';
$file_url1 = wp_get_attachment_url($attachment_id);
$files[md5( $file_url )] = array(
'name' => $file_name,
'file' => $file_url
);
update_post_meta( $post_id, '_downloadable_files', $files );
echo 'The image was uploaded successfully!';
}
}


This function upload files in a correct way, But it replace the old file by the new one.

How I can solve this issue?

What am I doing wrong in this script?

Thanks

Answer

Definitive Update 3

There was a lot of mistakes in your code:

In your code there is 2 mistakes in get_post_meta() function:
- Replaced undefined $abe_post_id by defined $post_id. - Removed third argument "true" as it's an array (NOT a string).

The outputted array of $abe_file is a tri-dimensional array with a structure similar to this example:

array( 
    0 => array(
        "67f3fe902b6c55ac07b92ac804d1a9c8" => array(
            "name" => "filename1"
            "file" => "http://www.domain.tld/wp-content/uploads/woocommerce_uploads/2016/09/file1.pdf"
        ),
        "95ce074e798b2e9d6d0d4cbce02f0497" => array(
            "name" => "filename2"
            "file" => "http://www.domain.tld/wp-content/uploads/woocommerce_uploads/2016/09/file2.pdf"
        )
    )
);

You don't need to iterate this array in a foreach loop as before, because you just want to insert your new file in it. You have to be sure that $post_id is the ID of your product (as $abe_post_id was undefined)…

This is the working updated code:

if($_FILES){
$attachment_id = media_handle_upload( 'abe_update_epub', $post_id );
    if ( is_wp_error( $attachment_id ) ) {
        $errors = $attachment_id->get_error_messages();
        foreach( $errors as $error ){
            echo $error;
        }
        echo 'There was an error uploading the image';
    } else {

        // Get exiting array of downloadable files. IMPORTANT:
        // 1) => Removed "true" condition as it's an array (ot a string)
        // 2) => As "$abe_post_id" is not defined, I have replace it with "$post_id"
        $abe_file = get_post_meta($post_id, '_downloadable_files'); // removed "true"

        // NEW FILE: Setting the name, getting the url and and Md5 hash number
        $file_name = 'Epub Files';
        $file_url  = wp_get_attachment_url($attachment_id);
        $md5_num = md5( $file_url );

        // Inserting new file in the exiting array of downloadable files
        $abe_file[0][$md5_num] = array(
            'name'   =>  $file_name,
            'file'   =>  $file_url
        );

        // Updating database with the new array
        update_post_meta( $post_id, '_downloadable_files', $abe_file[0] );

        // Displaying a success notice
        echo 'The image was uploaded successfully!';
    }
}
Comments