Bonjour à tous,
Je développe une petite extension pour la gestion des vouchers pour ma boutique WooCommerce.
Dans mon code, j'ajoute une nouvelle section "Vouchers" dans le menu du tableau de bord du client, qui permet de voir les vouchers achetés depuis la création de son compte et ainsi pouvoir les retélécharger si nécessaire.
À côté de ceux-ci se trouve un bouton "Download". Lorsqu'il clique, un fichier PDF est généré et retourné au client. Cela fonctionne parfaitement lorsque je suis dans la langue par défaut de ma boutique, mais dès que je suis en anglais (avec "/en/" dans mon adresse), le PDF qui est retourné est vide.
Voici le code de la section vouchers:
function display_vouchers_in_dashboard() {
$user_id = get_current_user_id();
if ($user_id > 0) {
$args = array(
'customer_id' => $user_id,
'post_type' => 'shop_order',
'posts_per_page' => -1,
);
$orders = wc_get_orders($args);
$voucher_found = false;
if ($orders) {
echo '<h2>Vos Vouchers</h2>';
echo '<table>';
echo '<thead><tr><th>Date d\'achat</th><th>Montant</th><th>À l\'attention de</th><th>Référence</th><th>Action</th></tr></thead>';
echo '<tbody>';
foreach ($orders as $order) {
foreach ($order->get_items() as $item_id => $item) {
$product = $item->get_product();
if ($product) {
$categories = wp_get_post_terms($product->get_id(), 'product_cat');
foreach ($categories as $category) {
if ($category->slug === 'bons-cadeaux') {
$voucher_reference = wc_get_order_item_meta($item_id, '_voucher_reference', true);
$recipient_name = wc_get_order_item_meta($item_id, '_gift_card_recipient', true);
$voucher_amount = $item->get_total();
$order_date = $order->get_date_created()->date('Y-m-d');
if (!empty($voucher_reference) && !empty($recipient_name)) {
$voucher_found = true;
echo '<tr>';
echo '<td>' . esc_html($order_date) . '</td>';
echo '<td>' . wc_price($voucher_amount) . '</td>';
echo '<td>' . esc_html($recipient_name) . '</td>';
echo '<td>' . esc_html($voucher_reference) . '</td>';
echo '<td>';
echo '<form method="POST" action="">';
echo '<input type="hidden" name="action" value="download_voucher">';
echo '<input type="hidden" name="order_id" value="' . $order->get_id() . '">';
echo '<input type="hidden" name="item_id" value="' . esc_attr($item_id) . '">';
echo '<button type="submit" class="button">Télécharger</button>';
echo '</form>';
echo '</td>';
echo '</tr>';
}
}
}
}
}
}
echo '</tbody>';
echo '</table>';
}
if (!$voucher_found) {
echo '
<div class="woocommerce-info">
Vous n\'avez pas encore acheté de vouchers.
<a class="woocommerce-Button wc-forward button" href="https://radici.lu/boutique/">Parcourir les produits</a>
</div>';
}
}
}
add_action('woocommerce_account_vouchers_endpoint', 'display_vouchers_in_dashboard');
function process_voucher_download() {
if (isset($_POST['action']) && $_POST['action'] === 'download_voucher' && isset($_POST['order_id']) && isset($_POST['item_id'])) {
$order_id = absint($_POST['order_id']);
$item_id = absint($_POST['item_id']);
$user_id = get_current_user_id();
$order = wc_get_order($order_id);
if ($order && $order->get_user_id() === $user_id) {
generate_pdf($order_id, $item_id);
exit;
} else {
wp_redirect(home_url('/404'));
exit;
}
}
}
add_action('template_redirect', 'process_voucher_download');
Et voici le code de ma fonction generate_pdf
function generate_pdf($order_id, $item_id) {
$order = wc_get_order($order_id);
$current_user = wp_get_current_user();
$language = get_locale();
if ($order->get_user_id() != $current_user->ID) {
wp_die('You are not authorized to generate vouchers for this order');
}
$item = $order->get_item($item_id);
$product_id = $item->get_product_id();
$pdf = new Fpdi();
if ($language === "en_GB") {
$pdf->setSourceFile(plugin_dir_path(__FILE__) . 'template_voucher_en.pdf');
$tplIdx = $pdf->importPage(1);
if (has_term('bons-cadeaux', 'product_cat', $product_id)) {
$recipient_name = $item->get_meta('_gift_card_recipient', true);
$reference = $item->get_meta('_voucher_reference', true);
$sender_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
$pdf->AddPage('L');
$pdf->useTemplate($tplIdx);
$pdf->SetFont('Helvetica', '', 14);
$pdf->SetXY(52.5, 13.5);
$pdf->Write(0, $reference);
$pdf->SetXY(93, 136.5);
$pdf->Write(0, $item->get_total() . '€');
$pdf->SetXY(118, 147);
$pdf->Write(0, $recipient_name);
$pdf->SetXY(87.5, 157.5);
$pdf->Write(0, $sender_name);
$pdf->SetXY(103, 168);
$pdf->Write(0, date('d/m/Y', strtotime('+1 year', strtotime($order->get_date_created()))));
}
} else {
$pdf->setSourceFile(plugin_dir_path(__FILE__) . 'template_voucher_fr.pdf');
$tplIdx = $pdf->importPage(1);
if (has_term('bons-cadeaux', 'product_cat', $product_id)) {
$recipient_name = $item->get_meta('_gift_card_recipient', true);
$reference = $item->get_meta('_voucher_reference', true);
$sender_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
$pdf->AddPage('L');
$pdf->useTemplate($tplIdx);
$pdf->SetFont('Helvetica', '', 14);
$pdf->SetXY(53, 13.5);
$pdf->Write(0, $reference);
$pdf->SetXY(94.5, 136.5);
$pdf->Write(0, $item->get_total() . '€');
$pdf->SetXY(109, 147);
$pdf->Write(0, $recipient_name);
$pdf->SetXY(104, 157.5);
$pdf->Write(0, $sender_name);
$pdf->SetXY(109.5, 168);
$pdf->Write(0, date('d/m/Y', strtotime('+1 year', strtotime($order->get_date_created()))));
}
}
return $pdf->Output("voucher_".$reference.".pdf", "D");
}
Quand je fais un dump de $pdf avant le Output, que ce soit en français ou en anglais, j'ai bien l'objet FPDI complet avec les bonnes informations : le bon fichier PDF uploadé selon la langue, ainsi que les données du client.
Pour information, j'utilise le plugin TranslatePress pour la traduction du site;
Auriez-vous une idée d'où pourrait venir le problème ?
Merci d'avance pour votre aide.