Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

php - Add downloads in Woocommerce downloadable product programmatically

I am creating a Woocommerce site where I wanna give vendors to upload products from their front end. But I stuck on a point where I am trying to add woocommerce downloadable products.

Here is my current code:

//Let's upload the download file Zip 
    $zipattachment_id = upload_music_files($music_ID, $musicZip); //This is my custom function which returns attachment id after file upload

    $download_file_name = $musicZip['name'];
    $download_file_url  = wp_get_attachment_url($zipattachment_id);
    $md5_download_num = md5( $download_file_url );

    //creating array of download product
    $downloadable_file[$md5_download_num] = array(
        'id'   =>  $md5_download_num,
        'name'   =>  $download_file_name,
        'file'   =>  $download_file_url,
        'previous_hash' => ''
    );
    $downloadMusic = serialize($downloadable_file);

    // adding downloadble file with the new array
    add_post_meta( $music_ID, '_downloadable_files', $downloadMusic );

But when I open the product edit from backend no downloadable files exits.

Here is example return of the seriallized array :

a:1:{s:32:"22618d7f028803f57f98ab6b21277387";a:4:{s:2:"id";s:32:"22618d7f028803f57f98ab6b21277387";s:4:"name";s:5:"1.zip";s:4:"file";s:71:"http://mydomain/wp-content/uploads/2017/12/5a2e22cecaca6_1.zip";s:13:"previous_hash";s:0:"";}}

I am working with the latest version of woocommerce, can anybody suggest me what I am doing wrong and what can be the right way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should better use WC_Product and WC_Product_Download methods. I have revisited your code renaming/shortening a bit your variables names.

The code:

//This is my custom function which returns attachment id after file upload
$zip_attachment_id = upload_music_files( $music_id, $music_zip ); 

$file_name = $music_zip['name'];
$file_url  = wp_get_attachment_url( $zip_attachment_id );
$download_id = md5( $file_url );

// Creating an empty instance of a WC_Product_Download object
$pd_object = new WC_Product_Download();

// Set the data in the WC_Product_Download object
$pd_object->set_id( $download_id );
$pd_object->set_name( $file_name );
$pd_object->set_file( $file_url );

// Get an instance of the WC_Product object (from a defined product ID)
$product = wc_get_product( $music_id ); // <=== Be sure it's the product ID

// Get existing downloads (if they exist)
$downloads = $product->get_downloads();

// Add the new WC_Product_Download object to the array
$downloads[$download_id] = $pd_object;

// Set the complete downloads array in the product
$product->set_downloads($downloads);
$product->save(); // Save the data in database

Tested and works

Now you have to be sure that your $music_id variable is the product ID in:

$product = wc_get_product( $music_id );

If not you should get directly the WC_Product object from global $product;

OR the product ID from global $post; and $product_id = $post->ID;, making some changes in the code:

global $post;
$product = wc_get_product( $post->ID );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...