Standard downloadable products are not suitable. It is necessary to generate the file after the creation and payment of the order and provide access to the buyer.
More than a day looking for information on this topic, found something similar, but not that.
It is necessary that the file is generated and attached to the order immediately after payment (it can also be free of charge) or, after payment, the order was able to process, there was an interception with the help of a hook woocommerce_order_status_processing
, the file was generated, the file was attached to the order, and the message was sent to the mail.
I tried to simply add a file to the order, but it is not added:
$pd_object = new WC_Product_Download();
$pd_object->set_id($md5_num);
$pd_object->set_name($file_name);
$pd_object->set_file($file_url);
update_post_meta($order_id,'_files', $pd_object);
I also tried to generate a new product on the move, put the uploaded file into it, and then add this product to the order, it is added but without a file:
$pd_object = new WC_Product_Download();
$pd_object->set_id($md5_num);
$pd_object->set_name($file_name);
$pd_object->set_file($file_url);
$order_product = new WC_Product();
$order_product -> set_name('ORDER'.$order_id);
$order_product -> set_price(0);
$order_product -> set_virtual(true);
$order_product -> set_downloadable(true);
$downloads = $order_product->get_downloads();
$downloads[$md5_num] = $pd_object;
$order_product -> set_downloads($pd_object);
$order -> add_product($order_product,1);
I want the file to be available to the buyer as if using standard downloadable products: Admin panel Buyer Profile