Sunday, 7 May 2017

Fetch products from Shopify and create or update metafields with key value of each product with Shopify api's.

Sometime, it becomes essentials to add a customized metafield in product so,we can add some functionality with the help of these metafields.
In this blog, we will fetch products from a store of shopify and create our customized metafield in which we will add pdf file url for each product.

 ############ Required Dependencies ##############


    require 'rubygems'

    require 'shopify_api'



    ############ Shopify Credentials ################


    API_KEY = '846bef1--------------------------'

    PASSWORD = '2e0eed--------------------------'

    SHOPNAME = 'store_name_of_shopify-----------'

    shop_url = "https://#{API_KEY}:#{PASSWORD}@#{SHOPNAME}.myshopify.com/admin"


    ##### Fetch Products and create metafields ######   

    begin

      ShopifyAPI::Base.site = shop_url

      products = ShopifyAPI::Product.all

      products.each do |product|

        metafield = ShopifyAPI::Metafield.new(key:'pdf_url_s3',

                                          namespace: "headerlabs",

                                          value: 'http://docs.aws.amazon.com/AmazonS3/latest/dev/s3-dg.pdf',

                                          value_type: "string")

        product.add_metafield(metafield)

      end

    rescue => e

      puts e.inspect

    end

Now, you can access these metafields in template of product in shopify liquid file

    {% assign headerlabs = product.metafields.headerlabs %}

    {% assign key = 'pdf_url_s3' %}

    <a href={{ headerlabs.pdf_url_s3}} target="_blank" > 

7 comments:

  1. It is nice blog Thank you porovide importent information and i am searching for same information to save my timeRuby on Rails Online Training Hyderabad

    ReplyDelete
  2. Hire Shopify developers from GEEKS PER HOUR who are adept at Shopify development trends. Our Shopify experts provide consistent and cost-effective solutions to fill the gaps in your Shopify project while ensuring complete transparency and control.

    ReplyDelete
  3. Recommendation on the structure and format of the store according to the target audience, characterization and objectives of the store. shopify

    ReplyDelete
  4. I am thankful to this blog for assisting me. I added some specified clues which are really important for me to use them in my writing skill. Really helpful stuff made by this blog. onlineshop agency

    ReplyDelete
  5. Thanks for sharing this informative article on Shopify app development with practical examples. If you have any requirement to Hire Shopify Developers for your project. You can contact us for more information.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. It is nice blog Thank you for providing important information. Shopify Plus Dеvеlopmеnt Services

    ReplyDelete