-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #1037 Add Quantity to Customer and Add Query or Create Variant …
…in Subscription
- Loading branch information
Showing
14 changed files
with
311 additions
and
18 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
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,31 @@ | ||
module SpreeCmCommissioner | ||
class SkuGenerator | ||
def initialize(product, variant_params) | ||
@product = product | ||
@variant_params = variant_params | ||
end | ||
|
||
def generate_sku | ||
load_product | ||
|
||
return if @variant_params.nil? || @variant_params.blank? | ||
|
||
option_values = @variant_params[:option_value_ids].map { |id| Spree::OptionValue.find(id) } | ||
sku_parts = [@product.name] | ||
|
||
option_values.each do |option_value| | ||
sku_parts << "#{option_value.option_type.name}-#{option_value.name}" | ||
end | ||
|
||
sku_parts << "price-#{@variant_params[:price]}" | ||
|
||
sku_parts.join('-').gsub(' ', '-').downcase | ||
end | ||
|
||
private | ||
|
||
def load_product | ||
@product ||= Spree::Product.find(@variant_params[:product_id]) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module SpreeCmCommissioner | ||
class VariantCreator | ||
attr_reader :variant | ||
|
||
def initialize(product, variant_params, current_vendor) | ||
@product = product | ||
@variant_params = variant_params | ||
@current_vendor = current_vendor | ||
end | ||
|
||
def create_variant | ||
@variant = @product.variants.new | ||
generate_sku | ||
set_variant_attributes | ||
save_variant | ||
end | ||
|
||
private | ||
|
||
def generate_sku | ||
sku_generator = SpreeCmCommissioner::SkuGenerator.new(@product, @variant_params) | ||
@variant.sku = sku_generator.generate_sku | ||
end | ||
|
||
def set_variant_attributes | ||
@variant.option_value_ids = @variant_params[:option_value_ids] | ||
@variant.price = @variant_params[:price] | ||
@variant.stock_items.new(stock_location_id: @current_vendor.id, count_on_hand: 1) | ||
end | ||
|
||
def save_variant | ||
@variant.save | ||
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 |
---|---|---|
@@ -1,22 +1,94 @@ | ||
<div data-hook="admin_user_form_fields" class="row"> | ||
<div class="col-12 col-md-6"> | ||
<div class="col-12"> | ||
<%= f.hidden_field :customer_id, value: @customer.id %> | ||
<%= f.field_container :variant_id do %> | ||
<%= f.label :variant, Spree.t(:variant) %> | ||
<%= f.collection_select(:variant_id, @customer.subscribable_variants, :id, :display_variant, { include_blank: false }, { class: 'select2' ,disabled: @subscription.persisted?}) %> | ||
<%= f.error_message_on :product_id %> | ||
<% end %> | ||
<%= f.field_container :start_date do %> | ||
<%= f.label :start_date, Spree.t(:start_date) %> | ||
<%= f.date_field :start_date, class: 'form-control', disabled: @subscription.persisted? %> | ||
<%= f.error_message_on :start_date %> | ||
<% end %> | ||
<% if @subscription.id.present? %> | ||
<%= f.field_container :status do %> | ||
<%= f.label :status, Spree.t(:status) %> | ||
<%= f.select :status, SpreeCmCommissioner::Subscription.statuses.keys, {}, class: 'select2' %> | ||
<%= f.error_message_on :status %> | ||
|
||
<div class="col-6"> | ||
<%= f.field_container :product do %> | ||
<%= f.label :product, Spree:t(:product) %> | ||
<%= f.select :product, options_for_select(Spree::Product.joins(:taxons) | ||
.where(spree_taxons: { id: @customer.taxon_id }) | ||
.map{ |product| [product.name, product.id] }), | ||
if @subscription.persisted? | ||
{ placeholder: @subscription.variant.product.name } | ||
else | ||
{ include_blank: true } | ||
end, | ||
{ class: "select2-clear", disabled: @subscription.persisted? } %> | ||
<%= f.error_message_on :product %> | ||
<% end %> | ||
</div> | ||
|
||
<% if @subscription.id.present? %> | ||
<div class="col-6"> | ||
<%= f.field_container :variant_id do %> | ||
<%= f.label :variant_id, Spree:t(:variant) %> | ||
<%= f.select :variant_id, options_for_select(Spree::Variant.joins(:subscriptions).where(cm_subscriptions: { customer_id: @customer.id }) | ||
.map{ |variant| [variant.sku, variant.id] }), | ||
if @subscription.persisted? | ||
{ placeholder: @subscription.variant.sku } | ||
else | ||
{ include_blank: true } | ||
end, | ||
{ class: "select2-clear", disabled: @subscription.persisted? } %> | ||
<%= f.error_message_on :product %> | ||
<% end %> | ||
</div> | ||
<% else %> | ||
<div id="option-types-form-container" class="col-12"> | ||
</div> | ||
<% end %> | ||
|
||
<div class="col-6"> | ||
<%= f.field_container :start_date do %> | ||
<%= f.label :start_date, Spree.t(:start_date) %> | ||
<%= f.date_field :start_date, class: 'form-control', disabled: @subscription.persisted? %> | ||
<%= f.error_message_on :start_date %> | ||
<% end %> | ||
</div> | ||
|
||
<div class="col-6"> | ||
<% if @subscription.id.present? %> | ||
<%= f.field_container :status do %> | ||
<%= f.label :status, Spree.t(:status) %> | ||
<%= f.select :status, SpreeCmCommissioner::Subscription.statuses.keys, {}, class: 'select2' %> | ||
<%= f.error_message_on :status %> | ||
<% end %> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<script> | ||
$(document).ready(function () { | ||
const productSelector = "#spree_cm_commissioner_subscription_product"; | ||
const variantIdSelector = "#spree_cm_commissioner_subscription_variant_id"; | ||
const optionTypesFormDiv = "#option-types-form-container"; | ||
|
||
$(productSelector).on("change", function () { | ||
removeOptionTypesForm(); | ||
appendOptionTypesForm(); | ||
}); | ||
|
||
function appendOptionTypesForm() { | ||
var productId = $(productSelector).val(); | ||
|
||
$.ajax({ | ||
url: '../subscriptions/load_option_types_form', | ||
type: 'GET', | ||
data: { product_id: productId }, | ||
dataType: 'html', | ||
success: function (response) { | ||
console.log('Success fetching form:', response); | ||
$(optionTypesFormDiv).append(response); | ||
}, | ||
error: function (error) { | ||
console.error('Error fetching form:', error); | ||
} | ||
}); | ||
} | ||
|
||
function removeOptionTypesForm() { | ||
$(optionTypesFormDiv).empty(); | ||
} | ||
}); | ||
</script> |
33 changes: 33 additions & 0 deletions
33
app/views/spree/billing/subscriptions/_option_types_form.html.erb
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 @@ | ||
<%= fields_for @variant do |f| %> | ||
<div class="card mb-3"> | ||
<div class="card-header"> | ||
<h5 class="mb-0"><%= Spree.t(:variant) %></h5> | ||
</div> | ||
<%= f.hidden_field :product_id, value: @product.id %> | ||
|
||
<div class="row"> | ||
<div data-hook="billing_variant_form_fields" class="col-6 p-4"> | ||
<div data-hook="variants"> | ||
<div class="form-group " data-hook="price"> | ||
<%= f.label :price, Spree.t(:price) %> | ||
<%= f.text_field :price, value: number_to_currency(@variant.price, unit: ''), class: 'form-control' %> | ||
</div> | ||
</div> | ||
</div> | ||
<div data-hook="billing_varaint_option_types_field" class="col-6 p-4"> | ||
<% @product.option_types.each do |option_type| %> | ||
<div class="form-group" data-hook="presentation"> | ||
<%= label :new_variant, option_type.presentation %> | ||
<% if option_type.name == 'color' %> | ||
<%= f.collection_select 'option_value_ids', option_type.option_values, :id, :name, | ||
{ include_blank: true }, { name: 'variant[option_value_ids][]', class: 'select2-clear form-control', id: "option_value_ids-#{option_type.id}" } %> | ||
<% else %> | ||
<%= f.collection_select 'option_value_ids', option_type.option_values, :id, :presentation, | ||
{ include_blank: true }, { name: 'variant[option_value_ids][]', class: 'select2-clear form-control', id: "option_value_ids-#{option_type.id}" } %> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
<% 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
Oops, something went wrong.