我正在使用" woocommerce_order_status_completed"挂钩以在用户付款后动态创建文件。 我需要在这个钩子中按顺序将此文件添加到他的可下载区域。
如何将文件附加到订单的任何ideias?
答案 0 :(得分:3)
woocommerce_order_status_completed您可以添加以下代码... 首先使用media_handle_upload
存储您在上传中创建的文件if($_FILES){
//if u don't want to $post_id u gan give 0
$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 {
// 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 downloadable files
$files[$md5_num] = array(
'name' => $file_name,
'file' => $file_url
);
// Updating database with the new array
$order = new WC_Order($order_id);
if(!empty($files)){
update_post_meta($order->ID,_files,$files));
}
// Displaying a success notice
echo 'The image was uploaded successfully!';
}
}
希望这有助于你..