Join us for an upcoming Shopify Partner webinar on February 27, 2024. Discover the latest Checkout Extensibility features, and deep dive on improvements to Shopify Functions and Web Pixels. Register now for either the 10am EST or 2pm EST sessions.
Solved

How to set metafield with multiple values using Python

B3th3sd4
Shopify Partner
7 0 0

Hello,

 

I am trying to set a metafield on some products, that may have multiple options, like the image attached.

test_screen.png

My code is the following:
    value = ['test','test2']

    metafiled = shopify.Metafield(
        {
            'key': 'product_test',
            'value': value,
            'type': 'list.single_line_text_field',
            'namespace': 'custom',
        }
    )
    product.add_metafield(metafiled)
    product.save()


But this is not working. Documentation has no examples on such case. Any ideas?

Accepted Solutions (2)

MetafieldsGuru
Shopify Partner
157 32 95

This is an accepted solution.

Hi @B3th3sd4 ,


It appears that you're trying to pass a Python array as the metafield value, which is not the best idea since the API would expect all attributes to be strings. Have you tried converting your array to a proper JSON array literal?

Check out Metafields Guru, the #1 ranked metafields app.

Bulk editor | Data import/export | Metafield sets | Browser extension

View solution in original post

JohnBerry
Shopify Staff
6 4 5

This is an accepted solution.

As MetafieldsGuru already helpfully explained, the value should always be passed as a String. In Python, you can use the json.dumps method to convert your Array value to a JSON String.

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 4 (4)

MetafieldsGuru
Shopify Partner
157 32 95

This is an accepted solution.

Hi @B3th3sd4 ,


It appears that you're trying to pass a Python array as the metafield value, which is not the best idea since the API would expect all attributes to be strings. Have you tried converting your array to a proper JSON array literal?

Check out Metafields Guru, the #1 ranked metafields app.

Bulk editor | Data import/export | Metafield sets | Browser extension
B3th3sd4
Shopify Partner
7 0 0

Thank you very much. I should expect that since all the API is JSON oriented.

JohnBerry
Shopify Staff
6 4 5

This is an accepted solution.

As MetafieldsGuru already helpfully explained, the value should always be passed as a String. In Python, you can use the json.dumps method to convert your Array value to a JSON String.

To learn more visit the Shopify Help Center or the Community Blog.

sandeepks23
Shopify Partner
41 0 5

Did you get a solution? If so please add the python code.