How to generate zip file after woocommerce_email_attachments Woocommerce hook

Viewed 69

What i want to do is create pdf (pdf's) on woocommerce_email_attachments hook, then create zip file with these pdf's, and then on thankyou page locate button to download this zip. To generate zip I used woocommerce_before_thankyou hook but this is too late. So I tried

woocommerce_order_status_on-hold,
woocommerce_order_status_processing

But these hooks seems not firing, maybe they work on transitions in admin order page. Also I tried woocommerce_payment_complete with test paypal account - not created zip also. Or maybe these hooks fire before woocommerce_email_attachments and there is no pdf yet to create zip? So can you suggest hook after sending wc email but before woocommerce_before_thankyou ?

Code which I use to generate pdf's:

add_filter('woocommerce_email_attachments', 'attach_order_notice', 10, 3);
function attach_order_notice($attachments, $email_id, $order)
{
    // check if all variables properly set
    if (!is_object($order) || !isset($email_id)) {
        return $attachments;
    }

    // Skip User emails
    if (get_class($order) == 'WP_User') {
        return $attachments;
    }

    // do not process low stock notifications, user emails etc!
    if (in_array($email_id, array('no_stock', 'low_stock', 'backorder', 'customer_new_account', 'customer_reset_password'))) {
        return $attachments;
    }
    // final check on order object
    if (!($order instanceof \WC_Order || is_subclass_of($order, '\WC_Abstract_Order'))) {
        return $attachments;
    }

    //     if ($email_id == 'customer_on_hold_order') {

    $order_items = $order->get_items(['line_item']);
    $date = $order->get_date_created()->date('h-i-s,j-m-y');
    foreach ($order_items as $product) {
        $html = render_template(locate_template('pdf/certificate/certificate.php'), ['order' => $order, 'order_product' => $product]);
        $html = utf8_decode(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));

        $options = new Dompdf\Options([
            'tempDir' => get_template_directory() . '/certificate',
            'fontDir' => get_template_directory() . '/assets/fonts/',
            'chroot' => get_template_directory(),
            'defaultFont'                => 'Montserrat',
            'isRemoteEnabled'            => true,
            'isHtml5ParserEnabled'        => true
        ]);
        $dompdf = new Dompdf\Dompdf($options);
        $dompdf->loadHtml($html);
        $dompdf->setPaper('A4', 'portrait');
        $dompdf->render();
        $data = $dompdf->output();
        $pdf_path = get_template_directory() . "/certificate/product-{$product->get_product_id()}({$date}).pdf";
        file_put_contents($pdf_path, $data);
        $attachments[] = $pdf_path;
    }
    //     }
    return $attachments;
}

Code which I use to create zip:

add_action('woocommerce_before_thankyou', 'custom_make_thank_page');
function custom_make_thank_page($order_id)
{
    $order = wc_get_order($order_id);
    $order_items = $order->get_items(['line_item']);
    $date = $order->get_date_created()->date('h-i-s,j-m-y');
    $total_quantity = array_sum(array_map(function ($product) {
        return $product->get_quantity();
    }, $order_items));

    //Zip
    $zip = new ZipArchive();
    $file = TMP_PATH . '/pdf/zip/certificates-' . $order->get_id() . '.zip';

    if ($zip->open($file, ZipArchive::CREATE) === TRUE) {

        foreach ($order_items as $item) {
            $file_pdf =  get_template_directory() . "/certificate/product-{$item->get_product_id()}({$date}).pdf";

            if (file_exists($file_pdf)) {
                $zip->addFile($file_pdf, "/certificate/product-{$item->get_product_id()}({$date}).pdf");
            }
        }
        $zip->close();
    }

?>
    <?php if (!$order->has_status('failed')) : ?>

    
        <a href="<?php echo  TMP_URL . '/pdf/zip/certificates-' . $order->get_id() . '.zip'; ?>" class="button button-thank-page"><span>Download certificate</span>

        </a> // Here downloading not works, works only after reloading page
    
        </div>
    <?php endif; ?>
<?php
}
0 Answers
Related