Rails & Shopify: DelayedJob (non WebHook) - Authenticate and create session

Solved

Rails & Shopify: DelayedJob (non WebHook) - Authenticate and create session

ToPre
Shopify Partner
6 1 1

Hey guys,

 

I have a job which runs every day at midnight and retrieves information from etsy. 

 

I need to update my products in shopify but I dont know how to create a valid shopsession.

 

I could do it with 

 

 

 

shop = Shop.find_by(shopify_domain: shop_domain)

 

 

and then shop.with_shopify_session but i still need the shop_domain for this. 
 
Do you have any tips?
 
Cheers!
 
Accepted Solution (1)

Liam
Community Manager
3108 340 871

This is an accepted solution.

Hello,

To create a valid Shopify session, you'll indeed need the shop_domain. The shop_domain is a unique identifier for each shop and is necessary to establish a session.

 

If you're running a job needs to update products on Shopify, you would likely have this information stored somewhere in your database. When a store installs your app, should be storing the shop_domain in your database.

 

Here's a general way you could use the shop_domain to establish a Shopify session in a job:

Shop.find_each do |shop|
  shop.with_shopify_session do
    # Your code to update products on Shopify goes here
  end
end

In this example, Shop.find_each is iterating over each Shop record in your database, and for each shop, it establishes a Shopify session and runs the code within the block. If for some reason you don't have the shop_domain stored, you would need to adjust your app installation process to save this information.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 2 (2)

Liam
Community Manager
3108 340 871

This is an accepted solution.

Hello,

To create a valid Shopify session, you'll indeed need the shop_domain. The shop_domain is a unique identifier for each shop and is necessary to establish a session.

 

If you're running a job needs to update products on Shopify, you would likely have this information stored somewhere in your database. When a store installs your app, should be storing the shop_domain in your database.

 

Here's a general way you could use the shop_domain to establish a Shopify session in a job:

Shop.find_each do |shop|
  shop.with_shopify_session do
    # Your code to update products on Shopify goes here
  end
end

In this example, Shop.find_each is iterating over each Shop record in your database, and for each shop, it establishes a Shopify session and runs the code within the block. If for some reason you don't have the shop_domain stored, you would need to adjust your app installation process to save this information.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

ToPre
Shopify Partner
6 1 1

Thank you das helps me a lot 🙂