Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering sync based on sales channel #19

Closed
dnlmzw opened this issue Jun 8, 2021 · 9 comments
Closed

Filtering sync based on sales channel #19

dnlmzw opened this issue Jun 8, 2021 · 9 comments
Labels
enhancement New feature or request

Comments

@dnlmzw
Copy link

dnlmzw commented Jun 8, 2021

I have a need to only sync products from Shopify that have been checked off in the "Sales channels" for my headless storefront. At the moment, it is currently being ignored.

Skærmbillede 2021-06-08 kl  10 16 59

@ndimatteo ndimatteo added the enhancement New feature or request label Jun 8, 2021
@ndimatteo
Copy link
Owner

ndimatteo commented Jun 8, 2021

Hey there @dnlmzw!

All webhooks will still fire when an update is made regardless of what channel's they're on.

If you wanted to check that the product that updated is part of your Headless storefront channel, you'd need to add an additional check in the product sync function. Something like this:

// Check if the proudct exists in this sales channel (optional)
const shopifyChannelProduct = await axios({
  url: `https://${process.env.SHOPIFY_STORE_ID}.myshopify.com/admin/product_listings/${id}.json`,
  method: 'GET',
  headers: shopifyConfig,
})

If you were to call that and check that it successfully returns the product you'd know it is part of the headless storefront channel, and can bail from the rest of the sync if it's not!

I'd suggest adding this additional check here: https://github.com/ndimatteo/HULL/blob/main/pages/api/shopify/product-update.js#L172

I will note: there's a bit more involved if you wanted to also handle keeping that tracked on subsequent channel changes to previously sync'd products. Simply checking here and bailing won't help if a product was already sync'd and now needs to be essentially "removed" from Sanity.

I hope that helps point you in the right direction!

@dnlmzw
Copy link
Author

dnlmzw commented Jun 9, 2021

Thank you so much! I will test it out to see if I can get it to work.

@dnlmzw
Copy link
Author

dnlmzw commented Jun 11, 2021

I ran into an issue when using the wasDeleted flag on products. I believe it might impact others as well, even if they're not trying to achieve what I'm doing in the issue above.

@ndimatteo I wanted to share this, so that you could potentially validate:

const { site, page } = data

When the wasDeleted flag is set to true the page is null, which breaks static site generation on Vercel.

I suspect this might be a general issue, since I was able to fix it by adding && wasDeleted != true to the @lib/api.js here:

HULL/lib/api.js

Line 339 in e7f5ec9

`*[_type == "${doc}"]{ "slug": slug.current }`

So that the line becomes: *[_type == "${doc}" && wasDeleted != true]{ "slug": slug.current }

I haven't tested out cases where Shopify deletes products, which is why I'm not sure if this is a general issue, or only me who is experiencing this, but wanted to let you know regardless as I thought you might easily weed out if this was a general flaw.

@ndimatteo Let me know if you want me to add a seperate ticket for this (or alternatively create a PR)

@ndimatteo ndimatteo pinned this issue Aug 4, 2021
@ndimatteo
Copy link
Owner

Closing this issue, but keeping it pinned for those who need this feature 🤘

@ndimatteo
Copy link
Owner

I ran into an issue when using the wasDeleted flag on products. I believe it might impact others as well, even if they're not trying to achieve what I'm doing in the issue above.

@ndimatteo I wanted to share this, so that you could potentially validate:

const { site, page } = data

When the wasDeleted flag is set to true the page is null, which breaks static site generation on Vercel.

I suspect this might be a general issue, since I was able to fix it by adding && wasDeleted != true to the @lib/api.js here:

HULL/lib/api.js

Line 339 in e7f5ec9

`*[_type == "${doc}"]{ "slug": slug.current }`

So that the line becomes: *[_type == "${doc}" && wasDeleted != true]{ "slug": slug.current }

I haven't tested out cases where Shopify deletes products, which is why I'm not sure if this is a general issue, or only me who is experiencing this, but wanted to let you know regardless as I thought you might easily weed out if this was a general flaw.

@ndimatteo Let me know if you want me to add a seperate ticket for this (or alternatively create a PR)

@dnlmzw good catch with this, I'll add that additional check to the GROQ function to account for this, will have that pushed in an update later this week!

@s1nny0ur
Copy link

this check doesnt seem to prevent a product from syncing any help would bemuch appreciated. On my dev store it did behave as expected but on a different live shopify it seems to do nothing.

`console.log('Checking for previous sync data...')

// Setup our Shopify connection
const shopifyConfig = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': process.env.SHOPIFY_API_PASSWORD,
}

// Check if the product exists in the channel
const shopifyChannelProduct = await axios({
url: https://${process.env.SHOPIFY_STORE_ID}.myshopify.com/admin/product_listings/${id}.json,
method: 'GET',
headers: shopifyConfig,
}).catch(
function (error) {
console.info('This Product is not in the sales channel for this app!', error)

return res

}
)
console.log('Checking sales channel sync data...', id)

// Check Channel found
if (!shopifyChannelProduct) {
console.warn('NOT HERE')
return res

// No changes found
} else {
console.info('Product is here continue to sync.')
`

@ndimatteo
Copy link
Owner

Hey there @highsandlows!

I'm not sure I follow, but if you're wanting to block a product from sync'ing into Sanity based on if the headless sales channel is checked or not, the check outlined here should work: #19 (comment)

The code you pasted is unformated and not really helpful in debugging your issue, but if it's working on one store account and not another, that should tell you that the issue is likely elsewhere with the implementation on the store that it's not working on.

If you have more details to share let me know, happy to take a look!

@s1nny0ur
Copy link

s1nny0ur commented Dec 17, 2021

Apologies @ndimatteo , My question relates to your example below and how to log it/check it is recognising the sales channel.

// Check if the proudct exists in this sales channel (optional)
const shopifyChannelProduct = await axios({
  url: `https://${process.env.SHOPIFY_STORE_ID}.myshopify.com/admin/product_listings/${id}.json`,
  method: 'GET',
  headers: shopifyConfig,
})

If you were to call that and check that it successfully returns the product you'd know it is part of the headless storefront channel, and can bail from the rest of the sync if it's not!

I have added some additional logs like below

 Version: $LATEST
2021-12-17T08:15:34.001Z		INFO	Sync triggered for product: GARMENT-DYED HEAVYWEIGHT SHORT - BLACK (id: 7075701293233)
2021-12-17T08:15:34.012Z		INFO	Checking for previous sync data...
2021-12-17T08:15:34.266Z		INFO	Checking sales channel sync data... 7075701293233
2021-12-17T08:15:34.266Z		INFO	Product is here continue to sync.
2021-12-17T08:15:34.441Z		INFO	Previous sync found, comparing differences...
2021-12-17T08:15:34.443Z		INFO	No differences found, sync complete!
END Request

Would the following work?

// Check if the product exists in the channel
const shopifyChannelProduct = await axios({
  url: `https://${process.env.SHOPIFY_STORE_ID}.myshopify.com/admin/product_listings/${id}.json`,
  method: 'GET',
  headers: shopifyConfig,
}).catch(
  function (error) {
    console.info('This Product is not in the sales channel for this app!', error)

    return res
        
       
  }
)
console.log('Checking sales channel sync data...', id)


 // Check Channel found
 if (!shopifyChannelProduct) {
  console.warn('NOT HERE')
  return res
   
  
  // No changes found
} else {
  console.info('Product is here continue to sync.')
  
}

Apologies for the bad formatting before was trying to post on the phone app :/

@ndimatteo
Copy link
Owner

No problem at all @highsandlows, thanks for elaborating!

So the issue is two-fold:

  1. Your catch() only gets hit if an exception is thrown. If the product wasn't part of the sales channel Shopify doesn't throw an exception, it returns data if it can't find the product (still resulting in a successful GET fetch)
  2. Your if/else check is only checking for the existence of shopifyChannelProduct which isn't enough. Much like above, even when the product isn't part of the sales channel, Shopify still returns data.

When a product isn't part of a sales channel, shopifyChannelProduct will return this:

{
    "errors": "Not Found"
}

And when it is part of a sales channel, it returns something like this:

{
  "product_listing": {
    "product_id": 1111111,

    // other product fields....
  }
}

You'll need to use this data to do your check.

I hope that makes more sense, let me know if you have more questions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants