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.

Add payment_methods scoped by store.

Matthew Kennedy461cb6b9a26f
diff --git a/backend/app/controllers/spree/admin/payment_methods_controller.rb b/backend/app/controllers/spree/admin/payment_methods_controller.rb
index d5c92d032a..4b7c5451b0 100644
--- a/backend/app/controllers/spree/admin/payment_methods_controller.rb
+++ b/backend/app/controllers/spree/admin/payment_methods_controller.rb
@@ -49,12 +49,22 @@ module Spree
private
+ def scope
+ current_store.payment_methods
+ end
+
def collection
return @collection if @collection.present?
params[:q] ||= {}
@collection = super.order(position: :asc)
+ @collection = if params[:q][:all_stores] == '1'
+ super.order(position: :asc)
+ else
+ scope.order(position: :asc)
+ end
+
@search = @collection.ransack(params[:q])
@collection = @search.result.page(params[:page]).per(params[:per_page])
end
diff --git a/backend/app/views/spree/admin/payment_methods/index.html.erb b/backend/app/views/spree/admin/payment_methods/index.html.erb
index a24aa585cd..fef9ea6afd 100644
--- a/backend/app/views/spree/admin/payment_methods/index.html.erb
+++ b/backend/app/views/spree/admin/payment_methods/index.html.erb
@@ -6,6 +6,33 @@
<%= button_link_to Spree.t(:new_payment_method), new_object_url, class: "btn-success", icon: 'add.svg', id: 'admin_new_payment_methods_link' %>
<% end if can? :create, Spree::PaymentMethod %>
+<% content_for :table_filter do %>
+ <div data-hook="admin_payment_methods_filter_table">
+ <%= search_form_for [:admin, @search] do |f| %>
+ <%- locals = {f: f} %>
+ <div data-hook="admin_payment_methods_index_search" class="row">
+ <div class="col-12">
+ <div class="form-group">
+ <%= f.label :name_cont, Spree.t('name') %>
+ <%= f.text_field :name_cont, size: 15, class: "form-control js-quick-search-target js-filterable" %>
+ </div>
+ </div>
+ <div class="col-12">
+ <%= f.field_container :all_stores do %>
+ <div class="custom-control custom-checkbox">
+ <%= f.check_box :all_stores, {checked: params[:q][:all_stores] == '1', class: 'custom-control-input'}, '1', '0' %>
+ <%= f.label :all_stores, Spree.t('admin.show_payment_methods_for_all_stores'), class: 'custom-control-label' %>
+ </div>
+ <% end %>
+ </div>
+ </div>
+ <div data-hook="admin_products_index_search_buttons" class="form-actions">
+ <%= button Spree.t(:search), 'search.svg' %>
+ </div>
+ <% end %>
+ </div>
+<% end %>
+
<% if @payment_methods.any? %>
<div class="table-responsive">
<table class="table sortable" id='listing_payment_methods' data-hook data-sortable-link="<%= update_positions_admin_payment_methods_url %>">
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index e6319ed648..6cca41919c 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -4,7 +4,9 @@ module Spree
FAVICON_CONTENT_TYPES = ['image/png', 'image/x-icon', 'image/vnd.microsoft.icon'].freeze
has_many :orders, class_name: 'Spree::Order'
- has_many :payment_methods, class_name: 'Spree::PaymentMethod'
+
+ has_many :store_payment_methods, class_name: 'Spree::StorePaymentMethod', dependent: :destroy
+ has_many :payment_methods, through: :store_payment_methods, class_name: 'Spree::PaymentMethod'
has_many :menus
diff --git a/core/app/models/spree/store_payment_method.rb b/core/app/models/spree/store_payment_method.rb
new file mode 100644
index 0000000000..7d41005569
--- /dev/null
+++ b/core/app/models/spree/store_payment_method.rb
@@ -0,0 +1,11 @@
+module Spree
+ class StorePaymentMethod < Spree::Base
+ self.table_name = 'spree_payment_methods_stores'
+
+ belongs_to :store, class_name: 'Spree::Store', touch: true
+ belongs_to :payment_method, class_name: 'Spree::PaymentMethod', touch: true
+
+ validates :store, :payment_method, presence: true
+ validates :store_id, uniqueness: { scope: :product_id }
+ end
+end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 42b377ae1c..dfea308670 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -549,9 +549,9 @@ en:
adjustment_total: Adjustment Total
adjustments: Adjustments
admin:
- show_orders_for_all_stores: Show orders for all stores
- show_products_for_all_stores: Show products for all stores
- show_menus_for_all_stores: Show menus for all stores
+ show_orders_for_all_stores: Show Orders for all stores
+ show_products_for_all_stores: Show Products for all stores
+ show_payment_methods_for_all_stores: Show Payment Methods for all stores
tab:
configuration: Configuration
content: Content