Class: Gemgento::Adapter::Shopify::Customer

Inherits:
Object
  • Object
show all
Defined in:
app/models/gemgento/adapter/shopify/customer.rb

Class Method Summary collapse

Class Method Details

.create_addresses(shopify_customer, user, skip_existing) ⇒ Void

Import all addresses for a shopify customer.

Parameters:

  • shopify_customer (ShopifyAPI::Customer)
  • user (Gemgento::User)
  • skip_existing (Boolean)

Returns:

  • (Void)


59
60
61
62
63
# File 'app/models/gemgento/adapter/shopify/customer.rb', line 59

def self.create_addresses(shopify_customer, user, skip_existing)
  shopify_customer.addresses.each do |shopify_address|
    Gemgento::Adapter::Shopify::Address.import(shopify_address, user, skip_existing)
  end
end

.create_user(shopify_customer, skip_existing) ⇒ Gemgento::User

Create a user from Shopify customer.

Parameters:

  • shopify_customer (ShopifyAPI::Customer)
  • skip_existing (Boolean)

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/gemgento/adapter/shopify/customer.rb', line 32

def self.create_user(shopify_customer, skip_existing)
  if shopify_adapter = Gemgento::Adapter::ShopifyAdapter.find_by_shopify_model(shopify_customer)
    user = shopify_adapter.gemgento_model
    return user if skip_existing && !user.magento_id.nil?
  else
    user = Gemgento::User.new
  end

  user.email = shopify_customer.email
  user.first_name = shopify_customer.first_name
  user.last_name = shopify_customer.last_name
  user.user_group = Gemgento::UserGroup.find_by(code: 'General')
  user.stores = Gemgento::Store.all
  user.sync_needed = true
  user.save validate: false

  Gemgento::Adapter::ShopifyAdapter.create_association(user, shopify_customer)

  return user
end

.import(skip_existing = false) ⇒ Void

Import all customers from Shopify.

Parameters:

  • skip_existing (Boolean) (defaults to: false)

Returns:

  • (Void)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/gemgento/adapter/shopify/customer.rb', line 10

def self.import(skip_existing = false)
  page = 1
  ShopifyAPI::Base.site = Gemgento::Adapter::ShopifyAdapter.api_url

  shopify_customers = ShopifyAPI::Customer.where(limit: 250, page: page)

  while shopify_customers.any?
    shopify_customers.each do |customer|
      user = create_user(customer, skip_existing)
      create_addresses(customer, user, skip_existing)
    end

    page = page + 1
    shopify_customers = ShopifyAPI::Customer.where(limit: 250, page: page)
  end
end