Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3.3] Allow to disable track inventory for product without variants #5048

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion backend/app/views/spree/admin/products/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,14 @@
</div>
<% end %>
</div>

<div data-hook="admin_product_form_track_inventory">
<%= f.field_container :track_inventory do %>
<label>
<%= f.check_box :track_inventory %>
<%= Spree::Variant.human_attribute_name(:track_inventory) %>
</label>
<% end %>
</div>
<% end %>

<div data-hook="admin_product_form_shipping_categories">
Expand Down
24 changes: 24 additions & 0 deletions backend/spec/features/admin/products/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ def build_option_type_with_values(name, values)
it "should show default tax category" do
expect(page).to have_select('product_tax_category_id', selected: 'Alcohol taxes')
end

it "can disable track_inventory" do
expect(page).to have_field("product_track_inventory", checked: true)
uncheck "product_track_inventory"
click_button "Create"
expect(page).to have_field("product_track_inventory", checked: false)
end
end

context "cloning a product", js: true do
Expand Down Expand Up @@ -343,6 +350,23 @@ def build_option_type_with_values(name, values)
end
end
end

it "can disable track_inventory" do
visit spree.admin_product_path(product)
expect(page).to have_field("product_track_inventory", checked: true)
uncheck "product_track_inventory"
click_button "Update"
expect(page).to have_field("product_track_inventory", checked: false)
end

context "with variants" do
let(:product) { create(:variant).product }

it "cannot disable track_inventory" do
visit spree.admin_product_path(product)
expect(page).to_not have_field("product_track_inventory")
end
end
end

context 'deleting a product', js: true do
Expand Down
1 change: 1 addition & 0 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def find_or_build_master
:height,
:price,
:sku,
:track_inventory,
:weight,
:width,
]
Expand Down
18 changes: 18 additions & 0 deletions core/spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -653,4 +653,22 @@ class Extension < Spree::Base
end
end
end

describe "inventory tracking" do
let(:product) { build(:product) }

describe "#track_inventory=" do
it "delegates to master variant" do
expect(product.master).to receive(:track_inventory=).with(true)
product.track_inventory = true
end
end

describe "#track_inventory" do
it "delegates to master variant" do
expect(product.master).to receive(:track_inventory) { true }
expect(product.track_inventory).to be(true)
end
end
end
end