在woocommerce中创建带有音频文件的可下载产品 [英] Create a downloadable product with audio file in woocommerce

查看:128
本文介绍了在woocommerce中创建带有音频文件的可下载产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相信您的一天已经圆满了.我需要从前端创建可下载的woocommerce产品.我已经能够创建链接以发布产品并将音频文件添加为附件,但是我需要像付款时一样从仪表板上发布可下载的woocommerce产品一样,使附件在付款后可下载.我已经可以在仪表板上看到我的产品,但是我必须手动检查可下载内容并从仪表板上添加文件.请帮助我自动下载从前端发布的产品. 永远感谢你们. 以下是我的代码段.

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

推荐答案

您应该做的是,在functions.php中添加以下代码.

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

此代码将挂接到woocommerce中,一旦完成订单,它将创建一个新的用户元数据.因此,每个将为指定产品付款的用户都将有一个名为"user_downloads"的新元项目.此项的值将是附件ID或某些加密ID的逗号分隔值.现在,您在模板页面上要做的就是

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.

这篇关于在woocommerce中创建带有音频文件的可下载产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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