Sha256: 106150c23a2ba9392ba8cf451c76de2511b71d455603d71c43a809b21a6f3860

Contents?: true

Size: 1.9 KB

Versions: 48

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module Spree
  class ReturnItem < Spree::Base
    module ExchangeVariantEligibility
      class SameOptionValue
        class_attribute :option_type_restrictions
        self.option_type_restrictions = []
        # This can be configured in an initializer, e.g.:
        # Spree::ReturnItem::ExchangeVariantEligibility::SameOptionValue.option_type_restrictions = ["size", "color"]
        #
        # This restriction causes only variants that share the same option value for the
        # specified option types to be returned. e.g.:
        #
        # option_type_restrictions = ["color", "waist"]
        # Variant: blue pants with 32 waist and 30 inseam
        #
        # can be exchanged for:
        # blue pants with 32 waist and 31 inseam
        #
        # cannot be exchanged for:
        # green pants with 32 waist and 30 inseam
        # blue pants with 34 waist and 32 inseam

        def self.eligible_variants(variant, options = {})
          product_variants = SameProduct.eligible_variants(variant, options).includes(option_values: :option_type)
          relevant_option_values = variant.option_values.select { |ov| option_type_restrictions.include? ov.option_type.name }

          if relevant_option_values.present?
            # Finds all the OptionValueVariants that have any of the
            # relevant option values, groups by variant and ensures the variant
            # has ALL of the relevant option values.
            variant_ids = Spree::OptionValuesVariant.
              where(variant_id: product_variants.distinct.pluck(:id)).
              where(option_value: relevant_option_values).
              group(:variant_id).
              having('COUNT(*) = ?', relevant_option_values.size).
              pluck(:variant_id)
            product_variants.where(id: variant_ids)
          else
            product_variants
          end
        end
      end
    end
  end
end

Version data entries

48 entries across 48 versions & 2 rubygems

Version Path
solidus_core-2.8.4 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core-2.8.3 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core-2.7.2 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core-2.6.4 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core-2.8.2 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core-2.8.1 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core-2.6.3 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core-2.8.0 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core-2.7.1 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core-2.6.2 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core_devise_token_auth-2.8.0.alpha.10 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core_devise_token_auth-2.8.0.alpha.9 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core_devise_token_auth-2.8.0.alpha.8 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core_devise_token_auth-2.8.0.alpha.7 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core_devise_token_auth-2.8.0.alpha.6 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb
solidus_core_devise_token_auth-2.8.0.alpha.5 app/models/spree/return_item/exchange_variant_eligibility/same_option_value.rb