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.



212
213
214
# File 'app/models/spree/store.rb', line 212

def default_country_iso
  @default_country_iso
end

Class Method Details

.available_localesObject



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

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

.current(_url = nil) ⇒ Object

Delegations



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

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

.defaultObject

Deprecated.

The or_initialize behavior will be removed in Spree 5.5.



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

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



358
359
360
361
362
# File 'app/models/spree/store.rb', line 358

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. See AllowedOrigin#matches? for the matching rules (scheme/host/port).

Parameters:

  • url (String)

    the full URL to check

Returns:



291
292
293
294
295
# File 'app/models/spree/store.rb', line 291

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

  allowed_origins.any? { |allowed_origin| allowed_origin.matches?(url) }
end

#checkout_zoneObject

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
  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.



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

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:



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'app/models/spree/store.rb', line 325

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_channelSpree::Channel?

Resolves the store's default channel via the default boolean column so promoting another channel in the admin takes effect immediately. Falls back to the first active channel only for malformed data with no flagged default.

Returns:



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

def default_channel
  channels.default.first || channels.active.first
end

#default_shipping_categoryObject



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

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



348
349
350
351
352
353
354
355
356
# File 'app/models/spree/store.rb', line 348

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



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

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

#formatted_urlObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/models/spree/store.rb', line 243

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



274
275
276
# File 'app/models/spree/store.rb', line 274

def formatted_url_or_custom_domain
  formatted_url
end

#metric_unit_system?Boolean

Returns:



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

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:



300
301
302
# File 'app/models/spree/store.rb', line 300

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:



282
283
284
# File 'app/models/spree/store.rb', line 282

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.



306
307
308
309
310
311
312
313
314
315
316
317
# File 'app/models/spree/store.rb', line 306

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



239
240
241
# File 'app/models/spree/store.rb', line 239

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

#url_or_custom_domainObject



270
271
272
# File 'app/models/spree/store.rb', line 270

def url_or_custom_domain
  url
end