2016-09-15 4 views
1

Ich versuche, eine weitere herunterladbare Dateien in Woocommerce-Produkt hochgeladen. Ich habe bereits eine herunterladbare Datei in meinem Produkt und möchte eine weitere hinzufügen.Hinzufügen von programmatisch mehr herunterladbaren Dateien für Produkte in WooCommerce

Dazu verwende ich folgenden 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!'; 
    } 
} 

Diese Funktion Upload-Dateien auf eine korrekte Art, Aber es die alte Datei durch die neue ersetzen.

Wie kann ich dieses Problem lösen?
Was mache ich falsch in diesem Skript?

Dank

+0

@LoicTheAztec jetzt es funktioniert, vielen Dank für mein Leben retten. Kann ich dies für das Hochladen von Variationsprodukten implementieren? –

Antwort

3

- Definitive Update 3

Es gab viele Fehler im Code:

In Ihrem Code gibt es zwei Fehler in get_post_meta() Funktion:
- Ersetzt undefined $abe_post_id durch definierte $post_id. - Entferntes drittes Argument "true" wie es ein Array ist (keine Zeichenfolge).

Das ausgegebene Array von $abe_file ist ein Tri-dimensionales Array mit einer Struktur, die ähnlich zu diesem Beispiel:

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

Sie brauchen nicht dieses Array in einer foreach Schleife iteriert wie zuvor, weil Sie nur Ihre neue Datei darin einfügen möchten. Sie müssen sicher sein, dass $post_id die ID Ihres Produkts (wie $abe_post_id undefined war) ...

Dies ist der Arbeits aktualisierte 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!'; 
    } 
} 
0

update_post_meta umschreibt meta ganz.

Sie sollten alte Daten ($ abe_file) und new ($ file) in ein neues Array einfügen und mit update_post_meta schreiben.

+0

Danke für die Antwort, können Sie mir helfen, wie ich neue Array beitreten? Ich habe versucht, aber nicht Erfolg. –

Verwandte Themen