-
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2256 from coopdevs/add-custom-package
Add custom package
- Loading branch information
Showing
7 changed files
with
88 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters