Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Last active April 7, 2025 11:36
Show Gist options
  • Save ihorduchenko/6a86a5207e82265176f9590e761851aa to your computer and use it in GitHub Desktop.
Save ihorduchenko/6a86a5207e82265176f9590e761851aa to your computer and use it in GitHub Desktop.
Shopify App troubleshoot
import { useEffect, useState } from "react";
import {
useCartLineTarget,
Text,
reactExtension,
useApplyCartLinesChange,
} from "@shopify/ui-extensions-react/checkout";
// Entry point
export default reactExtension("purchase.checkout.cart-line-item.render-after", () => <App />);
function App() {
const triggerWord = "Rezeptgebühr";
const cartLineTarget = useCartLineTarget();
const applyCartLinesChange = useApplyCartLinesChange();
const title = cartLineTarget?.merchandise?.product?.title ?? "";
const prodTitle = cartLineTarget?.merchandise?.title ?? "";
const quantity = cartLineTarget?.quantity ?? 0;
const [titleHasTrigger, setTitleHasTrigger] = useState(false);
const [wasAdjusted, setWasAdjusted] = useState(false);
function adjustQuantity(qty) {
if (qty < 5) return 5;
return Math.floor(qty / 5) * 5;
}
useEffect(() => {
async function handleQuantityAdjustment() {
const hasTrigger = title.toLowerCase().includes(triggerWord.toLowerCase());
setTitleHasTrigger(hasTrigger);
if (!hasTrigger && quantity % 5 !== 0) {
const newQuantity = adjustQuantity(quantity);
try {
await applyCartLinesChange({
type: "updateCartLine",
updates: [
{
id: String(cartLineTarget.id), // Ensure ID is a string
quantity: newQuantity,
},
],
});
setWasAdjusted(true);
} catch (error) {
}
}
}
handleQuantityAdjustment();
}, [title, quantity, triggerWord]);
return (
<Text>
{`Adjust applied: ${wasAdjusted ? "Yes" : "No"} | `}
{`CartLineTarget: ${JSON.stringify(cartLineTarget)} | `}
</Text>
);
}
# Learn more about configuring your checkout UI extension:
# https://shopify.dev/api/checkout-extensions/checkout/configuration
# The version of APIs your extension will receive. Learn more:
# https://shopify.dev/docs/api/usage/versioning
api_version = "2025-01"
[[extensions]]
name = "Quantity auto-adjustment"
handle = "quantity-auto-adjustment"
type = "ui_extension"
# Controls where in Shopify your extension will be injected,
# and the file that contains your extension’s source code. Learn more:
# https://shopify.dev/docs/api/checkout-ui-extensions/unstable/extension-targets-overview
[[extensions.targeting]]
module = "./src/Checkout.jsx"
target = "purchase.checkout.cart-line-item.render-after"
# Loads metafields on checkout resources, including the cart,
# products, customers, and more. Learn more:
# https://shopify.dev/docs/api/checkout-ui-extensions/unstable/configuration#metafields
# [[extensions.targeting.metafields]]
# namespace = "custom"
# key = "standard_selling_unit"
# [[extensions.metafields]]
# namespace = "custom"
# key = "standard_selling_unit"
[extensions.capabilities]
# Gives your extension access to directly query Shopify’s storefront API.
# https://shopify.dev/docs/api/checkout-ui-extensions/unstable/configuration#api-access
api_access = true
# Gives your extension access to make external network calls, using the
# JavaScript `fetch()` API. Learn more:
# https://shopify.dev/docs/api/checkout-ui-extensions/unstable/configuration#network-access
# network_access = true
# Defines settings that will be collected from merchants installing
# your extension. Learn more:
# https://shopify.dev/docs/api/checkout-ui-extensions/unstable/configuration#settings-definition
# [extensions.settings]
# [[extensions.settings.fields]]
# key = "excluded_products_title_trigger"
# type = "single_line_text_field"
# name = "Excluded products trigger word"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment