Back to Shopify Liquid
Copy this discount code v2
Shopify Liquid

Copy this discount code v2

A compact, centered discount code banner that allows users to copy a promotional code to their clipboard with a single click. It replaces the code text with a success confirmation message upon successful copying.

EasyDiscount CodeCROProduct Page LightsCopy to ClipboardPromotional Bar
77 lines · 4.7 KB
Setup 5–10 minutes 5 min read 77 lines
Key features
  • Clipboard API integration
  • Inline SVG iconography
  • Post-copy success state transition
  • Lightweight CSS styling
Best use cases
  • Displaying seasonal discounts on product pages
  • Incentivizing newsletter signups with a visible code
  • Reducing friction during the checkout process
Compatibility
  • Shopify Online Store 2.0
  • Most Shopify themes

The code

Shopify Liquid
1<div class="discount-code-wrapv1"
2id="discountCodeWrapv1">
3
4<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" width="18" viewBox="0 0 1200 1200" enable-background="new 0 0 1200 1200" xml:space="preserve">
5<path fill="#676767" d="M1068.883,321.37c-4.468-29.514-20.158-55.495-44.167-73.182c-19.327-14.233-42.198-21.771-66.143-21.771 c-35.414,0-69.062,16.997-90.026,45.461c-36.488,49.595-25.865,119.627,23.703,156.154c19.326,14.233,42.198,21.771,66.156,21.771 l0,0c5.823,0,11.57-0.628,17.239-1.523c-7.372,72.734-149.308,18.749-187.409,2.457c-78.161-33.405-140.95-56.622-194.921-69.599 C321.666,315.7,110.976,574.73,142.269,760.744c4.877,29.104,0.321,41.185,9.19,49.709c-7.077,9.907-12.555,20.375-16.189,31.485 c-19.032,58.233,17.035,110.22,28.553,124.786l5.439,6.86h573.618l4.94-10.162c31.792-65.222,26.57-118.207-15.857-160.775 c66.743-85.775-166.957-173.573,102.477-229.785c153.224-31.958,188.867-96.628,181.266-139.12 c12.593-7.513,23.83-17.355,32.687-29.412C1066.08,380.32,1073.374,350.858,1068.883,321.37z M719.956,937.414h-532.79 c-11.006-16.049-28.899-49.402-17.495-84.215c12.657-38.702,59.602-70.288,135.728-91.33c31.062-8.575,71.582-13.132,117.17-13.132 c96.911,0,194.563,20.786,248.804,52.935C730.63,836.792,745.528,877.773,719.956,937.414z M1019.262,382.879 c-14.18,19.25-36.923,30.73-60.856,30.73c-16.165,0-31.625-5.081-44.693-14.732c-33.506-24.675-40.699-72.03-16.023-105.536 c14.167-19.25,36.936-30.717,60.882-30.717c16.165,0,31.626,5.081,44.68,14.706c16.227,11.954,26.838,29.514,29.859,49.454 C1036.144,346.723,1031.229,366.639,1019.262,382.879z"/>
6</svg>
7
8
9 Christmas special! Enjoy 20% off disocunt code <span id="textToCopyv1">Xmas24</span>
10 <button id="copyButtonv1" style="border: none; background: none; cursor: pointer;">
11 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="13" viewBox="0 0 24 24" version="1.1">
12 <!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
13 <title>ic_fluent_copy_24_regular</title>
14 <desc>Created with Sketch.</desc>
15 <g id="🔍-Product-Icons" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
16 <g id="ic_fluent_copy_24_regular" fill="#676767" fill-rule="nonzero">
17 <path d="M5.50280381,4.62704038 L5.5,6.75 L5.5,17.2542087 C5.5,19.0491342 6.95507456,20.5042087 8.75,20.5042087 L17.3662868,20.5044622 C17.057338,21.3782241 16.2239751,22.0042087 15.2444057,22.0042087 L8.75,22.0042087 C6.12664744,22.0042087 4,19.8775613 4,17.2542087 L4,6.75 C4,5.76928848 4.62744523,4.93512464 5.50280381,4.62704038 Z M17.75,2 C18.9926407,2 20,3.00735931 20,4.25 L20,17.25 C20,18.4926407 18.9926407,19.5 17.75,19.5 L8.75,19.5 C7.50735931,19.5 6.5,18.4926407 6.5,17.25 L6.5,4.25 C6.5,3.00735931 7.50735931,2 8.75,2 L17.75,2 Z M17.75,3.5 L8.75,3.5 C8.33578644,3.5 8,3.83578644 8,4.25 L8,17.25 C8,17.6642136 8.33578644,18 8.75,18 L17.75,18 C18.1642136,18 18.5,17.6642136 18.5,17.25 L18.5,4.25 C18.5,3.83578644 18.1642136,3.5 17.75,3.5 Z" id="🎨-Color"></path>
18 </g>
19 </g>
20 </svg>
21 </button>
22</div>
23
24<script>
25 document.getElementById("copyButtonv1").addEventListener("click", function () {
26 const textToCopy = document.getElementById("textToCopyv1").textContent;
27
28 // Use Clipboard API for modern browsers
29 navigator.clipboard.writeText(textToCopyv1).then(function () {
30 // Hide the current content and show "Copied to clipboard!"
31 const discountWrap = document.getElementById("discountCodeWrapv1");
32 discountWrap.innerHTML = '<span class="copied-messagev1" id="copiedMessagev1">Copied to clipboard!</span>';
33
34 // Fade-in effect
35 const copiedMessagev1 = document.getElementById("copiedMessage");
36 copiedMessagev1.style.opacity = 0;
37 copiedMessagev1.style.transition = "opacity 1s";
38
39 setTimeout(function () {
40 copiedMessagev1.style.opacity = 1;
41 }, 10);
42 }).catch(function (err) {
43 console.error("Failed to copy: ", err);
44 });
45 });
46</script>
47
48<style>
49 .discount-code-wrapv1 {
50 display: flex;
51 justify-content: center;
52 font-size: 10px;
53 background: #f2f2f2;
54 border-radius: 8px;
55 width: max-content;
56 margin-left: auto;
57 margin-right: auto;
58 padding: 3px 7px;
59 font-weight: 600;
60 color: #676767;
61 align-items: center;
62gap: 2px;
63 }
64
65 #textToCopyv1 {
66 text-decoration: underline;
67 margin-left: 4px;
68 color: #0098d9;
69 }
70
71
72 .copied-messagev1 {
73 font-size: 12px;
74 font-weight: bold;
75 color: #0098d9;
76 }
77</style>

How to install Copy this discount code v2 on your Shopify theme

This snippet belongs in sections/main-product.liquid (or any section where you want it to appear), in the block of your template where it makes the most contextual sense. Expect the whole job to take about 5–10 minutes.

  1. 1From your Shopify admin, open Online Store → Themes, click the button on your live theme and choose Duplicate. Always work on a copy so you can roll back instantly.
  2. 2On the duplicated theme, click … → Edit code to open the theme code editor.
  3. 3Open sections/main-product.liquid (or any section where you want it to appear) in the file tree. If your theme uses different file names, search for the template that renders the area where you want the block to show up.
  4. 4Click Copy code on this page, then paste the snippet in the block of your template where it makes the most contextual sense.
  5. 5The snippet ships with its own scoped <style> block, so you do not need to touch base.css or theme.css. If you prefer to keep CSS centralised, move the contents of the style tag into your theme stylesheet and delete the inline block.
  6. 6Keep the <script> tag at the end of the snippet. It only runs once the surrounding markup exists in the DOM, so moving it higher up the file can break the behaviour.
  7. 7Click Save, then hit Preview and check the block on desktop, tablet and mobile widths.
  8. 8When you are happy with the result, publish the duplicated theme from Online Store → Themes → Actions → Publish.

How to customise it

Every value below lives inside the snippet, so you can adapt Copy this discount code v2 to your brand without touching the rest of the theme.

  • Colours — the snippet uses #676767, #f2f2f2, #0098d9. Replace those values inside the style block with your brand palette, keeping at least a 4.5:1 contrast ratio between text and background so the block stays accessible.
  • Sizing — adjust the font-size values (10px, 12px) and the padding so the block carries the same visual weight as the elements around it.
  • Cornersborder-radius: 8px controls the roundness. Set it to 0 for a sharper editorial look, or increase it for a softer, app-like feel.
  • Copy — rewrite the hard-coded text (for example “Xmas24”) in your own brand voice, and translate it if you sell in more than one language.

Common mistakes to avoid

These are the issues merchants run into most often when installing a snippet like this one.

  • Editing the live theme directly. Duplicate it first — a single unbalanced Liquid tag can take a storefront down mid-campaign.
  • Pasting the code into the wrong file. Dropped into layout/theme.liquid instead of sections/main-product.liquid, this block will render on every page of the store, including ones where it makes no sense.
  • Renaming the CSS classes in the markup but not in the style block, or vice versa. The class names have to match exactly or the styling silently disappears.
  • Adding the snippet twice on the same page. The script targets its selector, so two copies can conflict or double-fire.
  • Publishing without testing on a real phone. Most Shopify traffic is mobile, and layout problems almost always surface there first.

Performance notes

What this block costs you in load time, and how to keep that cost at zero.

  • The CSS is inline and scoped to this block, so it adds no extra network request. At a few kilobytes it has no measurable effect on your Lighthouse score.
  • The JavaScript is vanilla — no jQuery, no external library, no additional request. Keep it after the markup so it never blocks rendering.
  • Run PageSpeed Insights before and after you add the block so you have a concrete number rather than a guess.

SEO & accessibility

How to add this block without damaging your on-page structure or your rich results.

  • Nothing in this snippet should replace your page <h1>. Keep product and page headings intact and use <h2> or <h3> for the block's own heading so the document outline stays clean.
  • If the block shows review or rating content, keep it consistent with the structured data your review app already outputs. Duplicating or contradicting rating markup can cost you the rich result entirely.
  • The block does not create a new URL, so there is no canonical or indexing change to make. It improves on-page engagement signals rather than crawlability.
Frequently asked questions

Copy this discount code v2 — questions merchants ask

Will this snippet work with my theme?

It is built with standard Liquid, HTML and CSS, so it works with Dawn and virtually every Online Store 2.0 theme, as well as most vintage themes. The only thing that changes between themes is the file you paste it into — if yours has no sections/main-product.liquid, look for the equivalent template that renders the same area.

Do I need an app to use it?

No. This is theme code you own outright: no monthly fee, no third-party script tag, and no app that can break your storefront the next time it updates.

How long does it take to add “Copy this discount code v2” to a store?

Most merchants have it live in under fifteen minutes — duplicate the theme, paste the code, adjust the colours and copy, preview on mobile, publish.

The block appears but nothing happens. What is wrong?

That is almost always a script-order problem. Check that the script tag is still at the bottom of the snippet and that the markup was not pasted inside another script or a Liquid comment block. Open your browser console and look for an error naming the snippet's selector.

The styling looks broken after pasting. How do I fix it?

Confirm you copied the whole snippet including the closing style tag, then check whether one of your theme's own CSS rules is overriding it. If a theme rule wins, increase the specificity of the snippet's selector rather than scattering !important through the file.

Will it slow my store down?

Not meaningfully. It is inline markup with scoped CSS and at most a few lines of vanilla JavaScript — no external libraries, no extra HTTP request. Optimising your product images will always have a far bigger impact on load time.

Can I use it on a client store or a commercial project?

Yes. Every snippet in the MCO Hub library is free to copy, modify and ship on any store, including client work. Attribution is appreciated but not required.

Related snippets

Keep exploring