-
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
18 changed files
with
385 additions
and
20 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
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,32 @@ | ||
module SpreeCmCommissioner | ||
class VariantChecker | ||
attr_reader :variant | ||
|
||
def initialize(product, variant_params, current_vendor) | ||
@product = product | ||
@variant_params = variant_params | ||
@current_vendor = current_vendor | ||
end | ||
|
||
def find_or_create_variant | ||
find_variant_by_sku || create_variant | ||
end | ||
|
||
private | ||
|
||
def find_variant_by_sku | ||
@variant = @product.variants.where(sku: variant_sku).first | ||
end | ||
|
||
def create_variant | ||
variant_creator = SpreeCmCommissioner::VariantCreator.new(@product, @variant_params, @current_vendor) | ||
variant_creator.create_variant | ||
@variant = variant_creator.variant | ||
end | ||
|
||
def variant_sku | ||
sku_generator = SpreeCmCommissioner::SkuGenerator.new(@product, @variant_params) | ||
sku_generator.generate_sku | ||
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,37 @@ | ||
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.build | ||
generate_sku | ||
set_variant_attributes | ||
@variant.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] | ||
stock_item = @variant.stock_items.build(stock_location_id: stock_location, count_on_hand: 1, backorderable: false) | ||
stock_item.save | ||
end | ||
|
||
def stock_location | ||
@stock_location ||= Spree::StockLocation.find_by!(vendor_id: @current_vendor.id).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
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, placeholder: I18n.t('spree.billing.subscription_price_placeholder', currency: @variant.currency), 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.