How to generate a unique QR code for every purchased product in WooCommerce?

How to generate a unique QR code for every purchased product in WooCommerce?

Hello guys welcome back to my blog therichpost.com. Today in this blog post we will tell you How to generate a unique QR code for every purchased product in WooCommerce?

  1. WooCommerce Hooks
  2. WordPress Tricks
  3. WordPress Hooks
  4. Dokan

What We’ll Build

  1. Generate unique QR code per purchased product
  2. QR code contains Order ID + Product ID (or any data you want)
  3. QR code saved as real image (PNG)
  4. Display QR on:
  • Thank You page
  • Order details
  • Admin order page
  • Emails (optional)

Step 1: Install QR Code Library (PHP)

WooCommerce doesn’t include QR generation by default, so we’ll use a lightweight library.

Option A (Recommended): Endroid QR Code (PHP)

If you use Composer:

composer require endroid/qr-code

If no Composer, use Google Chart API (simpler – shown below).

Step 2: Generate QR Code After Order Is Placed

Hook: woocommerce_checkout_order_processed

Add this to functions.php or a custom plugin:

add_action('woocommerce_checkout_order_processed', 'generate_qr_for_each_product', 10, 3);

function generate_qr_for_each_product($order_id, $posted_data, $order) {
    $upload_dir = wp_upload_dir();
    $qr_dir = $upload_dir['basedir'] . '/order-qr/';

    if (!file_exists($qr_dir)) {
        wp_mkdir_p($qr_dir);
    }

    foreach ($order->get_items() as $item_id => $item) {
        $product_id = $item->get_product_id();

        // Data you want inside QR
        $qr_data = json_encode([
            'order_id'   => $order_id,
            'product_id' => $product_id,
            'email'      => $order->get_billing_email()
        ]);

        // QR image path
        $qr_file = $qr_dir . "order-{$order_id}-product-{$product_id}.png";

        // Generate QR (Google Chart API)
        $qr_url = 'https://api.qrserver.com/v1/create-qr-code/?size=300x300&data=' . urlencode($qr_data);

        file_put_contents($qr_file, file_get_contents($qr_url));

        // Save QR path in order item meta
        wc_add_order_item_meta($item_id, '_product_qr', $qr_file);
    }
}

This creates real PNG images
Stored in /wp-content/uploads/order-qr/

Step 3: Show QR Code on Thank You Page

add_action('woocommerce_thankyou', 'show_qr_on_thankyou_page');

function show_qr_on_thankyou_page($order_id) {
    $order = wc_get_order($order_id);
    $upload_dir = wp_upload_dir();

    echo '<h3>Your Product QR Codes</h3>';

    foreach ($order->get_items() as $item_id => $item) {
        $qr_path = wc_get_order_item_meta($item_id, '_product_qr', true);

        if ($qr_path) {
            $qr_url = str_replace($upload_dir['basedir'], $upload_dir['baseurl'], $qr_path);

            echo '<div style="margin-bottom:20px">';
            echo '<strong>' . esc_html($item->get_name()) . '</strong><br>';
            echo '<img src="' . esc_url($qr_url) . '" width="150">';
            echo '</div>';
        }
    }
}

Step 4: Show QR Code in Admin Order Page

add_action('woocommerce_after_order_itemmeta', function ($item_id) {
    $qr_path = wc_get_order_item_meta($item_id, '_product_qr', true);
    if ($qr_path) {
        $upload_dir = wp_upload_dir();
        $qr_url = str_replace($upload_dir['basedir'], $upload_dir['baseurl'], $qr_path);
        echo '<p><strong>Product QR:</strong><br><img src="' . esc_url($qr_url) . '" width="100"></p>';
    }
});

(Optional) Add QR Code to Order Emails

add_action('woocommerce_email_after_order_table', function ($order) {
    $upload_dir = wp_upload_dir();

    echo '<h3>Product QR Codes</h3>';
    foreach ($order->get_items() as $item_id => $item) {
        $qr_path = wc_get_order_item_meta($item_id, '_product_qr', true);
        if ($qr_path) {
            $qr_url = str_replace($upload_dir['basedir'], $upload_dir['baseurl'], $qr_path);
            echo '<img src="' . esc_url($qr_url) . '" width="120" style="margin-right:10px;">';
        }
    }
});

Common Use Cases

  • Event tickets
  • Product authenticity check
  • Digital downloads
  • Warranty verification
  • Crypto / NFT access
  • Scan-to-verify purchases

Final Notes

  • No plugin needed
  • Works with any WooCommerce theme
  • Lightweight & fast
  • Fully customizable QR content

Guy if you have any query feel free to contact me.

Ajay

Thanks