Last active
April 7, 2025 11:36
-
-
Save ihorduchenko/6a86a5207e82265176f9590e761851aa to your computer and use it in GitHub Desktop.
Shopify App troubleshoot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://can-doc.de/checkouts/cn/Z2NwLWV1cm9wZS13ZXN0MzowMUpRQzc1VFo2S1hZRjRLMkY1R0hOMVdZTQ?adminUrl=admin.shopify.com&cart_link_id=6xKh1p5o&editedAt=2025-04-07T11%3A17%3A53Z&isPublished=false&profileName=PX+auto+discount+in+checkout+v2&profile_preview_token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJtZWRpY2FsZ3JlZW5zLm15c2hvcGlmeS5jb20iLCJhdWQiOiJtZWRpY2FsZ3JlZW5zLm15c2hvcGlmeS5jb20iLCJuYmYiOjE3NDQwMjUyNjgsImNoZWNrb3V0X3Byb2ZpbGVfaWQiOjYwNzg0NjQzMjUsImNoZWNrb3V0X3Byb2ZpbGVfcHVibGlzaGVkIjpmYWxzZSwiY2hlY2tvdXRfcHJvZmlsZV90eXBfb3NwX2FjdGl2ZSI6dHJ1ZSwiZXhwIjoxNzQ0MDI4ODY4fQ.f4ju3mEBhhVQVPw0-bKUxJz4AiHhJRC1gQKNO0P2h4E |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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