2016-09-15 70 views
1

我正嘗試在woocommerce產品中上傳一個可下載文件。我的產品中已經有一個可下載的文件,並且希望再添加一個。以編程方式爲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!'; 
    } 
} 

此功能上傳文件以正確的方式,但它通過更換新一舊的文件。

我該如何解決這個問題?
我在這個劇本中做錯了什麼?

謝謝

+0

@LoicTheAztec現在它的工作,感謝拯救我的生命。我是否可以實現此變體產品上傳。 –

回答

3

- 權威更新3

有很多在你的代碼錯誤:

在你的代碼中有2次失誤get_post_meta()功能:
- 替換未定義$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是你的產品(如$abe_post_id是不確定的)的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!'; 
    } 
} 
0

update_post_meta整體重寫meta。

您應該在新數組中加入舊數據($ abe_file)和新數據($ file)並使用update_post_meta將其寫入。

+0

感謝您的回覆,你能幫我嗎我如何加入新陣列?我嘗試過但沒有成功。 –

相關問題