Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Allow customers to purchase free products (#12539)

Damian Kaczmarczyk70e645e4788b
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rb
index b702bb4d62..2d7580a57b 100644
--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -4,13 +4,12 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
stub_authorization!
let(:user) { create(:admin_user) }
+ let(:store) { @default_store }
before do
allow(controller).to receive(:current_ability).and_call_original
end
- let(:store) { @default_store }
-
describe 'GET #index' do
it 'can find a product by SKU' do
product = create(:product, sku: 'ABC123', stores: [store])
@@ -491,6 +490,48 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
end
end
+ describe 'master variant prices' do
+ let(:product_params) do
+ {
+ master_attributes: {
+ id: product.master_id,
+ prices_attributes: {
+ '0' => {
+ currency: 'PLN',
+ amount: 10,
+ id: product.master.price_in('PLN')&.id
+ },
+ '1' => {
+ currency: 'USD',
+ amount: 20,
+ id: product.master.price_in('USD')&.id
+ }
+ }
+ }
+ }
+ end
+
+ it 'updates the prices of the master variant' do
+ send_request
+ expect(product.master.price_in('PLN').amount).to eq 10
+ expect(product.master.price_in('USD').amount).to eq 20
+ end
+
+ context 'when price is not present' do
+ before do
+ product_params[:master_attributes][:prices_attributes]['0'][:amount] = nil
+ end
+
+ it 'removes the price' do
+ send_request
+ expect(product.master.price_in('PLN').amount).to be_nil
+ expect(product.master.price_in('PLN').id).to be_nil
+ expect(product.master.price_in('USD').amount).to eq 20
+ expect(product.master.price_in('USD').id).to be_present
+ end
+ end
+ end
+
context 'adding variants to existing product' do
let(:stock_location) { create(:stock_location) }
let(:other_stock_location) { create(:stock_location) }
@@ -594,8 +635,8 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
variants_attributes: {
'0' => {
prices_attributes: {
- '0': { currency: 'PLN', amount: 10, id: variant1.price_in('PLN')&.id },
- '1': { currency: 'USD', amount: 20, id: variant1.price_in('USD')&.id }
+ '0' => { currency: 'PLN', amount: 10, id: variant1.price_in('PLN')&.id },
+ '1' => { currency: 'USD', amount: 20, id: variant1.price_in('USD')&.id }
},
id: variant1.id,
stock_items_attributes: {
@@ -620,8 +661,8 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
},
'1' => {
prices_attributes: {
- '0': { currency: 'PLN', amount: 30, id: variant2.price_in('PLN')&.id },
- '1': { currency: 'USD', amount: 40, id: variant2.price_in('USD')&.id }
+ '0' => { currency: 'PLN', amount: 30, id: variant2.price_in('PLN')&.id },
+ '1' => { currency: 'USD', amount: 40, id: variant2.price_in('USD')&.id }
},
id: variant2.id,
stock_items_attributes: {
@@ -648,6 +689,21 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
}
end
+ context 'when price is not present' do
+ before do
+ product_params[:variants_attributes]['0'][:prices_attributes]['0'][:amount] = nil
+ end
+
+ it 'removes the price' do
+ send_request
+
+ expect(variant1.price_in('PLN').amount).to be_nil
+ expect(variant1.price_in('PLN').id).to be_nil
+ expect(variant1.price_in('USD').amount).to eq 20
+ expect(variant1.price_in('USD').id).to be_present
+ end
+ end
+
it 'updates the variants' do
send_request
@@ -1068,7 +1124,7 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
let!(:taxon_1) { create(:taxon) }
it 'skips auto matching taxons' do
- expect { send_request }.to_not have_enqueued_job(Spree::Products::AutoMatchTaxonsJob)
+ expect { send_request }.not_to have_enqueued_job(Spree::Products::AutoMatchTaxonsJob)
end
end
end
@@ -1165,7 +1221,7 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
let!(:taxon_1) { create(:taxon) }
it 'skips auto matching taxons' do
- expect { send_request }.to_not have_enqueued_job(Spree::Products::AutoMatchTaxonsJob)
+ expect { send_request }.not_to have_enqueued_job(Spree::Products::AutoMatchTaxonsJob)
end
end
end
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 38c37b99ff..97122cf81e 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -170,7 +170,7 @@ module Spree
accepts_nested_attributes_for(
:prices,
reject_if: ->(attributes) { attributes['currency'].blank? || attributes['amount'].blank? },
- allow_destroy: false
+ allow_destroy: true
)
accepts_nested_attributes_for(
diff --git a/core/app/services/spree/products/prepare_nested_attributes.rb b/core/app/services/spree/products/prepare_nested_attributes.rb
index 843feeab82..fe02f1ff8f 100644
--- a/core/app/services/spree/products/prepare_nested_attributes.rb
+++ b/core/app/services/spree/products/prepare_nested_attributes.rb
@@ -14,7 +14,14 @@ module Spree
existing_variant = variant_params[:id].presence && @product.variants.find_by(id: variant_params[:id])
variants_to_remove.delete(variant_params[:id]) if variant_params[:id].present?
- variant_params.delete(:prices_attributes) unless can_update_prices?
+ if can_update_prices?
+ # If the variant price is nil then mark it for destruction
+ variant_params[:prices_attributes]&.each do |_key, price_params|
+ variant_params[:prices_attributes][key]['_destroy'] = '1' if price_params[:amount].blank?
+ end
+ else
+ variant_params.delete(:prices_attributes)
+ end
variant_params[:option_value_variants_attributes] = update_option_value_variants(variant_params.delete(:options), existing_variant)
@@ -27,6 +34,15 @@ module Spree
params[:product_option_types_attributes] = product_option_types_params.merge(removed_product_option_types_attributes)
elsif params[:master_attributes]
params[:master_attributes].delete(:stock_items_attributes) unless can_update_stock_items?
+
+ if can_update_prices?
+ # If the master price is nil then mark it for destruction
+ params[:master_attributes][:prices_attributes]&.each do |key, price_params|
+ params[:master_attributes][:prices_attributes][key]['_destroy'] = '1' if price_params[:amount].blank?
+ end
+ else
+ params[:master_attributes].delete(:prices_attributes)
+ end
end
params.delete(:variants_attributes) if params[:variants_attributes].blank?
diff --git a/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb b/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb
index fdb7b793a8..345732e289 100644
--- a/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb
+++ b/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb
@@ -9,7 +9,7 @@ variant_not_available = selected_variant.nil? ||
product.discontinued? ||
selected_variant.discontinued? ||
!selected_variant.purchasable? ||
- !selected_variant.price_in(current_currency).amount&.positive?
+ selected_variant.price_in(current_currency).amount.nil?
%>
<div class='<%= sticky_button_classes %> bottom-0 flex flex-col gap-4 z-10' data-sticky-button-target='stickyButton'>
diff --git a/storefront/app/views/themes/default/spree/products/_variant_options.html.erb b/storefront/app/views/themes/default/spree/products/_variant_options.html.erb
index 26ada59ecb..12ac864c98 100644
--- a/storefront/app/views/themes/default/spree/products/_variant_options.html.erb
+++ b/storefront/app/views/themes/default/spree/products/_variant_options.html.erb
@@ -1,7 +1,17 @@
-<% selected_option = product_selected_option_for_option(option_type, product: product, selected_variant: selected_variant, options_param_name: options_param_name) %>
-<% variants_available_arr = product.variants.map { |v| v.purchasable? && v.price_in(current_currency).amount&.positive? } %>
+<% selected_option =
+ product_selected_option_for_option(
+ option_type,
+ product: product,
+ selected_variant: selected_variant,
+ options_param_name: options_param_name,
+ ) %>
+<% variants_available_arr =
+ product.variants.map do |v|
+ v.purchasable? && v.price_in(current_currency).amount.present?
+ end %>
<% variants_option1_arr = product.variants.map { |v| v.options.first&.dig(:value) } %>
-<% variants_option2_arr = product.variants.map { |v| v.options.second&.dig(:value) } %>
+<% variants_option2_arr =
+ product.variants.map { |v| v.options.second&.dig(:value) } %>
<% variants_option3_arr = product.variants.map { |v| v.options.third&.dig(:value) } %>
<% product_option_values_for_option(option_type, product: product).each_with_index do |value, index| %>
<% option_disabled = true %>