Require varian t category when creating new product variant [OFN-12666]

This commit is contained in:
wandji20
2024-07-21 21:05:05 +01:00
parent 53e3621e04
commit 2709479bf2
5 changed files with 22 additions and 10 deletions

View File

@@ -126,8 +126,11 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", ($scope, $timeout
DisplayProperties.setShowVariants 0, showVariants
$scope.addVariant = (product) ->
# Set new variant category to same as last product variant category to keep compactibility with deleted variant callback to set new variant category
newVariantId = $scope.nextVariantId();
newVariantCategoryId = product.variants[product.variants.length - 1]?.category_id
product.variants.push
id: $scope.nextVariantId()
id: newVariantId
unit_value: null
unit_description: null
on_demand: false
@@ -136,8 +139,9 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", ($scope, $timeout
on_hand: null
price: null
tax_category_id: null
category_id: null
category_id: newVariantCategoryId
DisplayProperties.setShowVariants product.id, true
DirtyProducts.addVariantProperty(product.id, newVariantId, 'category_id', newVariantCategoryId)
$scope.nextVariantId = ->

View File

@@ -87,7 +87,6 @@ module Spree
before_validation :ensure_unit_value
before_validation :update_weight_from_unit_value, if: ->(v) { v.product.present? }
before_validation :convert_variant_weight_to_decimal
before_validation :assign_related_taxon, if: ->(v) { v.primary_taxon.blank? }
before_save :assign_units, if: ->(variant) {
variant.new_record? || variant.changed_attributes.keys.intersection(NAME_FIELDS).any?
@@ -217,10 +216,6 @@ module Spree
private
def assign_related_taxon
self.primary_taxon ||= product.variants.last&.primary_taxon
end
def check_currency
return unless currency.nil?

View File

@@ -37,20 +37,24 @@
= f.label :on_demand do
= f.check_box :on_demand, 'data-action': 'change->toggle-control#disableIfPresent change->popout#closeIfChecked'
= t(:on_demand)
%td.col-producer.naked_inputs
%td.col-producer.field.naked_inputs
= render(SearchableDropdownComponent.new(form: f,
name: :supplier_id,
aria_label: t('.producer_field_name'),
options: producer_options,
selected_option: variant.supplier_id,
placeholder_value: t('admin.products_v3.filters.search_for_producers')))
include_blank: t('admin.products_v3.filters.select_producer'),
placeholder_value: t('admin.products_v3.filters.select_producer')))
= error_message_on variant, :supplier
%td.col-category.field.naked_inputs
= render(SearchableDropdownComponent.new(form: f,
name: :primary_taxon_id,
options: category_options,
selected_option: variant.primary_taxon_id,
aria_label: t('.category_field_name'),
placeholder_value: t('admin.products_v3.filters.search_for_categories')))
include_blank: t('admin.products_v3.filters.select_category'),
placeholder_value: t('admin.products_v3.filters.select_category')))
= error_message_on variant, :primary_taxon
%td.col-tax_category.field.naked_inputs
= render(SearchableDropdownComponent.new(form: f,
name: :tax_category_id,

View File

@@ -868,8 +868,10 @@ en:
filters:
search_products: Search for products
search_for_producers: Search for producers
select_producer: Select producer
all_producers: All producers
search_for_categories: Search for categories
select_category: Select category
all_categories: All categories
producers:
label: Producers

View File

@@ -20,6 +20,7 @@ RSpec.describe 'As an enterprise user, I can update my products' do
let(:tax_categories_search_selector) { 'input[placeholder="Search for tax categories"]' }
describe "updating" do
let!(:taxon) { create(:taxon) }
let!(:variant_a1) {
product_a.variants.first.tap{ |v|
v.update! display_name: "Medium box", sku: "APL-01", price: 5.25, on_hand: 5,
@@ -349,6 +350,9 @@ RSpec.describe 'As an enterprise user, I can update my products' do
click_on "On Hand" # activate popout
fill_in "On Hand", with: "3"
select producer.name, from: 'Producer'
select taxon.name, from: 'Category'
end
expect {
@@ -541,6 +545,9 @@ RSpec.describe 'As an enterprise user, I can update my products' do
click_on "Unit" # activate popout
fill_in "Unit value", with: "200"
select producer.name, from: 'Producer'
select taxon.name, from: 'Category'
end
expect {