feat: cross-theme cart placement for the storefront widget
Some checks failed
CI / Lint, Unit & Integration Tests (push) Has been cancelled
Some checks failed
CI / Lint, Unit & Integration Tests (push) Has been cancelled
Many themes only expose "Add section" for the cart's checkout area, not "Add block" next to the actual Checkout button — Shopify gives apps no supported way to inject a block into an arbitrary spot in a theme's own section markup, so a manually-placed app block can only ever land as a disconnected standalone section on those themes. Add a JS-based fallback via the app embed (already loaded site-wide, independent of block placement): app-embed.liquid now carries the same config settings as the block plus an "auto-place on cart page" toggle (default on), emitted as an inert <template> with the widget's config as data-* attributes. datetime-widget.ts's maybeAutoPlaceOnCart() finds the theme's own Checkout button by CSS selector (a few common patterns, most specific first) and inserts a live widget immediately before it — skipped entirely if a block-placed widget already exists on the page, or no Checkout button can be found by any known selector. Also fix shopify.app.toml: automatically_update_urls_on_dev was left on after the app got a real, permanent production domain (metatron-delivery.thedomainnest.com) — leaving it on meant a future local `shopify app dev` session would silently overwrite the live app's application_url with a temporary Cloudflare tunnel, breaking the deployed app until someone noticed and redeployed with the real URL. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
ba22407c22
commit
1262e0f5ab
@ -6,6 +6,16 @@
|
||||
min-height: 3.5rem; /* reserves space before JS renders content, to avoid layout shift */
|
||||
}
|
||||
|
||||
/* Cart-page auto-injection (datetime-widget.ts's maybeAutoPlaceOnCart) lands
|
||||
the widget as a sibling immediately before the theme's own Checkout
|
||||
button, which can be inside a narrow flex/inline-sized wrapper — force
|
||||
full width and give it breathing room so it doesn't get visually
|
||||
squeezed regardless of that wrapper's own layout rules. */
|
||||
.dd-widget--cart-injected {
|
||||
width: 100%;
|
||||
margin-block: 1rem;
|
||||
}
|
||||
|
||||
.dd-widget__heading {
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -1,10 +1,99 @@
|
||||
{{ 'datetime-widget.css' | asset_url | stylesheet_tag }}
|
||||
<script src="{{ 'datetime-widget.js' | asset_url }}" defer="defer"></script>
|
||||
|
||||
{% comment %}
|
||||
Cross-theme cart placement (PRODUCT_STRATEGY.md competitive parity item):
|
||||
a manually-placed app BLOCK only ever lands wherever the active theme's
|
||||
own section schema declares an `@app` slot — many themes don't expose one
|
||||
next to the cart's Checkout button at all, only "Add section" at the top
|
||||
level, which is what was showing up as a disconnected block. Shopify
|
||||
gives apps no supported way to inject a block into an arbitrary spot in a
|
||||
theme's own section markup, so this app embed (always loaded site-wide,
|
||||
independent of any block placement) instead emits an inert <template>
|
||||
carrying the widget's config as data-* attributes; datetime-widget.js
|
||||
finds the theme's own Checkout button by CSS selector at runtime and
|
||||
inserts a live widget immediately before it. Best-effort — it depends on
|
||||
the theme's Checkout button being findable by one of a few common
|
||||
selectors (see datetime-widget.ts's CHECKOUT_BUTTON_SELECTOR) — but it's
|
||||
the only way to get placement that isn't at the mercy of whether a given
|
||||
theme's cart section happens to support app blocks.
|
||||
{% endcomment %}
|
||||
{% if block.settings.auto_place_cart %}
|
||||
<template
|
||||
id="dd-widget-cart-template"
|
||||
data-heading="{{ block.settings.heading | escape }}"
|
||||
data-show-shipping="{{ block.settings.show_shipping }}"
|
||||
data-show-local-delivery="{{ block.settings.show_local_delivery }}"
|
||||
data-show-pickup="{{ block.settings.show_pickup }}"
|
||||
data-label-shipping="{{ 'widget.method_shipping' | t | escape }}"
|
||||
data-label-local-delivery="{{ 'widget.method_local_delivery' | t | escape }}"
|
||||
data-label-pickup="{{ 'widget.method_pickup' | t | escape }}"
|
||||
data-attr-label-shipping="{{ 'widget.date_label_shipping' | t | escape }}"
|
||||
data-attr-label-local-delivery="{{ 'widget.date_label_local_delivery' | t | escape }}"
|
||||
data-attr-label-pickup="{{ 'widget.date_label_pickup' | t | escape }}"
|
||||
data-label-choose-date="{{ 'widget.choose_date' | t | escape }}"
|
||||
data-label-choose-time="{{ 'widget.choose_time' | t | escape }}"
|
||||
data-label-no-dates="{{ 'widget.no_dates' | t | escape }}"
|
||||
data-label-confirmed="{{ 'widget.confirmed' | t | escape }}"
|
||||
data-label-change="{{ 'widget.change' | t | escape }}"
|
||||
data-label-loading="{{ 'widget.loading' | t | escape }}"
|
||||
data-label-error="{{ 'widget.error' | t | escape }}"
|
||||
data-label-postal-code="{{ 'widget.postal_code' | t | escape }}"
|
||||
data-label-postal-code-submit="{{ 'widget.postal_code_submit' | t | escape }}"
|
||||
data-label-out-of-area="{{ 'widget.out_of_area' | t | escape }}"
|
||||
{% if block.settings.location_id != blank %}data-location-id="{{ block.settings.location_id | escape }}"{% endif %}
|
||||
{% if block.settings.google_maps_api_key != blank %}data-google-maps-api-key="{{ block.settings.google_maps_api_key | escape }}"{% endif %}
|
||||
></template>
|
||||
{% endif %}
|
||||
|
||||
{% schema %}
|
||||
{
|
||||
"name": "t:app_embed.name",
|
||||
"target": "body",
|
||||
"settings": []
|
||||
"settings": [
|
||||
{
|
||||
"type": "checkbox",
|
||||
"id": "auto_place_cart",
|
||||
"label": "t:app_embed.auto_place_cart_label",
|
||||
"info": "t:app_embed.auto_place_cart_info",
|
||||
"default": true
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"id": "heading",
|
||||
"label": "t:app_embed.heading_label",
|
||||
"default": "Choose your delivery date"
|
||||
},
|
||||
{
|
||||
"type": "checkbox",
|
||||
"id": "show_shipping",
|
||||
"label": "t:app_embed.show_shipping_label",
|
||||
"default": true
|
||||
},
|
||||
{
|
||||
"type": "checkbox",
|
||||
"id": "show_local_delivery",
|
||||
"label": "t:app_embed.show_local_delivery_label",
|
||||
"default": true
|
||||
},
|
||||
{
|
||||
"type": "checkbox",
|
||||
"id": "show_pickup",
|
||||
"label": "t:app_embed.show_pickup_label",
|
||||
"default": true
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"id": "location_id",
|
||||
"label": "t:app_embed.location_id_label",
|
||||
"info": "t:app_embed.location_id_info"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"id": "google_maps_api_key",
|
||||
"label": "t:app_embed.google_maps_api_key_label",
|
||||
"info": "t:app_embed.google_maps_api_key_info"
|
||||
}
|
||||
]
|
||||
}
|
||||
{% endschema %}
|
||||
|
||||
@ -1,6 +1,16 @@
|
||||
{
|
||||
"app_embed": {
|
||||
"name": "Delivery Date & Time"
|
||||
"name": "Delivery Date & Time",
|
||||
"auto_place_cart_label": "Show automatically on the cart page",
|
||||
"auto_place_cart_info": "Inserts the picker right before your theme's Checkout button, without needing to add a block in the theme editor. Turn this off if you've already placed the \"Date & time picker\" block manually and don't want it to show twice.",
|
||||
"heading_label": "Heading",
|
||||
"show_shipping_label": "Show Shipping",
|
||||
"show_local_delivery_label": "Show Local Delivery",
|
||||
"show_pickup_label": "Show Pickup",
|
||||
"location_id_label": "Location ID (advanced)",
|
||||
"location_id_info": "Leave blank to use the shop's default location.",
|
||||
"google_maps_api_key_label": "Google Maps API key",
|
||||
"google_maps_api_key_info": "Optional — shows a map for Pickup locations. Restrict this key to your store's domain in Google Cloud Console."
|
||||
},
|
||||
"datetime_picker": {
|
||||
"name": "Date & Time Picker",
|
||||
|
||||
@ -2,14 +2,14 @@
|
||||
|
||||
client_id = "fd503f515942a32aa8b811c1ab8c89d0"
|
||||
name = "Metatron-delivery"
|
||||
# Placeholder — `shopify app dev` overwrites this on Shopify's live app
|
||||
# record automatically each session (via automatically_update_urls_on_dev
|
||||
# below), it does NOT rewrite this file. That auto-update only works
|
||||
# because shopify.web.toml exists at the repo root telling the CLI how to
|
||||
# run this project's Remix app; without it, the CLI has no local dev-server
|
||||
# port to build a tunnel URL from and silently keeps using whatever's here
|
||||
# — which is exactly what caused the embedded admin app to never load
|
||||
# during initial setup (see shopify.web.toml's own comment).
|
||||
# The app's real, permanent production URL — PM2 (ecosystem.config.cjs)
|
||||
# keeps the Remix server running here on the actual deployment server, per
|
||||
# .env's SHOPIFY_APP_URL/PORT on that machine. NOT a placeholder anymore:
|
||||
# automatically_update_urls_on_dev is deliberately OFF (see [build] below)
|
||||
# specifically so a local `shopify app dev` session never overwrites this
|
||||
# with a temporary Cloudflare tunnel URL and breaks the live app for real
|
||||
# usage. Only change this value (and redeploy) if the production domain
|
||||
# itself changes.
|
||||
application_url = "https://metatron-delivery.thedomainnest.com"
|
||||
embedded = true
|
||||
|
||||
@ -105,16 +105,22 @@ api_version = "unstable"
|
||||
uri = "/webhooks/events/placeholder"
|
||||
|
||||
# App proxy so the storefront Theme App Extension can call our backend
|
||||
# without CORS issues (see IMPLEMENTATION_PLAN.md §5.3). `shopify app dev`
|
||||
# points this at your dev tunnel automatically when
|
||||
# automatically_update_urls_on_dev is true (see [build] below); if it
|
||||
# doesn't, set it manually to `<your-tunnel-url>/apps/scheduling` — the
|
||||
# Remix routes it forwards to (apps.scheduling.*.tsx) assume that exact
|
||||
# prefix.
|
||||
# without CORS issues (see IMPLEMENTATION_PLAN.md §5.3). Points at the real
|
||||
# production domain — the Remix routes it forwards to (apps.scheduling.*.tsx)
|
||||
# assume the exact /apps/scheduling prefix below.
|
||||
[app_proxy]
|
||||
url = "https://metatron-delivery.thedomainnest.com/apps/scheduling"
|
||||
subpath = "scheduling"
|
||||
prefix = "apps"
|
||||
|
||||
[build]
|
||||
automatically_update_urls_on_dev = true
|
||||
# Deliberately OFF now that there's a real, permanent production URL above
|
||||
# — if this were true, running `shopify app dev` locally (e.g. to test a
|
||||
# theme/UI extension change) would silently overwrite application_url on
|
||||
# Shopify's live app record with a temporary Cloudflare tunnel, breaking
|
||||
# the deployed app for anyone using it until someone notices and redeploys
|
||||
# with the real URL. Local `shopify app dev` sessions still work fine for
|
||||
# testing extensions with this off; the embedded admin app just won't be
|
||||
# reachable through that session's tunnel (test it against the real
|
||||
# production domain above instead).
|
||||
automatically_update_urls_on_dev = false
|
||||
|
||||
@ -567,11 +567,71 @@ class DateTimeWidget {
|
||||
}
|
||||
}
|
||||
|
||||
// Cross-theme cart placement: a manually-placed app BLOCK only ever lands
|
||||
// wherever the active theme's own section schema happens to declare an
|
||||
// `@app` slot — many themes only expose "Add section" for the cart's
|
||||
// checkout area, not "Add block" next to the actual Checkout button, which
|
||||
// is what shows up as a disconnected standalone section. There's no
|
||||
// Shopify-supported way for an app to inject a block into an arbitrary
|
||||
// spot in a theme's own markup, so instead: the app embed (blocks/
|
||||
// app-embed.liquid, loaded site-wide once merchants enable it, independent
|
||||
// of any block placement) emits an inert <template id="dd-widget-cart-
|
||||
// template"> carrying the widget's config as data-* attributes. If we're
|
||||
// on the cart page, no block-placed widget already exists (avoids a
|
||||
// double render), and a Checkout button can be found by one of these
|
||||
// selectors, clone a live widget in immediately before it. Ordered
|
||||
// roughly most-to-least specific/reliable across common theme markup
|
||||
// patterns; the first match wins.
|
||||
const CHECKOUT_BUTTON_SELECTORS = [
|
||||
'form[action*="/cart"] button[name="checkout"]',
|
||||
'form[action*="/cart"] input[name="checkout"]',
|
||||
'[name="checkout"]',
|
||||
'#checkout',
|
||||
'a[href="/checkout"]',
|
||||
];
|
||||
|
||||
function findCheckoutButton(): HTMLElement | null {
|
||||
for (const selector of CHECKOUT_BUTTON_SELECTORS) {
|
||||
const el = document.querySelector<HTMLElement>(selector);
|
||||
if (el) return el;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function isCartPage(): boolean {
|
||||
// Matches /cart, /cart/, and locale-prefixed variants like /en/cart —
|
||||
// but not /cart/add or similar sub-paths that aren't the cart page itself.
|
||||
return window.location.pathname.replace(/\/+$/, "").endsWith("/cart");
|
||||
}
|
||||
|
||||
function maybeAutoPlaceOnCart() {
|
||||
if (!isCartPage()) return;
|
||||
if (document.querySelector("[data-dd-widget]")) return; // a block is already placed manually — don't double up
|
||||
|
||||
const template = document.getElementById("dd-widget-cart-template");
|
||||
if (!template) return; // merchant turned auto-placement off in the app embed's settings
|
||||
|
||||
const checkoutButton = findCheckoutButton();
|
||||
if (!checkoutButton) return; // couldn't find a safe, theme-agnostic anchor — do nothing rather than guess
|
||||
|
||||
const widget = document.createElement("div");
|
||||
widget.setAttribute("data-dd-widget", "");
|
||||
widget.classList.add("dd-widget--cart-injected");
|
||||
for (const attr of Array.from(template.attributes)) {
|
||||
if (attr.name === "id") continue;
|
||||
widget.setAttribute(attr.name, attr.value);
|
||||
}
|
||||
|
||||
checkoutButton.insertAdjacentElement("beforebegin", widget);
|
||||
new DateTimeWidget(readConfig(widget)).mount();
|
||||
}
|
||||
|
||||
function init() {
|
||||
const roots = document.querySelectorAll<HTMLElement>("[data-dd-widget]");
|
||||
roots.forEach((root) => {
|
||||
new DateTimeWidget(readConfig(root)).mount();
|
||||
});
|
||||
maybeAutoPlaceOnCart();
|
||||
}
|
||||
|
||||
if (document.readyState === "loading") {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user