WooCommerce code snippet: convert the Order Notes field into an EU VAT ID field

The scenario: My WooCommerce store has no need for the Order Notes field. In fact, up until now I had it hidden on the checkout page. But what my site does need is an EU VAT ID field. The portion of my business that takes place in Europe is, so far, well below the VAT reporting threshold, but I am increasingly being asked by customers to provide an invoice containing their VAT ID.

Well, my site does already produce PDF invoices. But there was no way for customers to include their VAT ID on the invoice. Until now.

A simple code snippet converts the existing WooCommerce Order Notes field into an EU VAT ID field, including changing it from a <textarea> to an <input type="text"> field. Put this in your theme’s functions.php file, or wherever else is appropriate in your setup:

add_filter('woocommerce_checkout_fields', function($fields) {
    $fields['order']['order_comments'] = array_merge(
        $fields['order']['order_comments'],
        array(
            'class' => array('eu-only'),
            'label' => 'EU VAT ID',
            'type' => 'text',
        )
    );
    return $fields;
}, 10, 1);

That’s it. You can stop right here. But you may notice a line in there that seems unnecessary: 'class' => array('eu-only')

What’s that all about? Well, I’m using that with a bit of jQuery to enhance the functionality: only showing my new EU VAT ID field when the user’s selected Billing Country is an EU country.

Here’s a JavaScript function you can use to dynamically show/hide elements with an .eu-only CSS class, depending on a given passed-in value:

function showHideEUOnly(val) {
    var eu = ['AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'ES', 'SE', 'GB'];
    if (eu.indexOf(val) != -1) {
        jQuery('.eu-only').show();
    }
    else {
        jQuery('.eu-only').hide();
    }
}

I obtained the list of EU VAT-applicable countries here, and I decided to include 'GB' (the United Kingdom) in the list, despite… y’know, uh… Brexit, because I have the vague impression that UK customers may still be impacted by VAT policies. (Being a dumb American, I don’t know much about it. I think maybe the UK has its own VAT now? Anyway, suffice to say, you may want to modify your list of 2-digit country codes in the eu array, as applicable to your situation.)

This function isn’t going to do anything unless it’s called though, so let’s do that. Here’s a bit of jQuery that will call it both on the initial page load and any time the Billing Country field changes:

jQuery(function() {
    if (jQuery('body').hasClass('woocommerce-checkout')) {
        jQuery('select[name="billing_country"]').on('change', function() {
            showHideEUOnly(jQuery(this).val());
        });
        showHideEUOnly(jQuery('select[name="billing_country"]').val());
    }
});

Both of these JavaScript snippets can go in a script.js file in your theme, or wherever else is appropriate in your setup.

That’s the end of the story, but there’s more…

Incidentally, there’s more to my custom setup. I’ve significantly modified the layout of my checkout page. I’ve got WooCommerce configured for billing addresses only, with this setting in Shipping Options:

I then used CSS to hide everything else in the second column (including, up until now, the Order Notes field) and moved the product summary and payment information up into that space. Explaining all of that is outside the scope of this post, but one thing you may find useful is my CSS for hiding the “Additional Information” <h3> heading. This selector is a bit of overkill, but it works:

body.woocommerce-checkout .woocommerce > form.checkout .col2-set > .col-2 .woocommerce-additional-fields > h3:first-child { display: none; }

There’s context in my CSS file to justify all of that, but you should be able to accomplish the same with just this:

.woocommerce-additional-fields > h3 { display: none; }

On the Futility of Naming Colors

I posted this over on the ICS Calendar blog, but it’s probably of some interest to the audience of this blog, dealing with the intersection of code and design.

HTML has “named colors” which are… weird. And I have finally given up on using them in the plugin, which was a bit of a silly thing to do anyway, when there are 16 million RGB colors at one’s disposal. The post ends with swatches of both the old and the newly revised color palettes.

https://icscalendar.com/on-the-futility-of-naming-colors/

How I learned to stop worrying and embrace being a middle-aged curmudgeon

Reading John Gruber’s thoughts on the potential future existence of an iPhone SE 4, which necessarily included a note on the lack of an ongoing slot in the iPhone lineup for my beloved 13 mini, got me once again thinking more generally about something that’s been on my mind a lot lately, as my 49th birthday approaches in 2023.

That thing I have been thinking is, this is not the future I signed up for. I’m beginning to understand why older people get set in their ways and cranky about change, in ways that probably could not have occurred to me until I had lived enough to experience a lot of things firsthand.

I remember lots of things, both good and bad. Most things, in general, improve and get better over time. But some things get worse — at least, worse by my subjective standards, because there are certain things I don’t want, like a phone that’s too big for me to use with one hand, or fit comfortably in my jeans pocket.

Possibly even more frustrating than things that get worse are things that just stop existing. Things I really liked that are now gone, especially if they are gone for reasons that I do not think are sound. That happens a lot for me with styles of music or genres of video games. I want new synthwave music* that I can get as excited about as when I first heard Tycho and Com Truise in 2012, for instance, or new Castlevania games that are as good as Aria of Sorrow.

True, sometimes those glories do return, and 2021 gave me a double whammy, in the form of Mitch Murder’s Then Again album and the all-time classic Metroid Dread. But more often than not, I just have to move on and give up on dreaming of a decent modern SimCity game, or a computer Scrabble that will ever be even a fraction as “smart” as the GameHouse version I loved back in 2006.

The world is change. I get that. It’s just hard to let go of things that I know are better than what has replaced them, and that makes me cling desperately to the old things I have that I’m still able to enjoy, whether that’s 40-year-old vinyl records on my turntable or 20-year-old GameBoy Advance ROMs in an emulator.

The true nature of the curmudgeon, I think, is not borne of pessimism. The curmudgeon is not purely a crank. It’s optimism, idealism. Belief in a world that could and should be better than the one we’re living in, because you remember something that’s gone. And beyond that, you remember the trajectory those past things suggested we were on. But somehow we never got where we were going.

Now, let me be clear: I’m not one of those MAGA types who longs for a return to their fictional, idealized version of what the 1950s were (at least, for white people). I’m inspired by the progress we are making towards a more equitable society for everyone — even as I see how far we still have to go. I’m longing for a future world that never was, but that I believed we could — and would — have by now.

Ironically, it was the sci-fi dystopias that were so popular in my childhood in the 1980s, that seemed to get many of the worst things about the 21st century right. The techno-fascism of corporations more powerful than governments, spying on our every action as a way to make more money. Aggregations of the intimate details of millions of people’s personal lives have become the most valuable commodity around. I didn’t believe this was the future we would have, but here we are.

I just want a small phone, some good music to listen to on it (ideally through wired headphones and a 1/8″ jack), and maybe a couple of games that are actually mentally stimulating instead of just “idle” ways to give up money or personal information in an endless stream of “microtransactions.”

Is that too much to ask?

* Of course, synthwave itself is an appeal to the future-should-be-better-than-it-is nostalgia of people (like me) who grew up in the 1980s.

On effective altruism, longtermism, crypto bro billionaires, and the boringness of maintenance

This morning I was reading a Vox article entitled How effective altruism let Sam Bankman-Fried happen, and if that title isn’t enough to scare you off, the first few paragraphs probably will be.

Of course, I am a glutton for punishment, especially when people in their third decade think they have solutions to all of the world’s problems. Not to be a crusty curmudgeon, even though as a “Gen X”er it seems to be my destiny. I just feel like you have to have first-hand memories of the 1980s for proper context on what’s happening today. Or maybe not, but you at least need to be even more cynical about the claimed promise of crypto than the author here. You know, cynical enough to not take the money in the first place.

The problem actually is longtermism

One section of the article begins with the subhead “The problem isn’t longtermism.” But I would argue that, in fact, it is the problem. Or rather, it’s one of a cluster of problems that all come out of the heads of people who think they are smarter than everyone else and somehow have The Answers that are beyond the grasp of mere mortals.

It’s the same mentality that lets a person make the choices (often building on past choices by their forbearers, the ones who allowed them to be “born on third base, thinking they’d hit a triple”) that lead to becoming a billionaire in the first place. It’s a status that can only be achieved on the backs of others.

But whether you’re a billionaire striver, an aspiring Mars colonist, just an ordinary “maker,” or a grandiose philosopher of the distant future, the common thread is that it’s more fun, more inspiring, to invent something — to inhabit a fresh, clean, newly-constructed mental space — than to do the messy, mundane, boring work of maintaining what we already have. Of cleaning up other people’s messes. Of learning how someone else’s invention works, because they’ve moved on to the Next Big Thing and now you’re the one charged with keeping the previous Next Big Thing up and running.

I get it. It’s the same for me, in many ways. I’m a “maker.” I work as a consultant, brought in to create new systems, that then get handed off to internal staff to keep running, while I move on to the next client. But I’m not claiming to solve all of the world’s problems; I’m just making something new that people ask me for when their old thing stops working.

OK, the problem actually is billionaires. Period.

It’s dangerous when the means to solve society’s big problems are concentrated in the hands of a small group of billionaires, and we are left to trust their vision for how that money should be used. These are the guys (and it’s pretty much always guys — bros, if you will) who get that thrill out of grandiose thinking, who don’t want to deal with the messy realities all around us. They don’t want to fix problems. They want to invent something, clean and shiny and new. Don’t clean up Earth; move to Mars. Don’t improve public transportation, distract us with ludicrous promises of tunnels and tubes, so we buy your fancy new cars instead. Don’t do the hard, complicated work to improve the actual lives of the billions of humans who are living in poverty today; concoct some imaginary future event that might kill billions of people, and invent a fancy new high-tech gizmo that will prevent that event from happening.

Later on, the article does get into a lot of the problems with big longtermist philosophy and silver spoon crypto bros who think they understand all of the world’s problems better than anyone who actually has any of those problems in their own lives.

But ultimately, for me, it all simply comes back to the idea that billionaires shouldn’t exist. And since they shouldn’t exist, they shouldn’t be making decisions about how to fund solutions to society’s problems.

This is the kind of thing that I find maddening about Gutenberg

First off, if you are the regular reader of this blog, you are painfully aware of how much I am waffling on rejecting or embracing Gutenberg (a.k.a. the WordPress Block Editor). There are things I genuinely do like about it, but I also have some fundamental disagreements with its approach (like the facts that it writes inline CSS directly into post content, or that its templates don’t support PHP code).

And then there are the weird little quirks that just make using it much more difficult than it needs to be. In general I have learned that this comes down to the incredible finickiness of JSON. Fine. But the fact that one character out of place in the theme.json file can break everything seems a bit ridiculous… or at least, a big step backwards for usability.

Fix your syntax. Sure. I wish JSON allowed trailing commas the way PHP — and even JavaScript! — does, but I can grok the fact that it doesn’t and learn to adapt. What I have a bit more trouble with is the problem that struck yesterday, only because it was so hard to pin down.

I’m working on a new, small block theme as a one-off for a unique project — a website that will feed menu board monitors in a pizza place. Seems like a perfect opportunity to practice and experiment a bit with what Gutenberg can do. (And, after over a decade of trying to find the “right” way to give restaurant staff a reliable but still easy-to-manage web interface for updating menu information, I actually think Gutenberg’s Block Patterns might be the optimal solution.)

So here’s the maddening thing. I was setting up my theme.json file like a good Block Theme developer, defining all of my colors, typography, etc. And then I started loading in some test content. All looked fine on the back end, in the Block Editor itself. But none of the styles were getting applied on the front end!

I googled the problem, of course, and found a WordPress forum thread about this very issue.

Huh. Sure enough, what’s described in the thread was exactly what was happening in my case. I had this line in my code (font names changed to protect the innocent):

"fontFamily": "'Helvetica Neue', 'Comic Sans', 'sans-serif",

Do you see the problem there? (No, it’s not the trailing comma… that actually belongs, in context.) It’s the stray apostrophe before sans-serif. Removing that fixed the problem.

Now, if this were just in straight CSS, I would have seen the problem immediately, because the syntax highlighting in BBEdit would’ve gone all wonky. But since this was inside a text string in JSON, it looked just fine, and I didn’t notice the apostrophe was there.

I get that the parser that converts the contents of theme.json into CSS would get thrown off by this. Honestly, I’m surprised it didn’t cause the whole page to bork with a PHP fatal error… I’m pretty sure earlier versions did throw a fatal error if there was any problem parsing the theme.json file. So this is… an improvement?

I also get that the generated CSS output would not work properly. What I don’t get is that it all looked correct in the Block Editor. Why was the Block Editor on the admin side able to “fix” this issue, but on the front end it didn’t? It doesn’t make sense!

Anyway, trying to unravel that mystery wasted about an hour late yesterday afternoon, when I was finally starting to get productive after having a near existential crisis over Gutenberg for most of the week.

These are hard times to be a WordPress developer. No, strike that. These are hard times to be a generalist web developer who happened to make the fateful decision 8 years ago to go all-in on WordPress.