Create a downloadable product with audio file in woocommerce

偶尔善良 提交于 2019-12-22 16:40:01

问题


trust your day has been fulfilling. I need to create a downloadable woocommerce product from the front end. I have been able to create link to post the product and add the audio file as attachment but I need to make this attachment downloadable after payment just like you have when you post a downloadable woocommerce product from the dashboard. I can see my product in the dashboard already but I have to manually check downloadable and add file from the dashboard. Please I need help on how I can make the product posted from front-end downloadable automatically. Thank you people for always. Below is my code snippet .

// ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
    'post_title'    =>   $title,
    'post_content'  =>   $description,
    'post_category' =>   array($_POST['cat']),  
    'tags_input'    =>   array($tags),
    'post_status'   =>   'draft',          
    'post_type' =>   'product',  //'post',page' or use a custom post type if you want to
    'rating'    =>   $myrating
    );

//SAVE THE POST
$pid = wp_insert_post($new_post);

         //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
wp_set_post_tags($pid, $_POST['post_tags']);

//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect( $link );

//ADD OUR CUSTOM FIELDS
add_post_meta($pid, 'rating', $myrating, true);

//INSERT OUR MEDIA ATTACHMENTS
if ($_FILES) {
    foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$pid);
    // $newupload returns the attachment id of the file that

    }

} // END THE IF STATEMENT FOR FILES

回答1:


What you should do is, in your functions.php add this code.

function user_downloads($order_id) {
// Variables to change
$download_file_id = CUSTOM_ID_OR_ATTACHMENT_ID_OF_FILE;
$user_id = USER_ID;
$product_id = PRODUCT_ID;


  $order = new WC_Order( $order_id );
  if($order['product_id'] == $product_id ){

    $user_downloads = get_user_meta($user_id, 'user_downloads', TRUE);
    if(!empty($user_downloads)){
        $user_downloads = $user_downloads.','.$download_file_id;
        update_user_meta($user_id, 'user_downloads', $user_downloads );
    }else{
        update_user_meta($user_id, 'user_downloads', $user_downloads );
    }
  }
}
add_action( 'woocommerce_order_status_completed', 'user_downloads' );

This code will hook into woocommerce and when ever an order is completed it will create a new user meta data. So every user who will make a payment for the specified product will have a new meta item called 'user_downloads'. The value of this item will be the comma separated values of attachment id or some encrypted id. Now all you have to do in the template page is

$user_downloads = get_user_meta($user_id, 'user_downloads', TRUE);
$user_downloads = explode(',',$user_downloads);

foreach($user_downloads as $user_download){
   echo '<a href="'.wp_get_attachment_url( $user_downloads[$user_download] ).'">Downloadable File</a>';
}

This way they can buy as many products as they want and all these items will get stacked in their user profile from where they can download them.



来源:https://stackoverflow.com/questions/28602548/create-a-downloadable-product-with-audio-file-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!