gpt4 book ai didi

How to setup order create webhook in shopify?(如何在Shopify中设置订单创建网络挂钩?)

转载 作者:bug小助手 更新时间:2023-10-25 22:42:54 27 4
gpt4 key购买 nike



I have started building an application on shopify-app-remix. I cannot set up an order to create a webhook on this package. I have tried the below code to fire a webhook when an order is made. when i run the application with npm run dev that time in command prompt i can see only "app uninstaller".

我已经开始在Shopify-app-ReMix上构建一个应用程序。我无法设置在此包上创建Web挂钩的订单。我已经尝试了下面的代码,以便在下订单时触发一个网络挂钩。当我在命令提示符下用npm run dev运行应用程序时,我只能看到“app uninstaller”。


In the shopify.server.js file:-

在shop ify.server.js文件中:-


import "@shopify/shopify-app-remix/adapters/node";
import {
AppDistribution,
DeliveryMethod,
shopifyApp,
LATEST_API_VERSION,
} from "@shopify/shopify-app-remix/server";
import { PrismaSessionStorage } from "@shopify/shopify-app-session-storage-prisma";
import { restResources } from "@shopify/shopify-api/rest/admin/2023-07";

import prisma from "./db.server";

const shopify = shopifyApp({
apiKey: process.env.SHOPIFY_API_KEY,
apiSecretKey: process.env.SHOPIFY_API_SECRET || "",
apiVersion: LATEST_API_VERSION,
scopes: process.env.SCOPES?.split(","),
appUrl: process.env.SHOPIFY_APP_URL || "",
authPathPrefix: "/auth",
sessionStorage: new PrismaSessionStorage(prisma),
distribution: AppDistribution.AppStore,
restResources,
webhooks: {
APP_UNINSTALLED: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: "/webhooks",
},
ORDERS_CREATE: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: "/webhooks",
},
},
hooks: {
afterAuth: async ({ session }) => {
shopify.registerWebhooks({ session });
},
},
...(process.env.SHOP_CUSTOM_DOMAIN
? { customShopDomains: [process.env.SHOP_CUSTOM_DOMAIN] }
: {}),
});

export default shopify;
export const apiVersion = LATEST_API_VERSION;
export const addDocumentResponseHeaders = shopify.addDocumentResponseHeaders;
export const authenticate = shopify.authenticate;
export const unauthenticated = shopify.unauthenticated;
export const login = shopify.login;
export const registerWebhooks = shopify.registerWebhooks;
export const sessionStorage = shopify.sessionStorage;

In the webhooks.jsx file:-

在webhooks.jsx文件中:-


import { authenticate } from "../shopify.server";
import db from "../db.server";

export const action = async ({ request }) => {
const { topic, shop, session } = await authenticate.webhook(request);

switch (topic) {
case "APP_UNINSTALLED":
if (session) {
await db.session.deleteMany({ where: { shop } });
}
break;
case "CUSTOMERS_DATA_REQUEST":
case "CUSTOMERS_REDACT":
case "SHOP_REDACT":
case "ORDERS_CREATE": console.log(' Order create webhook run'); break;
default:
throw new Response("Unhandled webhook topic", { status: 404 });
}

throw new Response();
};

更多回答
优秀答案推荐

Assuming you are using the latest Shopify Boiler plate produced by the CLI.

假设您使用的是CLI生产的最新Shopify锅炉板。


Additional webhooks are created on the initial install. Even if it was correctly installed, relying on this isn't advised.

在初始安装时会创建其他Web挂钩。即使它已正确安装,也不建议依赖于此。


In the long run, you'll want to develop methods to add the webhooks yourself.

从长远来看,您需要自己开发方法来添加Web挂钩。



  • What if your app grows and you require more webhooks

  • What if your app crashed and Shopify removed your webhooks.


Here is the official documentation https://github.com/Shopify/shopify-api-js/blob/main/docs/guides/webhooks.md

以下是官方文档https://github.com/Shopify/shopify-api-js/blob/main/docs/guides/webhooks.md


更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com