在WooCommerce中以编程方式为产品添加更多可下载文件 [英] Adding programatically more downloadable files for products in WooCommerce

查看:105
本文介绍了在WooCommerce中以编程方式为产品添加更多可下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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?

谢谢

推荐答案


权威性更新3

其中存在很多错误您的代码:

在您的代码中, get_post_meta() 函数:

-替换了未定义 $ abe_post_id 通过定义的 $ post_id
-删除了第三个参数 true ,因为它是一个数组(不是字符串)。

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).

$ abe_file 的输出数组是三维数组,其结构类似于以下示例:

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


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

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

这篇关于在WooCommerce中以编程方式为产品添加更多可下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆