Skip methods that distributor can't use in Package

This ensures that Spree's shipping methods take into account the ones
the order's distributor uses.

This reimplements what `#available_to_order_with_distributor_check?`
used to do and it doesn't work with Spree 2.0.4.
This commit is contained in:
Pau Perez
2018-04-30 13:54:40 +02:00
parent 6784621c1b
commit 776b5a23f6
4 changed files with 86 additions and 6 deletions

View File

@@ -42,12 +42,6 @@ Spree::ShippingMethod.class_eval do
]
end
def available_to_order_with_distributor_check?(order)
available_to_order_without_distributor_check?(order) &&
self.distributors.include?(order.distributor)
end
alias_method_chain :available_to_order?, :distributor_check
def within_zone?(order)
if order.ship_address
zone && zone.include?(order.ship_address)

View File

@@ -0,0 +1,33 @@
# Extends Spree's Package implementation to skip shipping methods that are not
# valid for OFN.
#
# It requires the following configuration in config/initializers/spree.rb:
#
# Spree.config do |config|
# ...
# config.package_factory = Stock::Package
# end
#
module Stock
class Package < Spree::Stock::Package
# Skips the methods that are not used by the order's distributor
#
# @return [Array<Spree::ShippingMethod>]
def shipping_methods
super.delete_if do |shipping_method|
!ships_with?(order.distributor, shipping_method)
end
end
private
# Checks whether the given distributor provides the specified shipping method
#
# @param distributor [Spree::Enterprise]
# @param shipping_method [Spree::ShippingMethod]
# @return [Boolean]
def ships_with?(distributor, shipping_method)
distributor.shipping_methods.include?(shipping_method)
end
end
end

View File

@@ -20,6 +20,8 @@ Spree.config do |config|
# Auto-capture payments. Without this option, payments must be manually captured in the paypal interface.
config.auto_capture = true
#config.override_actionmailer_config = false
config.package_factory = Stock::Package
end
# TODO Work out why this is necessary

View File

@@ -0,0 +1,51 @@
require 'spec_helper'
module Stock
describe Package do
describe '#shipping_methods' do
let(:stock_location) { double(:stock_location) }
let(:order) { build(:order) }
subject(:package) { Package.new(stock_location, order, contents) }
describe '#shipping_methods' do
let(:enterprise) { build(:enterprise) }
let(:other_enterprise) { build(:enterprise) }
let(:order) { build(:order, distributor: enterprise) }
let(:variant1) do
instance_double(
Spree::Variant,
shipping_category: shipping_method1.shipping_categories.first
)
end
let(:variant2) do
instance_double(
Spree::Variant,
shipping_category: shipping_method2.shipping_categories.first
)
end
let(:variant3) do
instance_double(Spree::Variant, shipping_category: nil)
end
let(:contents) do
[
Package::ContentItem.new(variant1, 1),
Package::ContentItem.new(variant1, 1),
Package::ContentItem.new(variant2, 1),
Package::ContentItem.new(variant3, 1)
]
end
let(:shipping_method1) { create(:shipping_method, distributors: [enterprise]) }
let(:shipping_method2) { create(:shipping_method, distributors: [other_enterprise]) }
it 'does not return shipping methods not used by the package\'s order distributor' do
expect(package.shipping_methods).to eq [shipping_method1]
end
end
end
end
end