This code will allow you to add a new tabs inside the Product Datas to your woocommerce product trough the WordPress Media Library
Then, when your customers will place an order and when the order will be processing, they will receive the file inside the email. On the body of the mail and also in attached file.
Great, no ? 🙂
Here is the full code of the plugin
Josselyn jayant & WYCAN
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// Ajouter l'onglet
add_filter('woocommerce_product_data_tabs', 'add_upload_file_tab', 50, 1);
function add_upload_file_tab($tabs) {
$tabs['upload_file'] = array(
'label' => __('Téléverser un fichier', 'woocommerce'),
'target' => 'upload_file_options',
'class' => array(),
);
return $tabs;
}
// Ajouter le contenu de l'onglet
add_action('woocommerce_product_data_panels', 'upload_file_tab_content');
function upload_file_tab_content() {
global $post;
$file_url = get_post_meta($post->ID, '_uploaded_file', true);
?>
get_items();
foreach ($items as $item) {
$product_id = $item->get_product_id();
$file_url = get_post_meta($product_id, '_uploaded_file', true);
if ($file_url) {
$file_path = get_attached_file(get_attachment_id_by_url($file_url));
if ($file_path) {
$attachments[] = $file_path;
}
}
}
}
return $attachments;
}
// Ajouter le lien de téléchargement dans l'e-mail de confirmation
add_action('woocommerce_email_after_order_table', 'add_file_link_to_order_email', 10, 4);
function add_file_link_to_order_email($order, $sent_to_admin, $plain_text, $email) {
if ($email->id == 'customer_processing_order') {
$items = $order->get_items();
foreach ($items as $item) {
$product_id = $item->get_product_id();
$file_url = get_post_meta($product_id, '_uploaded_file', true);
if ($file_url) {
echo "Fichier lié à votre produit : " . $item->get_name() . "
";
if ($plain_text) {
echo "Télécharger: " . $file_url . "\n";
} else {
echo "Télécharger
";
}
}
}
}
}
// Fonction pour récupérer l'ID d'une pièce jointe depuis son URL
function get_attachment_id_by_url($url) {
$id = attachment_url_to_postid($url);
if ($id) {
return $id;
}
return false;
}
// Ajouter l'icône "dashicons-upload"
function custom_admin_style() {
echo '';
}
add_action('admin_head', 'custom_admin_style');