Class: Spree::Store

Inherits:
Object
  • Object
show all
Includes:
FriendlyId, Metadata, Metafields, OrderRouting::HasStrategyPreference, Spree::Security::Stores, Spree::Stores::Channels, Spree::Stores::Markets, Spree::Stores::Setup, TranslatableResource, UserManagement
Defined in:
app/models/spree/store.rb

Constant Summary collapse

TRANSLATABLE_FIELDS =

Translations

%i[name meta_description meta_keywords seo_title customer_support_email
address contact_phone].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserManagement

#add_user, #default_user_role, #remove_user

Methods included from Spree::Stores::Channels

#ensure_default_channel

Methods included from Spree::Stores::Markets

#countries_available_for_checkout, #countries_from_markets, #default_country, #default_country_id, #default_currency, #default_locale, #has_markets?, #market_for_country, #supported_currencies_list, #supported_locales_list

Methods included from Spree::Stores::Setup

#payment_method_setup?, #setup_completed?, #setup_percentage, #setup_task_done?, #setup_tasks_done, #setup_tasks_list, #setup_tasks_total

Methods included from Metadata

#metadata, #metadata=, #public_metadata=

Instance Attribute Details

#default_country_isoObject

Virtual attribute — sets the country for the default market created on store creation. Not persisted on the store itself; only used by the after_create callback.



206
207
208
# File 'app/models/spree/store.rb', line 206

def default_country_iso
  @default_country_iso
end

Class Method Details

.available_localesObject



184
185
186
# File 'app/models/spree/store.rb', line 184

def self.available_locales
  Spree::Store.default&.supported_locales_list || []
end

.current(_url = nil) ⇒ Object

Delegations



166
167
168
# File 'app/models/spree/store.rb', line 166

def self.current(_url = nil)
  Spree::Current.store
end

.defaultObject

Deprecated.

The or_initialize behavior will be removed in Spree 5.5.



171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/models/spree/store.rb', line 171

def self.default
  # workaround for Mobility bug with first_or_initialize
  if where(default: true).any?
    where(default: true).first
  else
    Spree::Deprecation.warn(
      'Spree::Store.default returning a new unpersisted store when no default store exists is deprecated ' \
      'and will be removed in Spree 6.0. Please ensure a default store is created before calling Store.default.'
    )
    new(default: true)
  end
end

Instance Method Details

#admin_usersObject



363
364
365
366
367
# File 'app/models/spree/store.rb', line 363

def admin_users
  Spree::Deprecation.warn('Store#admin_users is deprecated and will be removed in Spree 5.5. Please use Store#users instead.')

  users
end

#allowed_origin?(url) ⇒ Boolean

Returns true if the given URL’s origin matches one of the store’s allowed origins. Comparison is port-less: only scheme + host are matched, so storing ‘localhost` will match `localhost:3000`, `localhost:4000`, etc.

Parameters:

  • url (String)

    the full URL to check

Returns:



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'app/models/spree/store.rb', line 286

def allowed_origin?(url)
  return false if url.blank?

  uri = URI.parse(url)
  request_origin = "#{uri.scheme}://#{uri.host}"

  allowed_origins.pluck(:origin).any? do |stored|
    stored_uri = URI.parse(stored)
    "#{stored_uri.scheme}://#{stored_uri.host}" == request_origin
  rescue URI::InvalidURIError
    false
  end
rescue URI::InvalidURIError
  false
end

#checkout_zoneObject

Deprecated.

Use Markets instead. Will be removed in Spree 5.5.



193
194
195
196
# File 'app/models/spree/store.rb', line 193

def checkout_zone
  Spree::Deprecation.warn('Store#checkout_zone is deprecated and will be removed in Spree 5.5. Use Markets instead.')
  super
end

#checkout_zone=(zone) ⇒ Object

Deprecated.

Use Markets instead. Will be removed in Spree 5.5.



199
200
201
202
# File 'app/models/spree/store.rb', line 199

def checkout_zone=(zone)
  Spree::Deprecation.warn('Store#checkout_zone= is deprecated and will be removed in Spree 5.5. Use Markets instead.')
  super
end

#countries_with_shipping_coverageActiveRecord::Relation<Spree::Country>

Returns countries covered by at least one shipping zone that has an active shipping method attached. Handles both country-type zones (direct membership) and state-type zones (country inferred from state).

Returns:



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'app/models/spree/store.rb', line 330

def countries_with_shipping_coverage
  zone_ids = Spree::Zone
             .joins(:shipping_methods)
             .select(:id)

  country_zone_country_ids = Spree::ZoneMember
                             .where(zone_id: zone_ids, zoneable_type: 'Spree::Country')
                             .select(:zoneable_id)

  state_zone_country_ids = Spree::State
                           .where(id: Spree::ZoneMember
                                      .where(zone_id: zone_ids, zoneable_type: 'Spree::State')
                                      .select(:zoneable_id))
                           .select(:country_id)

  Spree::Country
    .where(id: country_zone_country_ids)
    .or(Spree::Country.where(id: state_zone_country_ids))
    .order(:name)
end

#default_channelObject



188
189
190
# File 'app/models/spree/store.rb', line 188

def default_channel
  channels.find_by(code: Spree::Channel::DEFAULT_CODE) || channels.active.first
end

#default_shipping_categoryObject



373
374
375
# File 'app/models/spree/store.rb', line 373

def default_shipping_category
  @default_shipping_category ||= ShippingCategory.find_or_create_by(name: 'Default')
end

#default_stock_locationSpree::StockLocation

Returns the default stock location for the store or creates a new one if it doesn’t exist



353
354
355
356
357
358
359
360
361
# File 'app/models/spree/store.rb', line 353

def default_stock_location
  @default_stock_location ||= begin
    stock_location_scope = Spree::StockLocation.where(default: true)
    stock_location_scope.first || ActiveRecord::Base.connected_to(role: :writing) do
      stock_location_scope.create(default: true, name: Spree.t(:default_stock_location_name),
                                  country: default_country)
    end
  end
end

#digital_shipping_categoryObject



377
378
379
# File 'app/models/spree/store.rb', line 377

def digital_shipping_category
  @digital_shipping_category ||= ShippingCategory.find_or_create_by(name: 'Digital')
end

#formatted_urlObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/models/spree/store.rb', line 237

def formatted_url
  @formatted_url ||= begin
    clean_url = url.to_s.sub(%r{^https?://}, '').split(':').first

    if Rails.env.development? || Rails.env.test?
      scheme = Rails.application.routes.default_url_options[:protocol] || :http
      port = Rails.application.routes.default_url_options[:port].presence || (Rails.env.development? ? 3000 : nil)

      if scheme.to_sym == :https
        URI::HTTPS.build(
          host: clean_url,
          port: port
        ).to_s
      else
        URI::HTTP.build(
          host: clean_url,
          port: port
        ).to_s
      end
    else
      URI::HTTPS.build(
        host: clean_url
      ).to_s
    end
  end
end

#formatted_url_or_custom_domainObject



268
269
270
# File 'app/models/spree/store.rb', line 268

def formatted_url_or_custom_domain
  formatted_url
end

#metric_unit_system?Boolean

Returns:



369
370
371
# File 'app/models/spree/store.rb', line 369

def metric_unit_system?
  preferred_unit_system == 'metric'
end

#states_available_for_checkout(country) ⇒ Array<Spree::State>

Returns the states available for checkout for the store

Parameters:

Returns:



305
306
307
# File 'app/models/spree/store.rb', line 305

def states_available_for_checkout(country)
  country.states.to_a
end

#storefront_urlString

Returns the storefront origin URL for use in customer-facing emails and links. Uses the first allowed origin if configured, otherwise falls back to formatted_url.

Returns:



276
277
278
# File 'app/models/spree/store.rb', line 276

def storefront_url
  allowed_origins.order(:created_at).pick(:origin) || formatted_url
end

#supported_shipping_zonesObject

Deprecated.

Use Zone.all or #countries_with_shipping_coverage instead. Will be removed in Spree 5.5.



311
312
313
314
315
316
317
318
319
320
321
322
# File 'app/models/spree/store.rb', line 311

def supported_shipping_zones
  Spree::Deprecation.warn(
    'Store#supported_shipping_zones is deprecated and will be removed in Spree 5.5. ' \
    'Use Spree::Zone.all or Store#countries_with_shipping_coverage instead.'
  )
  zone = Spree::Zone.find_by(id: read_attribute(:checkout_zone_id))
  if zone.present?
    [zone]
  else
    Spree::Zone.includes(zone_members: :zoneable).all
  end
end

#unique_nameObject



233
234
235
# File 'app/models/spree/store.rb', line 233

def unique_name
  @unique_name ||= "#{name} (#{code})"
end

#url_or_custom_domainObject



264
265
266
# File 'app/models/spree/store.rb', line 264

def url_or_custom_domain
  url
end