Skip to content
◆ WC Gift Cards/README.md·~/projects/wc-giftcards
● WordPress plugin

WC Gift Cards

role year 2025

A WooCommerce gift card plugin for a tailor's webshop: custom purchase flow, custom emails, and a PDF the buyer prints at home.

WordPressWooCommercePHPmPDFJavaScript

Same client as my suit configurator, a Danish tailoring shop. This time they wanted gift cards. There are plugins for that, but they wanted a custom pipeline: custom PDF to print, custom email, everything custom.

So it's its own WooCommerce plugin, with its own database tables instead of WooCommerce coupons.

Recorded on a local test install with demo data and a placeholder shop name.

Buying one

The buyer picks an amount, picks one of three designs, and writes a personal message. The preview card on the right updates as they type. They choose who gets the card (the recipient, themselves, or both), and they can schedule the email for a later date.

After checkout the recipient gets an email with the card, and the buyer gets a PDF to print at home and give as a present.

The gift card purchase page: amount buttons from 100 to 1.000 kr., three design choices, recipient fields and a delivery choice on the left, and a dark green preview card on the right showing 100 DKK.
The purchase page with the live preview. Local test install, placeholder shop name.

The PDF, emoji included

The PDF is the part I'm proudest of. It's built from an HTML template with mPDF, with custom fonts bundled in the plugin.

The annoying part was emoji. People write emoji in the personal message, and mPDF can't draw colour emoji, so they came out as empty boxes. Now the plugin finds every emoji in the card before rendering and swaps it for an image:

includes/admin/class-wc-giftcards-pdf-service.php
1if (preg_match_all($emoji_pattern, $html, $matches)) {
2 $found_emojis = array_unique($matches[0]);
3
4 foreach ($found_emojis as $emoji) {
5 // Extract font size from surrounding context for dynamic sizing
6 $font_size = $this->extract_font_size_for_emoji($html, $emoji);
7 $emoji_replacements[$emoji] = $this->get_emoji_png($emoji, $font_size);
8 }
9}

The pattern catches flags first, since a flag is two characters that only mean something together. Each emoji is fetched from WordPress's own emoji set and sized to match the text around it, so an emoji in the 32 px heading comes out bigger than one in the message. Generated PDFs are cleaned up by a daily cron job.

A gift card PDF page: the heading Tillykke Sofie!, four numbered steps for redeeming the card with the code, a yellow box of important information, and a help box with a contact email.
A PDF generated by the plugin on a local test install with demo data.
gotcha

The shop's host ran PHP 7.4 and had no Composer, so mPDF couldn't be installed the normal way. The plugin ships without a vendor folder and downloads a ready-made 1.8 MB bundle on demand instead, with a progress bar in the admin. I put the bundle on Google Drive, since it was the quickest place to host a file the server could download. If mPDF still isn't there, the plugin falls back to a printable HTML version.

Using one

In the cart you can stack several gift cards on one order and use just part of a balance. Each card shows what it covers and what's left, like "Bruger: 100 kr • Tilbage efter køb: 400 kr" (100 kr used, 400 kr left after the purchase). The order summary lists every code with its own line.

When the order goes through, the balance update runs in a database transaction and locks each card's row with SELECT ... FOR UPDATE, so the same card can't be spent twice by two orders at once. Balance checks and code entry are rate-limited too, so nobody can guess codes by brute force.