statamic-rad-pack / shopify

Integrate your Shopify products into Statamic using the Admin API.
https://statamic.com/addons/rad-pack/shopify
Other
8 stars 5 forks source link

Product status change on Shopify isn't showing in Statamic until the stache is cleared. #252

Open sunscreem opened 2 weeks ago

sunscreem commented 2 weeks ago

Description

We've noticed that although most product changes at Shopify, such as a name or description change, are updated via the webhooks almost instantly, this doesn't appear to be the case for the published status.

Is this a Statamic issue?

Steps to reproduce

  1. Change a product status from published to draft
  2. Webhook fires correct - product.yaml show the change
  3. Product published remains unchanged until the Statche is cleared.

Environment

Environment Application Name: X Laravel Version: 11.30.0 PHP Version: 8.2.25 Composer Version: 2.3.2 Environment: production Debug Mode: OFF URL: X Maintenance Mode: OFF Timezone: Europe/London Locale: en_GB

Cache Config: CACHED Events: CACHED Routes: CACHED Views: CACHED

Drivers Broadcasting: log Cache: file Database: mysql Logs: stack / single, bugsnag Mail: smtp Queue: redis Session: file

Statamic Addons: 7 Sites: 1 Stache Watcher: Disabled Static Caching: half Version: 5.36.0 PRO

Statamic Addons statamic-rad-pack/shopify: 4.0.6

ryanmitchell commented 2 weeks ago

Is this a future dated product?

sunscreem commented 2 weeks ago

Hey @ryanmitchell I don't think we use dates at all on this project - but let me check.

sunscreem commented 2 weeks ago

Hey @ryanmitchell Double checked and no dates are used. I can email you over a quick video to show the issue if you would like?

ryanmitchell commented 2 weeks ago

On fresh installs your collection should be dated and publish dates are considered. I’m wondering if the line below needs to be GTE not GT :

https://github.com/statamic-rad-pack/shopify/blob/695be9f1267b32a498ee3f1509ddf2328758e9a8/src/Jobs/ImportSingleProductJob.php#L222

sunscreem commented 2 weeks ago

@ryanmitchell Thanks - I've tried disabling dates on the product collections and still seeing the same issue. Note that the published status in the control panel also shows the incorrect status until I clear the stache.

ryanmitchell commented 2 weeks ago

If you just resave the product rather than clearing the stache does it resolve it?

sunscreem commented 2 weeks ago

Thanks Ryan. If I save from the control panel it will overwrite the data on the product so here's how I saved it in tinker. You will see I clear the stache and everything is working as expected:

image

ryanmitchell commented 2 weeks ago

Weird.

If you dd or log here https://github.com/statamic-rad-pack/shopify/blob/695be9f1267b32a498ee3f1509ddf2328758e9a8/src/Jobs/ImportSingleProductJob.php#L227 what status is being set? I wonder if we also need to set $entry->set('published', $published) but I didnt think that was needed.

sunscreem commented 2 weeks ago

@ryanmitchell After a bunch of experimenting it does seem that with the STATAMIC_STACHE_WATCHER set to false that data changes on the products trigger an update to the stache, but the published status change doesn't. I'm not sure why.

To work around it I've tried adding:

Stache::store('entries::products')->clear();
Stache::store('entries::products')->warm();

Just here and that seems to work.

What are your thoughts on using that as a workaround? I'm tempted to raise this as a bug on the Statamic repo as I can't find any discussion of it anywhere and which is odd.

ryanmitchell commented 2 weeks ago

That shouldn’t be necessary. Saving the item should be enough. Did you try setting published in the data like I suggested?

sunscreem commented 2 weeks ago

I did yes - it didn't change anything - sorry I should have said earlier.

ryanmitchell commented 2 weeks ago

The whole thing seems odd - definitely not something we're seeing on our installs. Are you running a queue?

sunscreem commented 2 weeks ago

No locally in testing no. I can try to recreate on a clean install that that helps?

ryanmitchell commented 2 weeks ago

You could listen for the ProductCreate/Delete/Update events and call the stache:clear in there.

sunscreem commented 2 weeks ago

Yeah - that's a plan. Tempted to try to recreate on a clean install anyway. Can you leave this open for now, I'll do that tomorrow if I get chance?

sunscreem commented 1 day ago

Hey @ryanmitchell I'm finally getting the bottom of this.

https://github.com/statamic-rad-pack/shopify/blob/2ff75c2b1f0fbe84f7283ba877078b6d114b7d32/src/Jobs/ImportSingleProductJob.php#L200-L232

Would you mind explaining what this code is doing?

ryanmitchell commented 1 day ago

It’s syncs the publication status and dates from shopifys online store channel

sunscreem commented 1 day ago

Cool thanks. Does that work with the publish status set in the $data array above?

I'm finding if I put a quickly addin $entry->published(true) - or $entry->published(false) just before the $entry-save() on 238 it fixing my stache issue.

ryanmitchell commented 1 day ago

If you have an ‘online store’ channel in shopify - make sure you have the product available on it.

sunscreem commented 1 day ago

Thanks Ryan. I'll do a little more digging.