← back to Shopify Sample Shipping

inject-cart-notice.mjs

25 lines

import {TOKEN, SHOP} from '../designerwallcoverings/scripts/lib/shopify.mjs';
import fs from 'node:fs';
const THEME='145121607731', KEY='sections/cart.liquid';
const base=`https://${SHOP}/admin/api/2026-07/themes/${THEME}/assets.json`;
const H={'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'};
let val=fs.readFileSync('verification/cart.liquid.backup','utf8'); // original backup
const anchor=`<h1 class="page-title">{{ 'cart.general.header' | t }}</h1>`;
if(!val.includes(anchor)){console.log('ABORT: anchor not found');process.exit(1);}
if(val.includes('tk-ship-notice')){console.log('notice already present — skipping insert');}
else{
  const notice=`\n  {% comment %}TK-11333 sample shipping notice{% endcomment %}\n  <p class="tk-ship-notice" style="margin:12px 0;padding:10px 14px;background:#f6f6f4;border:1px solid #e2e2dd;border-radius:6px;font-size:14px;line-height:1.4;color:#333;">\n    <strong>Sample shipping:</strong> orders of <strong>10 samples or fewer ship free</strong> (no tracking). Sample orders <strong>over 10 units</strong> include a shipping charge.\n  </p>`;
  val=val.replace(anchor, anchor+notice);
}
const put=await fetch(base,{method:'PUT',headers:H,body:JSON.stringify({asset:{key:KEY,value:val}})});
const pj=await put.json();
console.log('PUT status',put.status,'| errors:',JSON.stringify(pj.errors||pj.asset?.key||'ok'));
if(!put.ok){console.log('PUT FAILED — live theme unchanged (backup intact)');process.exit(1);}
// verify asset now contains the notice
const chk=await (await fetch(`${base}?asset[key]=${encodeURIComponent(KEY)}`,{headers:H})).json();
console.log('asset contains notice?', (chk.asset?.value||'').includes('tk-ship-notice'));
// verify the live cart page renders + shows it
for(const url of [`https://designerwallcoverings.com/cart`,`https://${SHOP}/cart`]){
  try{const r=await fetch(url,{redirect:'follow'});const html=await r.text();console.log(`GET ${url} -> ${r.status} | notice visible: ${html.includes('over 10 units')}`);}catch(e){console.log(`GET ${url} err`,e.message);}
}