Skip to content

Cart promo code in WordPress

A promo code field that checks the code, explains a refusal (unknown, minimum spend) and recalculates discount, shipping and total.

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

code-promo-panier.php
<?php
/**
 * Plugin Name: Forge, Code promo dans le panier
 * Description: Un champ de code promo qui vérifie le code, explique le refus (inconnu, minimum d’achat) et recalcule remise, livraison et total. Ajoute la composition « Code promo dans le panier » à 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-code-promo-panier
 *
pattern.html
<!-- wp:html -->
<section class="cpr" data-cpr data-cpr-codes="{&quot;BIENVENUE10&quot;:{&quot;type&quot;:&quot;percent&quot;,&quot;value&quot;:10,&quot;label&quot;:&quot;−10 % sur les articles&quot;},&quot;PORTOFFERT&quot;:{&quot;type&quot;:&quot;shipping&quot;,&quot;value&quot;:0,&quot;label&quot;:&quot;Livraison offerte&quot;},&quot;NOEL15&quot;:{&quot;type&quot;:&quot;amount&quot;,&quot;value&quot;:15,&quot;min&quot;:80,&quot;label&quot;:&quot;−15 € dès 80 € d’achat&quot;}}" data-cpr-sub="64" data-cpr-ship="4.9" aria-labelledby="cpr-titre">
        <h2 class="cpr-title" id="cpr-titre">Récapitulatif</h2>
        <dl class="cpr-lines">
          <div class="cpr-line"><dt>Coffret thés de Chine, 4 × 50 g</dt><dd>38,00 €</dd></div>
          <div class="cpr-line"><dt>Théière en verre, 60 cl</dt><dd>26,00 €</dd></div>
        </dl>
        <form class="cpr-form" data-cpr-form novalidate>
          <label class="cpr-label" for="cpr-code">Code promo</label>
          <div class="cpr-row">
            <input class="cpr-input" id="cpr-code" name="code" type="text" autocomplete="off" autocapitalize="characters" spellcheck="false" aria-describedby="cpr-message">
            <button class="cpr-apply" type="submit">Appliquer</button>
script.js
(() => {
  if (!document.querySelector(".cpr")) return;
  document.querySelectorAll("[data-cpr]").forEach((root) => {
    const on = (el, type, fn) => el.addEventListener(type, fn);
    const codes = JSON.parse(root.getAttribute("data-cpr-codes"));
    const sub = Number(root.getAttribute("data-cpr-sub"));
    const shipping = Number(root.getAttribute("data-cpr-ship"));
    const euro = new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" });
    const form = root.querySelector("[data-cpr-form]");
    const input = form.querySelector("input");
    const msg = root.querySelector("[data-cpr-msg]");
    const chip = root.querySelector("[data-cpr-chip]");
style.css
:root {
  --cpr-card: #ffffff;
  --cpr-text: #13261f;
  --cpr-muted: #475a52;
  --cpr-line: #d3e0da;
  --cpr-field: #ffffff;
  --cpr-accent: #0f5c46;
  --cpr-accent-text: #ffffff;
  --cpr-chip: #e3f3ec;
  --cpr-good: #0f6b4f;
  --cpr-error: #b91c1c;
  --demo-code-promo-panier-bg: #e8f0ec;

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 colour and size picker for a product page: the visual, price and stock follow the choice, and sold-out sizes are disabled.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component