Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Not receiving webhook on deleting a product

Not receiving webhook on deleting a product

akash2
Visitor
2 0 0

Hello All,

 

I have this code in webhook_handler.js working for PRODUCTS_CREATE but not for PRODUCTS_DELETE

import { DeliveryMethod } from "@shopify/shopify-api"

export default {
  PRODUCTS_CREATE: {
    deliveryMethod: DeliveryMethod.Http,
    callbackUrl: "/api/webhooks",
    callback: async (topic, shop, body, webhookId) => {
      const payload = { body, webhookId, topic, shop }
	  console.log(payload)
      fetch(`${BASE_URL_V2}/shop/none/products/webhook`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(payload),
      }).then((res) => res.json())
	},
  },

   PRODUCTS_DELETE: {
    deliveryMethod: DeliveryMethod.Http,
    callbackUrl: "/api/webhooks",
    callback: async (topic, shop, body, webhookId) => {
      const payload = { body, webhookId, topic, shop }
      console.log(payload)
      fetch(`${BASE_URL_V2}/shop/delete_product`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(payload),
      }).then((res) => res.json())
    },
  },
 }

 

Have following code in index.js:

import webhookHandlers from "./webhook-handlers.js";

const app = express();

app.post(
    shopify.config.webhooks.path,
    express.text({ type: '*/*', limit: '30MB' }),
    shopify.processWebhooks({
        webhookHandlers,
    })
)

On my app, I am not getting any logs for PRODUCTS_DELETE, however it works perfectly fine for PRODUCTS_CREATE. Can someone help on what mistake I am doing here and what needs to be done to have webhook for PRODUCTS_DELETE working.

Replies 2 (2)

ETXElectronics
Shopify Partner
4 1 1

My suggestion would be to add some console.logs to see what is going on. I quickly added some logs you might try and see if you get any useful data.

PRODUCTS_DELETE: {
  deliveryMethod: DeliveryMethod.Http,
  callbackUrl: "/api/webhooks",
  callback: async (topic, shop, body, webhookId) => {
    console.log(`Received ${topic} webhook from ${shop}`);
    const payload = { body, webhookId, topic, shop };
    console.log('Payload:', payload);
    
    try {
      const response = await fetch(`${BASE_URL_V2}/shop/delete_product`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(payload),
      });
      
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
      
      const result = await response.json();
      console.log('Fetch result:', result);
    } catch (error) {
      console.error('Error in PRODUCTS_DELETE webhook:', error);
    }
  },
},
app.post(
  shopify.config.webhooks.path,
  express.text({ type: '*/*', limit: '30MB' }),
  (req, res, next) => {
    console.log('Received webhook:', req.headers['x-shopify-topic']);
    next();
  },
  shopify.processWebhooks({
    webhookHandlers,
  })
);
akash2
Visitor
2 0 0

Thanks for replying! Have added the suggested logs and got following error for products/delete webhook. However, webhooks for products/create and products/update is working fine. I tried finding solution on community but did not get any working solution. Could you please suggest.

2024-08-01T06:11:18.501 app[08069e9b692968] atl [info] Received webhook: products/delete

2024-08-01T06:11:18.502 app[08069e9b692968] atl [info] [shopify-api/INFO] Receiving webhook request

2024-08-01T06:11:18.509 app[08069e9b692968] atl [info] [shopify-app/ERROR] Failed to process webhook: Error: Could not validate request HMAC