shopwp / wp-shopify

🎉 Sell and build custom Shopify experiences on WordPress.
https://wpshop.io
GNU General Public License v2.0
45 stars 9 forks source link

Product metafields and tags data ouput #121

Open designbureau opened 3 years ago

designbureau commented 3 years ago

It would be really useful to have outputs of product tags and product metafields. Especially with Shopify's new metafields editor. Any suggestions how best to go about including this data with the product sync?

arobbins commented 3 years ago

@designbureau Hey there,

I'm working on adding these things for version 4.0 of the plugin (coming later this summer). I haven't figured out the API for it yet, but I want to try making it as simple as possible.

designbureau commented 3 years ago

Interestingly it looks like you need to expose metafields via a graphql mutation, and you need to specify the namespace of the fields. They're not automatically available. Still doable.

arobbins commented 3 years ago

@designbureau Yeah that's part of the trouble. From what I understand you need to manually "whitelist" which metafields you want access to. So I'm trying to figure out how to enable this process in an easy to use way.

Open to suggestions!

designbureau commented 3 years ago

@arobbins Yeah exactly. And for good reason I suppose, its a security issue.

It looks like the metafields for products are exposed at the /admin/products/#{id}/metafields.json endpoint in the admin API. You could potentially use that to build a reference table, but you could hit some rate limiting.

The sensible thing would probably be to keep it manual, but devise a simple interface for building the mutation query variables to whitelist specific metafields.

The mutation query is generic, only the variables need to be set manually, of which we need the "namespace" and "key" keys. Hypothetically you could build a form with repeatable fields to input the namespace and key values to build the graphql variables json payload, then push that into the mutation query.

Generic GraphQL Mutation Query: mutation($input: MetafieldStorefrontVisibilityInput!) { metafieldStorefrontVisibilityCreate( input: $input ) { metafieldStorefrontVisibility { id } userErrors { field message } } }

GraphQL Variables: { "input": { "namespace": "your_namespace_goes_here", "key": "your_key_goes_here", "ownerType": "PRODUCT" } }