Class: Spree::DeliveryMethod
- Inherits:
-
Object
- Object
- Spree::DeliveryMethod
- Extended by:
- DisplayMoney
- Includes:
- CalculatedAdjustments, HasCustomFields, MemoizedData, Metadata, SingleStoreResource, TypedAssociations
- Defined in:
- app/models/spree/delivery_method.rb
Constant Summary collapse
- MEMOIZED_METHODS =
%w[display_estimated_price]
- STOREFRONT =
Audience a rate refresh is quoting for: the storefront sees only customer-facing methods, the backoffice sees every method.
:storefront- BACKOFFICE =
:backoffice- DEFAULT_RATE_PROVIDER =
Quoting strategy used when
rate_provideris blank. 'Spree::DeliveryRateProvider::Internal'.freeze
- DEFAULT_FULFILLMENT_PROVIDER =
Dispatch strategy used when
fulfillment_provideris blank. 'Spree::FulfillmentProvider::Manual'.freeze
- DISPLAY_ON_FRONT_END =
Deprecated.
Use STOREFRONT/BACKOFFICE; removed in 6.3.
1- DISPLAY_ON_BACK_END =
Deprecated.
Use STOREFRONT/BACKOFFICE; removed in 6.3.
2
Class Method Summary collapse
- .calculators ⇒ Object
-
.normalize_audience(audience) ⇒ Symbol
Maps an audience onto STOREFRONT/BACKOFFICE, accepting the legacy DISPLAY_ON_* integers so callers holding the old constants keep working until 6.3.
Instance Method Summary collapse
- #assign_default_delivery_profile ⇒ Object
-
#assign_default_origin_group ⇒ Object
The zone's group wins when a zone is set (they must agree anyway); zoneless methods fall back to the profile's default group.
-
#available_pickup_locations ⇒ ActiveRecord::Relation<Spree::StockLocation>
Stock locations this method can fulfill from.
-
#available_to?(audience) ⇒ Boolean
Whether this method may be quoted for the given audience.
-
#available_to_display?(display_filter) ⇒ Boolean
deprecated
Deprecated.
Use #available_to? with STOREFRONT/BACKOFFICE; removed in 6.3.
-
#available_to_seller ⇒ ActiveRecord::Relation<Spree::DeliveryMethod>
Every method a package sourced from the given seller's location may be quoted by: that seller's own, plus whatever the operator shares.
- #build_tracking_url(tracking) ⇒ Object
- #default_calculator ⇒ Object
- #delivery_range ⇒ Object
-
#digital? ⇒ Boolean
Returns true if the delivery method delivers digitally.
- #display_estimated_price ⇒ Object
-
#display_on ⇒ Object
deprecated
Deprecated.
Use #storefront_visible; removed in 6.3.
-
#display_on=(value) ⇒ Object
deprecated
Deprecated.
Use #storefront_visible=; removed in 6.3.
-
#eligible_for_package?(package) ⇒ Boolean
AND over the method's active rules; no rules = eligible everywhere.
- #ensure_calculator ⇒ Object
- #include?(address) ⇒ Boolean
-
#offers_service?(estimate) ⇒ Boolean
Whether the method offers this estimate: no service rows means every service the provider returns, rows mean exactly those.
- #origin_group_must_match ⇒ Object
- #pending_rules? ⇒ Boolean
- #pending_services? ⇒ Boolean
- #pickup? ⇒ Boolean
- #pickup_point? ⇒ Boolean
-
#pickup_point_provider_instance ⇒ Spree::PickupPointProvider::Base?
The third-party pickup point network behind a pickup_point method.
- #provider ⇒ Object
-
#provider_class ⇒ Class
The FulfillmentProvider strategy handling this method's fulfillments.
-
#rate_provider_class ⇒ Class
Falls back to the default when the stored provider no longer resolves — a gem can be uninstalled while its rows remain, and raising here would surface as a checkout error rather than a missing carrier option.
-
#rate_provider_instance ⇒ Spree::DeliveryRateProvider::Base
The DeliveryRateProvider strategy quoting this method.
-
#requires_address? ⇒ Boolean
Whether this method delivers to a customer shipping address — answered by the fulfillment provider (digital and the pickup kinds are address-free; custom providers default to requiring one).
-
#requires_zone_check? ⇒ Boolean
Zones describe the customer's destination address, so only methods that deliver to one are zone-checked.
-
#rules=(rows) ⇒ Object
Flat-payload writer for
rules, so one PATCH saves the method and its conditions together. -
#seller_owned? ⇒ Boolean
Whether this method belongs to a seller rather than the marketplace.
-
#serves_location?(stock_location) ⇒ Boolean
Whether this method can serve a package sourced from the given stock location — answered by the fulfillment provider (shipping/digital are location-agnostic; merchant pickup requires an eligible counter).
-
#service_for(estimate) ⇒ Spree::DeliveryMethodService?
The service row matching an estimate's carrier + service identity.
-
#services=(rows) ⇒ Object
Flat-payload writer for carrier service rows, so one PATCH saves the method and its services together.
-
#tracking_number_service(tracking) ⇒ Object
your shipping method subclasses can override this method to provide a custom tracking number service.
Methods included from DisplayMoney
Methods included from Metadata
#private_metadata, #private_metadata=
Class Method Details
.calculators ⇒ Object
391 392 393 394 395 |
# File 'app/models/spree/delivery_method.rb', line 391 def self.calculators # calculator registry key keeps its historical name spree_calculators.shipping_methods. select { |c| c.to_s.constantize < Spree::ShippingCalculator } end |
.normalize_audience(audience) ⇒ Symbol
Maps an audience onto STOREFRONT/BACKOFFICE, accepting the legacy DISPLAY_ON_* integers so callers holding the old constants keep working until 6.3. Anything else raises — a typo must not silently quote storefront-only rates.
419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'app/models/spree/delivery_method.rb', line 419 def self.normalize_audience(audience) case audience when STOREFRONT, BACKOFFICE then audience when DISPLAY_ON_FRONT_END, DISPLAY_ON_BACK_END Spree::Deprecation.warn( 'Spree::DeliveryMethod::DISPLAY_ON_* audience filters are deprecated and will be removed in Spree 6.3. ' \ "Use #{STOREFRONT.inspect} / #{BACKOFFICE.inspect} instead." ) audience == DISPLAY_ON_BACK_END ? BACKOFFICE : STOREFRONT else raise ArgumentError, "unknown delivery audience #{audience.inspect} (expected #{STOREFRONT.inspect} or #{BACKOFFICE.inspect})" end end |
Instance Method Details
#assign_default_delivery_profile ⇒ Object
238 239 240 |
# File 'app/models/spree/delivery_method.rb', line 238 def assign_default_delivery_profile self.delivery_profile ||= store&.default_delivery_profile end |
#assign_default_origin_group ⇒ Object
The zone's group wins when a zone is set (they must agree anyway); zoneless methods fall back to the profile's default group.
244 245 246 |
# File 'app/models/spree/delivery_method.rb', line 244 def assign_default_origin_group self.delivery_origin_group ||= delivery_zone&.delivery_origin_group || delivery_profile&.default_origin_group end |
#available_pickup_locations ⇒ ActiveRecord::Relation<Spree::StockLocation>
Stock locations this method can fulfill from. For pickup methods an empty set means every active pickup-enabled location of the method's own store qualifies — never other stores' counters.
379 380 381 382 |
# File 'app/models/spree/delivery_method.rb', line 379 def available_pickup_locations configured = pickup_locations.merge(Spree::StockLocation.active) configured.exists? ? configured : store.stock_locations.active.pickup_enabled end |
#available_to?(audience) ⇒ Boolean
Whether this method may be quoted for the given audience. The backoffice sees every method; the storefront only customer-facing ones. An unrecognized audience raises rather than silently quoting storefront rates — a typo must not narrow the offer set unnoticed.
405 406 407 408 409 410 |
# File 'app/models/spree/delivery_method.rb', line 405 def available_to?(audience) case self.class.normalize_audience(audience) when BACKOFFICE then true else storefront_visible? end end |
#available_to_display?(display_filter) ⇒ Boolean
Use #available_to? with STOREFRONT/BACKOFFICE; removed in 6.3.
435 436 437 438 439 440 441 |
# File 'app/models/spree/delivery_method.rb', line 435 def available_to_display?(display_filter) Spree::Deprecation.warn( 'Spree::DeliveryMethod#available_to_display? is deprecated and will be removed in Spree 6.3. ' \ 'Use #available_to?(Spree::DeliveryMethod::STOREFRONT / BACKOFFICE) instead.' ) available_to?(display_filter) end |
#available_to_seller ⇒ ActiveRecord::Relation<Spree::DeliveryMethod>
Every method a package sourced from the given seller's location may be quoted by: that seller's own, plus whatever the operator shares. A first-party package (nil seller) sees the operator's methods only — including the shared ones, which are the operator's to begin with.
113 114 115 |
# File 'app/models/spree/delivery_method.rb', line 113 scope :available_to_seller, ->(seller) { seller.nil? ? first_party : where(seller_id: seller.id).or(shared_with_sellers) } |
#build_tracking_url(tracking) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'app/models/spree/delivery_method.rb', line 197 def build_tracking_url(tracking) return if tracking.blank? tracking = tracking.upcase # build tracking url automatically if tracking_url.blank? # use tracking number gem to build tracking url # we need to upcase the tracking number # https://github.com/jkeen/tracking_number/pull/85 tracking_number_service(tracking).tracking_url if tracking_number_service(tracking).valid? else # build tracking url manually tracking_url.gsub(/:tracking/, ERB::Util.url_encode(tracking)) # :url_encode exists in 1.8.7 through 2.1.0 end end |
#default_calculator ⇒ Object
234 235 236 |
# File 'app/models/spree/delivery_method.rb', line 234 def default_calculator Spree::Calculator::Shipping::FlatRate.new(preferred_amount: 0) end |
#delivery_range ⇒ Object
443 444 445 446 447 448 449 450 451 |
# File 'app/models/spree/delivery_method.rb', line 443 def delivery_range return unless estimated_transit_business_days_min || estimated_transit_business_days_max if estimated_transit_business_days_min == estimated_transit_business_days_max estimated_transit_business_days_min.to_s else [estimated_transit_business_days_min, estimated_transit_business_days_max].compact.join('-') end end |
#digital? ⇒ Boolean
Returns true if the delivery method delivers digitally
481 482 483 |
# File 'app/models/spree/delivery_method.rb', line 481 def digital? provider_class.digital? end |
#display_estimated_price ⇒ Object
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'app/models/spree/delivery_method.rb', line 453 def display_estimated_price return unless calculator @display_estimated_price ||= begin calculator.description + ': ' + if calculator.is_a?(Spree::Calculator::Shipping::FlatRate) amount = calculator.amount_for(calculator.preferred_currency) if amount.nil? '' elsif amount.zero? I18n.t('spree.free') else Spree::Money.new(amount, { currency: calculator.preferred_currency }).to_s end elsif calculator.is_a?(Spree::Calculator::Shipping::FlexiRate) Spree::Money.new(calculator.preferred_first_item, { currency: calculator.preferred_currency }).to_s elsif calculator.is_a?(Spree::Calculator::Shipping::FlatPercentItemTotal) ActiveSupport::NumberHelper.number_to_percentage(calculator.preferred_flat_percent, precision: 2) else '' end end end |
#display_on ⇒ Object
Use #storefront_visible; removed in 6.3.
486 487 488 489 |
# File 'app/models/spree/delivery_method.rb', line 486 def display_on Spree::Deprecation.warn('Spree::DeliveryMethod#display_on is deprecated and will be removed in Spree 6.3. Use #storefront_visible instead.') storefront_visible? ? 'both' : 'back_end' end |
#display_on=(value) ⇒ Object
Use #storefront_visible=; removed in 6.3.
492 493 494 495 |
# File 'app/models/spree/delivery_method.rb', line 492 def display_on=(value) Spree::Deprecation.warn('Spree::DeliveryMethod#display_on= is deprecated and will be removed in Spree 6.3. Use #storefront_visible= instead.') self.storefront_visible = value.to_s != 'back_end' end |
#eligible_for_package?(package) ⇒ Boolean
AND over the method's active rules; no rules = eligible everywhere. Evaluated in the Estimator's method filter — the single seam every rate consumer (checkout, routing, cart estimates) flows through.
370 371 372 |
# File 'app/models/spree/delivery_method.rb', line 370 def eligible_for_package?(package) delivery_method_rules.select(&:active).all? { |rule| rule.eligible?(package) } end |
#ensure_calculator ⇒ Object
230 231 232 |
# File 'app/models/spree/delivery_method.rb', line 230 def ensure_calculator self.calculator ||= default_calculator end |
#include?(address) ⇒ Boolean
183 184 185 186 187 188 189 |
# File 'app/models/spree/delivery_method.rb', line 183 def include?(address) return true unless requires_zone_check? return false unless address return true if delivery_zone.nil? delivery_zone.include?(address) end |
#offers_service?(estimate) ⇒ Boolean
Whether the method offers this estimate: no service rows means every service the provider returns, rows mean exactly those.
356 357 358 |
# File 'app/models/spree/delivery_method.rb', line 356 def offers_service?(estimate) services.empty? || service_for(estimate).present? end |
#origin_group_must_match ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'app/models/spree/delivery_method.rb', line 248 def origin_group_must_match if delivery_origin_group.present? && delivery_profile.present? && delivery_origin_group.delivery_profile_id != delivery_profile_id errors.add(:delivery_origin_group, :invalid) end return if delivery_zone.nil? || delivery_origin_group.nil? return if delivery_zone.delivery_origin_group_id == delivery_origin_group_id errors.add(:delivery_zone, :does_not_belong_to_origin_group, message: I18n.t('spree.errors.messages.delivery_zone_not_in_origin_group')) end |
#pending_rules? ⇒ Boolean
360 361 362 |
# File 'app/models/spree/delivery_method.rb', line 360 def pending_rules? @pending_delivery_method_rules.present? end |
#pending_services? ⇒ Boolean
339 340 341 |
# File 'app/models/spree/delivery_method.rb', line 339 def pending_services? @pending_services.present? end |
#pickup? ⇒ Boolean
219 220 221 |
# File 'app/models/spree/delivery_method.rb', line 219 def pickup? provider_class.pickup? end |
#pickup_point? ⇒ Boolean
261 262 263 |
# File 'app/models/spree/delivery_method.rb', line 261 def pickup_point? provider_class.pickup_point? end |
#pickup_point_provider_instance ⇒ Spree::PickupPointProvider::Base?
The third-party pickup point network behind a pickup_point method.
387 388 389 |
# File 'app/models/spree/delivery_method.rb', line 387 def pickup_point_provider_instance @pickup_point_provider_instance ||= Spree::ApiTyped.registered_class(Spree.pickup_point_providers, pickup_point_provider)&.new end |
#provider ⇒ Object
291 292 293 |
# File 'app/models/spree/delivery_method.rb', line 291 def provider @provider ||= provider_class.new end |
#provider_class ⇒ Class
The FulfillmentProvider strategy handling this method's fulfillments. Falls back to Manual for rows created before the 6.0 backfill ran. Falls back to Manual when the stored provider no longer resolves, for the same reason as #rate_provider_class — an uninstalled gem must not raise at ship time.
286 287 288 289 |
# File 'app/models/spree/delivery_method.rb', line 286 def provider_class Spree::ApiTyped.registered_class(Spree.fulfillment_providers, fulfillment_provider.presence || DEFAULT_FULFILLMENT_PROVIDER) || DEFAULT_FULFILLMENT_PROVIDER.constantize end |
#rate_provider_class ⇒ Class
Falls back to the default when the stored provider no longer resolves — a gem can be uninstalled while its rows remain, and raising here would surface as a checkout error rather than a missing carrier option.
309 310 311 312 |
# File 'app/models/spree/delivery_method.rb', line 309 def rate_provider_class Spree::ApiTyped.registered_class(Spree.delivery_rate_providers, rate_provider.presence || DEFAULT_RATE_PROVIDER) || DEFAULT_RATE_PROVIDER.constantize end |
#rate_provider_instance ⇒ Spree::DeliveryRateProvider::Base
The DeliveryRateProvider strategy quoting this method. Blank means the Internal (calculator-backed) provider, so untouched rows keep their existing pricing.
300 301 302 |
# File 'app/models/spree/delivery_method.rb', line 300 def rate_provider_instance @rate_provider_instance ||= rate_provider_class.new(self) end |
#requires_address? ⇒ Boolean
Whether this method delivers to a customer shipping address — answered by the fulfillment provider (digital and the pickup kinds are address-free; custom providers default to requiring one).
268 269 270 |
# File 'app/models/spree/delivery_method.rb', line 268 def requires_address? provider.requires_address? end |
#requires_zone_check? ⇒ Boolean
Zones describe the customer's destination address, so only methods that deliver to one are zone-checked.
193 194 195 |
# File 'app/models/spree/delivery_method.rb', line 193 def requires_zone_check? requires_address? end |
#rules=(rows) ⇒ Object
Flat-payload writer for rules, so one PATCH saves the method and its
conditions together. See
TypedAssociations#assign_typed_association.
318 319 320 |
# File 'app/models/spree/delivery_method.rb', line 318 def rules=(rows) assign_typed_association(:delivery_method_rules, rows) end |
#seller_owned? ⇒ Boolean
Whether this method belongs to a seller rather than the marketplace.
226 227 228 |
# File 'app/models/spree/delivery_method.rb', line 226 def seller_owned? seller_id.present? end |
#serves_location?(stock_location) ⇒ Boolean
Whether this method can serve a package sourced from the given stock location — answered by the fulfillment provider (shipping/digital are location-agnostic; merchant pickup requires an eligible counter).
275 276 277 |
# File 'app/models/spree/delivery_method.rb', line 275 def serves_location?(stock_location) provider.serves_location?(self, stock_location) end |
#service_for(estimate) ⇒ Spree::DeliveryMethodService?
The service row matching an estimate's carrier + service identity.
347 348 349 |
# File 'app/models/spree/delivery_method.rb', line 347 def service_for(estimate) services.detect { |row| row.carrier == estimate.carrier && row.service == estimate.service_level } end |
#services=(rows) ⇒ Object
Flat-payload writer for carrier service rows, so one PATCH saves the method and its services together. Rows update by id, match by (carrier, service), or create; rows omitted from the payload are destroyed. Assigning model instances (or an empty array) falls through to the standard association writer.
327 328 329 330 331 332 333 334 335 336 337 |
# File 'app/models/spree/delivery_method.rb', line 327 def services=(rows) first = Array(rows).first return super if first.nil? || first.is_a?(Spree.base_class) pending = Array(rows).map { |row| row.to_h.with_indifferent_access } if new_record? @pending_services = pending else reconcile_services(pending) end end |
#tracking_number_service(tracking) ⇒ Object
your shipping method subclasses can override this method to provide a custom tracking number service
215 216 217 |
# File 'app/models/spree/delivery_method.rb', line 215 def tracking_number_service(tracking) @tracking_number_service ||= Spree.tracking_number_service.new(tracking) end |