craftcms / shopify

Synchronize and extend product data from your Shopify storefront.
MIT License
45 stars 25 forks source link

$event->metafields is always an empty array when syncing *all* products #73

Closed johndwells closed 11 months ago

johndwells commented 1 year ago

Description

We are trying to skip the synchronization of certain products based on their metafields. To accomplish this we are following the readme's example: https://github.com/craftcms/shopify#craftshopifyservicesproductsevent_before_synchronize_product

This code works fine when an individual product is being synchronized due to a webhook event; however if we run the CLI command shopify/sync/all, or sync all products via the CP Utility, the contents of $event->metafields is always an empty array.

This is because when the Products::syncAllProducts() method is called, it pushes the fetching of metadata into a queue of its own:

foreach ($products as $product) {
  $this->createOrUpdateProduct($product);
  Craft::$app->getQueue()->push(new UpdateProductMetadata([
    'description' => Craft::t('shopify', 'Updating product metadata for “{title}”', [
      'title' => $product->title,
    ]),
    'shopifyProductId' => $product->id,
  ]));
}

https://github.com/craftcms/shopify/blob/develop/src/services/Products.php#L66-L74

I can appreciate why it's been done this way, but I don't see an obvious way to account for this in our event handler code, other than making our own call out to the API.

Any thoughts?

lukeholder commented 1 year ago

Yeah, this is tricky because the meta data is synced after product sync.

Shopify has the concept of sales channels, and a sales channel for your app integration was made. Could you just turn off the product for the Craft sales channel instead of using Meta?

johndwells commented 1 year ago

Shopify has the concept of sales channels, and a sales channel for your app integration was made. Could you just turn off the product for the Craft sales channel instead of using Meta?

@lukeholder Interesting. Right now all products come back regardless of what "channel" they may be assigned to. But you're suggesting that shouldn't happen? How do we associate our app with a sales channel?

lukeholder commented 11 months ago

This is fixed for the next major release. I will let you know once it is released.

lukeholder commented 11 months ago

4.0.0 is now out. Make sure to read the changelog item to learn what you need to do to upgrade. The readme also contains instructions.