Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
[CSV Import] fix assigning tags to product (#14052)
Kacper Mękarski9f97729f19ac
diff --git a/spree/core/app/jobs/spree/imports/assign_tags_job.rb b/spree/core/app/jobs/spree/imports/assign_tags_job.rbnew file mode 100644index 0000000000..4b0930144d--- /dev/null+++ b/spree/core/app/jobs/spree/imports/assign_tags_job.rb@@ -0,0 +1,14 @@+module Spree+ module Imports+ class AssignTagsJob < Spree::BaseJob+ queue_as Spree.queues.imports+ retry_on StandardError, wait: :polynomially_longer, attempts: 5++ def perform(product_id, tags)+ product = Spree::Product.find(product_id)+ product.tag_list = tags+ product.save!+ end+ end+ end+enddiff --git a/spree/core/app/services/spree/imports/row_processors/product_variant.rb b/spree/core/app/services/spree/imports/row_processors/product_variant.rbindex 4c7de55a41..ed6f6f9037 100644--- a/spree/core/app/services/spree/imports/row_processors/product_variant.rb+++ b/spree/core/app/services/spree/imports/row_processors/product_variant.rb@@ -72,10 +72,12 @@ module Spreeproduct.save!end+ handle_tags(product) if attributes['tags'].present?if has_product_attributes?handle_metafields(product)handle_categories(product)end+productelse# For non-master variants, only look up the product@@ -105,7 +107,6 @@ module Spreeproduct.meta_description = attributes['meta_description'] if attributes['meta_description'].present?product.meta_keywords = attributes['meta_keywords'] if attributes['meta_keywords'].present?product.status = to_spree_status(attributes['status']) if attributes['status'].present?- product.tag_list = attributes['tags'] if attributes['tags'].present?if options.empty?if attributes['shipping_category'].present?@@ -236,6 +237,10 @@ module Spreeproduct.update(metafields_attributes: nested_attrs) unless nested_attrs.empty?end+ def handle_tags(product)+ Spree::Imports::AssignTagsJob.perform_later(product.id, attributes['tags'])+ end+def handle_categories(product)Spree::Imports::CreateCategoriesJob.perform_later(product.id, store.id, prepare_taxon_pretty_names)enddiff --git a/spree/core/spec/jobs/spree/imports/assign_tags_job_spec.rb b/spree/core/spec/jobs/spree/imports/assign_tags_job_spec.rbnew file mode 100644index 0000000000..85589f0113--- /dev/null+++ b/spree/core/spec/jobs/spree/imports/assign_tags_job_spec.rb@@ -0,0 +1,51 @@+require 'spec_helper'++RSpec.describe Spree::Imports::AssignTagsJob, type: :job do+ let(:store) { @default_store }+ let!(:product) { create(:product) }++ describe '#perform' do+ it 'assigns tags to the product' do+ described_class.perform_now(product.id, 'ECO, Gold')++ expect(product.reload.tag_list).to contain_exactly('ECO', 'Gold')+ end++ it 'replaces existing tags' do+ product.tag_list = 'Old Tag'+ product.save!++ described_class.perform_now(product.id, 'New Tag')++ expect(product.reload.tag_list).to contain_exactly('New Tag')+ end++ it 'is idempotent' do+ described_class.perform_now(product.id, 'ECO, Gold')+ described_class.perform_now(product.id, 'ECO, Gold')++ expect(product.reload.tag_list).to contain_exactly('ECO', 'Gold')+ end++ context 'when there were no matching tags' do+ it 'creates the tags' do+ expect {+ described_class.perform_now(product.id, 'ECO, Gold')+ }.to change { ActsAsTaggableOn::Tag.count }.by(2)+ end+ end++ context 'when there were matching tags' do+ before do+ ActsAsTaggableOn::Tag.create(name: 'ECO')+ ActsAsTaggableOn::Tag.create(name: 'Gold')+ end++ it 'does not create new tags' do+ expect {+ described_class.perform_now(product.id, 'ECO, Gold')+ }.not_to change { ActsAsTaggableOn::Tag.count }+ end+ end+ end+enddiff --git a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rbindex c03fa31163..fd7bd5608a 100644--- a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb+++ b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb@@ -46,7 +46,6 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doexpect(product.status).to eq 'draft'expect(product.description).to eq row_data['description']expect(product.stores).to include(store)- expect(product.tag_list).to contain_exactly('ECO', 'Gold')expect(product.master).to eq variantexpect(variant.sku).to be_blankexpect(variant.price_in('USD').amount.to_f).to eq 62.99@@ -56,6 +55,10 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doexpect(variant.stock_items.first.stock_location).to eq store.default_stock_locationend+ it 'enqueues AssignTagsJob' do+ expect { subject.process! }.to have_enqueued_job(Spree::Imports::AssignTagsJob).with(anything, 'ECO, Gold')+ end+it 'does not touch the store when associating the product' doexpect { subject.process! }.not_to change { store.reload.updated_at }end@@ -83,6 +86,14 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doexpect(existing_product.reload.name).to eq 'Denim Shirt'endend++ context 'when tags are not present' do+ it 'does not enqueue AssignTagsJob' do+ row_data['tags'] = nil++ expect { subject.process! }.not_to have_enqueued_job(Spree::Imports::AssignTagsJob)+ end+ endendcontext 'when importing a variant row with options' do