Skip to content

Colour and size variants in WordPress

A colour and size picker for a product page: the visual, price and stock follow the choice, and sold-out sizes are disabled.

Here is an excerpt of this component's code in WordPress. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in WordPress

pattern.html
<!-- wp:html -->
<form class="pvc" data-pvc data-stock="{&quot;ecru&quot;:{&quot;S&quot;:6,&quot;M&quot;:2,&quot;L&quot;:0,&quot;XL&quot;:4},&quot;marine&quot;:{&quot;S&quot;:0,&quot;M&quot;:7,&quot;L&quot;:5,&quot;XL&quot;:1},&quot;rouille&quot;:{&quot;S&quot;:3,&quot;M&quot;:0,&quot;L&quot;:2,&quot;XL&quot;:0}}" aria-labelledby="pvc-titre">
        <div class="pvc-visual" style="--pvc-color: #1f2f52" data-pvc-visual>
          <svg class="pvc-svg" viewBox="0 0 80 80" aria-hidden="true"><path d="M22 10l10-5h16l10 5 14 12-8 11-6-4v41H22V29l-6 4-8-11z"/><path class="pvc-stripe" d="M22 40h36M22 50h36M22 60h36"/></svg>
        </div>
        <div class="pvc-info">
          <p class="pvc-brand">Tricots Kerlan · fabriqué à Douarnenez</p>
          <h2 class="pvc-title" id="pvc-titre">Pull marin en laine mérinos</h2>
          <p class="pvc-price"><span data-pvc-price>89,00 €</span></p>
          <fieldset class="pvc-fs">
            <legend class="pvc-legend">Couleur : <strong data-pvc-color>Marine</strong></legend>
            <div class="pvc-swatches">
produit-variantes-couleurs.php
<?php
/**
 * Plugin Name: Forge, Variantes de couleur et de taille
 * Description: Un sélecteur de couleur et de taille pour fiche produit : le visuel, le prix et le stock suivent le choix, les tailles épuisées sont désactivées. Ajoute la composition « Variantes de couleur et de taille » à l'éditeur de blocs.
 * Version: 1.0.0
 * Requires at least: 6.6
 * Requires PHP: 8.1
 * Author: Forge
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: forge-produit-variantes-couleurs
 *
script.js
(() => {
  if (!document.querySelector(".pvc")) return;
  document.querySelectorAll("[data-pvc]").forEach((root) => {
    const on = (el, type, fn) => el.addEventListener(type, fn);
    const form = root;
    const stock = JSON.parse(form.dataset.stock);
    const euro = new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" });
    const field = (name) => form.querySelector(`[data-pvc-${name}]`);
    const picked = (name) => form.querySelector(`input[name="pvc-${name}"]:checked`);
    const colorName = (input) => input.closest("label").textContent.trim();

    const update = () => {
style.css
:root {
  --pvc-card: #ffffff;
  --pvc-text: #18202f;
  --pvc-muted: #4b5568;
  --pvc-line: #cfd6e0;
  --pvc-visual: #eef2f7;
  --pvc-ring: #1f2f52;
  --pvc-ring-bg: #ffffff;
  --pvc-accent: #1f2f52;
  --pvc-accent-text: #ffffff;
  --pvc-low: #9a3412;
  --pvc-selected: #eef2f7;

Free component: copy it freely, including for commercial projects.

E-commerce components in WordPress
  • A limited-time offer bar: countdown to a fixed date, one-click copyable promo code and a fallback message once the offer is over.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A sales card showing the discount that grows with the cart amount (-10% from €50, -15% from €100…) with a progress bar towards the next tier.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A Christmas gift guide filtered by budget and theme, with the number of ideas announced and an empty state offering to show everything again.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • The last order dates for delivery before Christmas, per shipping method, with the days left worked out from the visitor’s date.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A cart that shows live how much is left to add for free shipping, with a progress bar and recalculated totals.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A wishlist for an online shop: add from product cards, a summary with total and removal, remembered in the browser.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component