在WooCommerce中以编程方式为产品添加可编程的更多可下载文件

时间:2016-09-15 06:40:16

标签: php wordpress download woocommerce product

我正在尝试在woocommerce产品中上传一个可下载的文件。我的产品中已有一个可下载的文件,并且想要再添加一个。

为此,我使用以下代码:

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

此功能以正确的方式上传文件,但它会用新文件替换旧文件。

我如何解决这个问题?
我在这个剧本中做错了什么?

由于

2 个答案:

答案 0 :(得分:3)

  

- 最终更新3

     

您的代码中出现了很多错误:

     

在您的代码中,get_post_meta()函数中存在2个错误:
   - 按照定义的 $abe_post_id 替换了未定义$post_id 。    - 删除了第三个参数 "true" ,因为它是一个数组(不是字符串)。

     

输出的 $abe_file 数组是一个三维数组,其结构与此示例类似:

     
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"
        )
    )
);

您不需要像以前那样在foreach循环中迭代此数组,因为您只想在其中插入新文件。您必须确保 $post_id 是您产品的ID(因为 $abe_post_id 未定义)...

这是工作更新的代码:

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

答案 1 :(得分:0)

update_post_meta重写整个元。

您应该在新数组中加入旧数据($ abe_file)和new($ file)并使用update_post_meta编写它。