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.

Implement channels and product publications in rails admin

Damian Legawiec802029dfc742
diff --git a/spree/admin/app/javascript/spree/admin/application.js b/spree/admin/app/javascript/spree/admin/application.js
index 3640529aca..ecd2cda730 100644
--- a/spree/admin/app/javascript/spree/admin/application.js
+++ b/spree/admin/app/javascript/spree/admin/application.js
@@ -68,6 +68,7 @@ import OrderBillingAddressController from 'spree/admin/controllers/order_billing
import PageBuilderController from 'spree/admin/controllers/page_builder_controller'
import PasswordToggle from 'spree/admin/controllers/password_toggle_controller'
import ProductFormController from 'spree/admin/controllers/product_form_controller'
+import ProductPublishingController from 'spree/admin/controllers/product_publishing_controller'
import QueryBuilderController from 'spree/admin/controllers/query_builder_controller'
import RangeInputController from 'spree/admin/controllers/range_input_controller'
import TableController from 'spree/admin/controllers/table_controller'
@@ -140,6 +141,7 @@ application.register('page-builder', PageBuilderController)
application.register('password-toggle', PasswordToggle)
application.register('password-visibility', PasswordVisibility)
application.register('product-form', ProductFormController)
+application.register('product-publishing', ProductPublishingController)
application.register('query-builder', QueryBuilderController)
application.register('range-input', RangeInputController)
application.register('table', TableController)
diff --git a/spree/admin/app/javascript/spree/admin/controllers/product_form_controller.js b/spree/admin/app/javascript/spree/admin/controllers/product_form_controller.js
index 14209c4dec..d710e06d0b 100644
--- a/spree/admin/app/javascript/spree/admin/controllers/product_form_controller.js
+++ b/spree/admin/app/javascript/spree/admin/controllers/product_form_controller.js
@@ -4,10 +4,6 @@ export default class extends Controller {
static targets = [
'trackInventoryCheckbox',
'quantityForm',
- 'availableOn',
- 'makeActiveAt',
- 'discontinueOn',
- 'status',
'pricesForm'
]
@@ -26,28 +22,6 @@ export default class extends Controller {
this.togglePricesFormVisibility()
}
- switchAvailabilityDatesFields(event) {
- let status = event.target.value
- if (status === 'draft') {
- this.show(this.availableOnTarget)
- this.show(this.makeActiveAtTarget)
- } else if (status === 'active') {
- this.show(this.availableOnTarget)
- this.hide(this.makeActiveAtTarget)
- } else {
- this.hide(this.availableOnTarget)
- this.hide(this.makeActiveAtTarget)
- }
- }
-
- show(element) {
- element.classList.remove('hidden', 'd-none')
- }
-
- hide(element) {
- element.classList.add('hidden')
- }
-
toggleQuantityFormVisibility() {
if (this.hasQuantityFormTarget) {
if (!this.hasVariantsValue && this.trackInventoryCheckboxTarget.checked) {
diff --git a/spree/admin/app/views/spree/admin/products/_form.html.erb b/spree/admin/app/views/spree/admin/products/_form.html.erb
index 4cfc99ac71..13cbe1288a 100644
--- a/spree/admin/app/views/spree/admin/products/_form.html.erb
+++ b/spree/admin/app/views/spree/admin/products/_form.html.erb
@@ -11,6 +11,7 @@
</div>
<div class="col-span-12 md:col-span-4">
<%= render 'spree/admin/products/form/status', f: f %>
+ <%= render 'spree/admin/products/form/publishing', f: f %>
<%= render 'spree/admin/products/form/categorization', f: f %>
<%= render 'spree/admin/products/form/shipping', f: f %>
<%= render 'spree/admin/products/form/tax', f: f %>
diff --git a/spree/admin/app/views/spree/admin/products/form/_status.html.erb b/spree/admin/app/views/spree/admin/products/form/_status.html.erb
index 1eb61fef27..ff38613ce2 100644
--- a/spree/admin/app/views/spree/admin/products/form/_status.html.erb
+++ b/spree/admin/app/views/spree/admin/products/form/_status.html.erb
@@ -1,51 +1,7 @@
-<%# datetime-local inputs have no timezone; render values in the store's timezone so admins see and submit values in the store's local time. %>
-<%
- store_timezone = ActiveSupport::TimeZone[current_store.preferred_timezone] || Time.zone
- to_store_local = ->(value) { value&.in_time_zone(store_timezone)&.strftime('%Y-%m-%dT%H:%M') }
- make_active_at_local = to_store_local.call(@product.make_active_at)
- available_on_local = to_store_local.call(@product.available_on)
- discontinue_on_local = to_store_local.call(@product.discontinue_on)
- tz_help = Spree.t('admin.datetime_field_timezone_help', zone: store_timezone.name)
-%>
-
<div class="card mb-6">
<div class="card-body">
- <div class="grid grid-cols-12 gap-6 mb-6">
- <div class="col-span-6">
- <%= f.spree_select :status,
- available_status_options(@product),
- { selected: @product.status, help_bubble: show_product_status_help_bubble? ? Spree.t('admin.products.status_form.status') : nil },
- { data: {
- action: 'change->product-form#switchAvailabilityDatesFields',
- 'product-form-target': 'status'
- }
- } %>
- </div>
- <div class="col-span-6 <%= 'hidden' if @product.active? %>" data-product-form-target="makeActiveAt">
- <% if can?(:activate, @product) %>
- <%= f.spree_datetime_field :make_active_at,
- value: make_active_at_local,
- help: tz_help,
- help_bubble: Spree.t('admin.products.status_form.make_active_at'),
- max: discontinue_on_local %>
- <% end %>
- </div>
- </div>
- <div class="grid grid-cols-12 gap-6">
- <div data-product-form-target="availableOn" class="col-span-6">
- <%= f.spree_datetime_field :available_on,
- value: available_on_local,
- help: tz_help,
- help_bubble: Spree.t('admin.products.status_form.available_on'),
- max: discontinue_on_local %>
- </div>
- <div data-product-form-target="discontinueOn" class="col-span-6">
- <%= f.spree_datetime_field :discontinue_on,
- value: discontinue_on_local,
- help: tz_help,
- help_bubble: Spree.t('admin.products.status_form.discontinue_on'),
- min: make_active_at_local %>
- </div>
- </div>
+ <%= f.spree_select :status,
+ available_status_options(@product),
+ { selected: @product.status, help_bubble: show_product_status_help_bubble? ? Spree.t('admin.products.status_form.status') : nil } %>
</div>
</div>
diff --git a/spree/admin/config/initializers/spree_admin_navigation.rb b/spree/admin/config/initializers/spree_admin_navigation.rb
index 68989b71b9..4b6a1ad8cf 100644
--- a/spree/admin/config/initializers/spree_admin_navigation.rb
+++ b/spree/admin/config/initializers/spree_admin_navigation.rb
@@ -251,6 +251,15 @@ Rails.application.config.after_initialize do
active: -> { controller_name == 'policies' },
if: -> { can?(:manage, Spree::Policy) }
+ # Channels
+ settings_nav.add :channels,
+ label: :channels,
+ url: :admin_channels_path,
+ icon: 'broadcast',
+ position: 65,
+ active: -> { controller_name == 'channels' },
+ if: -> { can?(:manage, Spree::Channel) }
+
# Payment Methods
settings_nav.add :payment_methods,
label: :payments,
diff --git a/spree/admin/config/initializers/spree_admin_tables.rb b/spree/admin/config/initializers/spree_admin_tables.rb
index 2605bcf04b..c7c339d48b 100644
--- a/spree/admin/config/initializers/spree_admin_tables.rb
+++ b/spree/admin/config/initializers/spree_admin_tables.rb
@@ -1744,6 +1744,33 @@ Rails.application.config.after_initialize do
default: true,
position: 10
+ # ==========================================
+ # Register Channels table
+ # ==========================================
+ Spree.admin.tables.register(:channels, model_class: Spree::Channel, search_param: :name_cont)
+
+ Spree.admin.tables.channels.add :name,
+ label: :name,
+ type: :link,
+ sortable: true,
+ filterable: true,
+ default: true,
+ position: 10
+
+ Spree.admin.tables.channels.add :code,
+ label: :code,
+ sortable: true,
+ default: true,
+ position: 20
+
+ Spree.admin.tables.channels.add :active,
+ label: :status,
+ type: :status,
+ sortable: false,
+ default: true,
+ position: 30,
+ method: ->(c) { c.active? ? 'active' : 'inactive' }
+
# ==========================================
# Register Refund Reasons table
# ==========================================
diff --git a/spree/admin/config/locales/en.yml b/spree/admin/config/locales/en.yml
index defb4f0e0e..6e50051f9a 100644
--- a/spree/admin/config/locales/en.yml
+++ b/spree/admin/config/locales/en.yml
@@ -75,6 +75,8 @@ en:
title:
add_tags: Add tags
remove_tags: Remove tags
+ channels:
+ code_hint: A short, stable identifier (e.g. "online", "pos", "wholesale"). Used by API clients via the X-Spree-Channel header and for order attribution.
checkout_settings:
checkout_links:
description: Links to these pages will be displayed in the checkout footer.
@@ -280,14 +282,34 @@ en:
seo:
placeholder: Add a title and description to see how this product might appear in a search engine listing
status_form:
- available_on: Marks when the product will be released, put a future date to indicate that this is a pre-order
- discontinue_on: Marks when the product should be automatically taken off from your site
- make_active_at: Marks when the product should be automatically promoted to "active" state
status: Draft products aren't available for purchase. Active products are live.
+ status_options:
+ active: Active
+ archived: Archived
+ draft: Draft
stores:
choose_stores: Choose which stores this product should be available in
variants:
option_types_link: To add more option types please go to <a href="%{link}">Option Types</a>
+ publishing:
+ caption_hidden_after: Comes down %{date}
+ caption_live: Visible to customers now
+ caption_not_available: Hidden — product is %{product_status}
+ caption_scheduled: Goes live %{date}
+ caption_unpublished: Was hidden on %{date}
+ caption_window: Live %{start} → %{end}
+ empty: Not listed on any sales channel yet.
+ inactive_marker: Inactive
+ manage_cta: Manage
+ manage_description: Pick which sales channels list this product. Toggle a channel off to unlist it everywhere.
+ no_channels: No sales channels available. Create one in Settings → Sales channels.
+ published_at_label: Publish from
+ status_hidden: Hidden
+ status_live: Live
+ status_not_available: Not available
+ status_scheduled: Scheduled
+ title: Publishing
+ unpublished_at_label: Unpublish on
recommended_size: Recommended size
report_created: Your report is being generated. You will receive an email with a download link when it is ready!
reset_digital_link_download_limits: Reset download limits
diff --git a/spree/admin/config/routes.rb b/spree/admin/config/routes.rb
index 619938ec3f..fe916ea3a7 100644
--- a/spree/admin/config/routes.rb
+++ b/spree/admin/config/routes.rb
@@ -226,6 +226,7 @@ Spree::Core::Engine.add_routes do
resources :payment_methods, except: :show
resources :shipping_methods, except: :show
resources :shipping_categories, except: :show
+ resources :channels, except: :show
resources :store_credit_categories
resources :tax_rates, except: :show
resources :tax_categories, except: :show
diff --git a/spree/admin/lib/spree/admin/engine.rb b/spree/admin/lib/spree/admin/engine.rb
index 42359fc0b2..ebc9806c00 100644
--- a/spree/admin/lib/spree/admin/engine.rb
+++ b/spree/admin/lib/spree/admin/engine.rb
@@ -9,6 +9,8 @@ module Spree
:admin_users_header_partials,
:body_end_partials,
:body_start_partials,
+ :channels_actions_partials,
+ :channels_header_partials,
:classifications_actions_partials,
:classifications_header_partials,
:coupon_codes_actions_partials,
diff --git a/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb b/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb
index 448d9075b3..54179766ec 100644
--- a/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -1136,6 +1136,51 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
expect(product.reload.slug).to eq('existing-product-2-2dc1cfdf-81fe-4983-8709-ef6ee843c41d')
end
end
+
+ describe 'publishing card (product_publications_attributes)' do
+ let!(:pos_channel) { create(:channel, store: store, name: 'POS', code: 'pos') }
+ let!(:wholesale_channel) { create(:channel, store: store, name: 'Wholesale', code: 'wholesale') }
+ let(:default_channel) { store.default_channel }
+ let!(:default_publication) { product.product_publications.find_by(channel: default_channel) || product.product_publications.create!(channel: default_channel) }
+ let!(:wholesale_publication) { product.product_publications.create!(channel: wholesale_channel) }
+
+ let(:product_params) do
+ {
+ product_publications_attributes: {
+ '0' => { id: default_publication.id, channel_id: default_channel.id, _destroy: '0', published_at: '', unpublished_at: '' },
+ '1' => { id: '', channel_id: pos_channel.id, _destroy: '0', published_at: '', unpublished_at: '' },
+ '2' => { id: wholesale_publication.id, channel_id: wholesale_channel.id, _destroy: '1', published_at: '', unpublished_at: '' }
+ }
+ }
+ end
+
+ it 'syncs publications: keeps, creates, and destroys in one request' do
+ send_request
+
+ product.reload
+ expect(product.channels).to contain_exactly(default_channel, pos_channel)
+ expect(product.product_publications.find_by(channel: wholesale_channel)).to be_nil
+ end
+
+ it 'persists the published_at window on an existing publication' do
+ future = 2.days.from_now.change(usec: 0)
+ product_params[:product_publications_attributes]['0'][:published_at] = future.iso8601
+
+ send_request
+
+ expect(default_publication.reload.published_at).to be_within(1.second).of(future)
+ end
+
+ it 'is idempotent when no publications change' do
+ # Re-submit exactly the current state — both publications kept, no
+ # new attach, no detach.
+ product_params[:product_publications_attributes]['2'][:_destroy] = '0'
+ product_params[:product_publications_attributes].delete('1')
+
+ expect { send_request }
+ .not_to change { product.product_publications.reload.pluck(:channel_id).sort }
+ end
+ end
end
describe 'PUT #clone' do
diff --git a/spree/admin/spec/features/spree/admin/products/products_spec.rb b/spree/admin/spec/features/spree/admin/products/products_spec.rb
index b538b2dcf3..8ec3a3d58b 100644
--- a/spree/admin/spec/features/spree/admin/products/products_spec.rb
+++ b/spree/admin/spec/features/spree/admin/products/products_spec.rb
@@ -118,13 +118,10 @@ describe 'Products', type: :feature do
@property_prototype = create(:prototype, name: 'Random')
end
- it 'parses correctly available_on' do
- visit spree.edit_admin_product_path(product)
- fill_in 'product_available_on', with: '2012/12/25'
- within('#page-header') { click_button 'Update' }
- expect(page).to have_content('successfully updated!')
- expect(Spree::Product.last.available_on.to_date).to eq('2012-12-25'.to_date)
- end
+ # The product-level +available_on+ / +discontinue_on+ inputs were removed
+ # from the legacy admin in 5.5 — per-channel scheduling lives on the
+ # Publishing card's publication rows. The deprecated +available_on=+
+ # cascade is covered at the model level (spec/models/spree/product_spec.rb).
context 'using a locale with a different decimal format' do
before do
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 1f43eb8c02..9adff733f9 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -885,8 +885,8 @@ en:
change: Change
change_password: Change password
changes_published: Changes published!
- channel: Channel
- channels: Channels
+ channel: Sales channel
+ channels: Sales channels
charged: Charged
checkout: Checkout
checkout_message: Checkout message
@@ -1461,7 +1461,7 @@ en:
new_api_key: New API Key
new_balance: New balance
new_billing_address: New Billing Address
- new_channel: New Channel
+ new_channel: New sales channel
new_country: New Country
new_custom_domain: New Custom Domain
new_customer: New Customer
diff --git a/spree/core/lib/spree/permitted_attributes.rb b/spree/core/lib/spree/permitted_attributes.rb
index 2aad641403..9c29461c62 100644
--- a/spree/core/lib/spree/permitted_attributes.rb
+++ b/spree/core/lib/spree/permitted_attributes.rb
@@ -192,8 +192,8 @@ module Spree
label_list: [],
option_type_ids: [],
taxon_ids: [],
- store_ids: [],
- product_option_types_attributes: [:id, :option_type_id, :position, :_destroy]
+ product_option_types_attributes: [:id, :option_type_id, :position, :_destroy],
+ product_publications_attributes: [:id, :channel_id, :published_at, :unpublished_at, :_destroy]
}
]