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

Sync Variant meta fields to the the DB #99

Closed
zsavajji opened this issue Mar 28, 2024 · 4 comments
Closed

Sync Variant meta fields to the the DB #99

zsavajji opened this issue Mar 28, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request Plugins → Shopify For Linear sync Plugins For Linear sync

Comments

@zsavajji
Copy link

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

  • Craft version: 5.0.0
  • PHP version: 8.3.1
  • Database driver & version: MariaDB 11.3
@zsavajji zsavajji added bug Something isn't working Plugins For Linear sync Plugins → Shopify For Linear sync labels Mar 28, 2024
Copy link

linear bot commented Mar 28, 2024

@nfourtythree
Copy link
Contributor

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!

@nfourtythree nfourtythree changed the title Support for variant metafields? Sync Variant meta fields to the the DB Mar 28, 2024
@nfourtythree nfourtythree added enhancement New feature or request and removed bug Something isn't working labels Mar 28, 2024
@zsavajji
Copy link
Author

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
Copy link
Contributor

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!

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

Successfully merging a pull request may close this issue.

2 participants