craftcms / shopify

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

Sync Variant meta fields to the the DB #99

Closed zsavajji closed 6 months ago

zsavajji commented 6 months ago

Hi, I am using the addon on a new project on Craft5, but I'm stuck on a (seemingly) simple problem. Is there no way to get the values of the metafields stored on variants? I'm trying to get color custom codes on items but i'm unable to get such information from Shopify, is this the correct behavior?

Thanks

Additional info

linear[bot] commented 6 months ago

PT-1536 Support for variant metafields?

nfourtythree commented 6 months ago

Hi @zsavajji

Thank you for your message.

Currently the plugin is only syncing the product's meta fields and not currently syncing the variant meta fields.

We can add this as a feature request and look to get it in a future version of the plugin to make this easier.

For a solution for the moment you are still able to get this data, if you are doing this in a twig template you could do the following:

{# Assuming you have already retrieved the product and set it on a `product` variable #}

<strong>{{ product.id }} - {{ product.title }}</strong>
<ul>
  {% for variant in product.getVariants() %}
    <li>
      <div>{{ variant.title }}</div>

      {% cache using key "shopify:variant:metafields:" ~ variant.id %}
        {% set metafields = craft.shopify.api.getAll('Shopify\\Rest\\Admin2023_10\\Metafield', {
          metafield: {
            owner_id: variant.id,
            owner_resource: 'variants',
          }
        }) %}
        {% if metafields|length %}
          {% for field in metafields %}
            <div>{{ field.key }}: {{ field.value }}</div>
          {% endfor %}
        {% endif %}
      {% endcache %}
    </li>
  {% endfor %}
</ul>

Noted that because we are interacting with the Shopify API I have wrapped the call in a cache tag to avoid any instances of calling the Api too much.

Hope this helps and please keep an eye on this issue to see any progress with the feature request.

Thanks!

zsavajji commented 6 months ago

Hi @nfourtythree, thank you for taking time to send me the Twig snippet, but it's not a viable solution for me since I'm working headless with the ElementAPI. I went over to the code and tried to implement a sync function for the variant metafields, it works, but it's not the prettiest solution, since it needs to perform side effects.

I'm now working with my fork, if you could check out my PR for the next version it would be pretty awesome :)

Thanks!

nfourtythree commented 6 months ago

We have just released versions 4.1.0 and 5.1.0 of the Shopify plugin.

In those versions we have added a couple of new settings that allow you to control the syncing of meta fields for both products and variants. The settings can be added to your shopify.php config file.

For variants setting syncVariantMetafields to true and then running the craft shopify/sync/products command will pull down all the data. Due to the way the Shopify API is designed this can be fairly time consuming for large product catalogs so the command line is the best way to sync the data as it helps deal with rate limiting.

Thanks!