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 cart warnings system for out-of-stock/discontinued items

Damian Legawiec332592dd87e2
diff --git a/packages/next/src/index.ts b/packages/next/src/index.ts
index 03655f01e7..f7a0b90fba 100644
--- a/packages/next/src/index.ts
+++ b/packages/next/src/index.ts
@@ -70,6 +70,7 @@ export {
// Re-export commonly used SDK types for convenience
export type {
Cart,
+ CartWarning,
Product,
Order,
LineItem,
diff --git a/packages/sdk/src/store-client.ts b/packages/sdk/src/store-client.ts
index 7b9a4f3d35..a7eb40dbf9 100644
--- a/packages/sdk/src/store-client.ts
+++ b/packages/sdk/src/store-client.ts
@@ -28,7 +28,6 @@ import type {
RequestPasswordResetParams,
ResetPasswordParams,
Cart,
- CartResponse,
CreditCard,
GiftCard,
Product,
@@ -303,15 +302,15 @@ export class StoreClient {
* Get a cart by prefixed ID
* @param cartId - Cart prefixed ID (e.g., "cart_abc123")
*/
- get: (cartId: string, options?: RequestOptions): Promise<CartResponse> =>
- this.request<CartResponse>('GET', `/carts/${cartId}`, options),
+ get: (cartId: string, options?: RequestOptions): Promise<Cart> =>
+ this.request<Cart>('GET', `/carts/${cartId}`, options),
/**
* Create a new cart
* @param params - Optional cart parameters (e.g., metadata, items)
*/
- create: (params?: CreateCartParams, options?: RequestOptions): Promise<CartResponse> =>
- this.request<CartResponse>('POST', '/carts', {
+ create: (params?: CreateCartParams, options?: RequestOptions): Promise<Cart> =>
+ this.request<Cart>('POST', '/carts', {
...options,
body: params,
}),
@@ -325,8 +324,8 @@ export class StoreClient {
cartId: string,
params: UpdateCartParams,
options?: RequestOptions
- ): Promise<CartResponse> =>
- this.request<CartResponse>('PATCH', `/carts/${cartId}`, {
+ ): Promise<Cart> =>
+ this.request<Cart>('PATCH', `/carts/${cartId}`, {
...options,
body: params,
}),
@@ -343,8 +342,8 @@ export class StoreClient {
* @param cartId - Cart prefixed ID
* @param options - Must include `token` (JWT) for authentication
*/
- associate: (cartId: string, options: RequestOptions): Promise<CartResponse> =>
- this.request<CartResponse>('PATCH', `/carts/${cartId}/associate`, options),
+ associate: (cartId: string, options: RequestOptions): Promise<Cart> =>
+ this.request<Cart>('PATCH', `/carts/${cartId}/associate`, options),
/**
* Complete the cart and finalize the purchase.
@@ -367,8 +366,8 @@ export class StoreClient {
cartId: string,
params: AddLineItemParams,
options?: RequestOptions
- ): Promise<CartResponse> =>
- this.request<CartResponse>('POST', `/carts/${cartId}/items`, {
+ ): Promise<Cart> =>
+ this.request<Cart>('POST', `/carts/${cartId}/items`, {
...options,
body: params,
}),
@@ -383,8 +382,8 @@ export class StoreClient {
lineItemId: string,
params: UpdateLineItemParams,
options?: RequestOptions
- ): Promise<CartResponse> =>
- this.request<CartResponse>(
+ ): Promise<Cart> =>
+ this.request<Cart>(
'PATCH',
`/carts/${cartId}/items/${lineItemId}`,
{ ...options, body: params }
@@ -399,8 +398,8 @@ export class StoreClient {
cartId: string,
lineItemId: string,
options?: RequestOptions
- ): Promise<CartResponse> =>
- this.request<CartResponse>(
+ ): Promise<Cart> =>
+ this.request<Cart>(
'DELETE',
`/carts/${cartId}/items/${lineItemId}`,
options
@@ -420,8 +419,8 @@ export class StoreClient {
cartId: string,
code: string,
options?: RequestOptions
- ): Promise<CartResponse> =>
- this.request<CartResponse>('POST', `/carts/${cartId}/discount_codes`, {
+ ): Promise<Cart> =>
+ this.request<Cart>('POST', `/carts/${cartId}/discount_codes`, {
...options,
body: { code },
}),
@@ -435,8 +434,8 @@ export class StoreClient {
cartId: string,
code: string,
options?: RequestOptions
- ): Promise<CartResponse> =>
- this.request<CartResponse>(
+ ): Promise<Cart> =>
+ this.request<Cart>(
'DELETE',
`/carts/${cartId}/discount_codes/${code}`,
options
@@ -458,8 +457,8 @@ export class StoreClient {
cartId: string,
code: string,
options?: RequestOptions
- ): Promise<CartResponse> =>
- this.request<CartResponse>('POST', `/carts/${cartId}/gift_cards`, {
+ ): Promise<Cart> =>
+ this.request<Cart>('POST', `/carts/${cartId}/gift_cards`, {
...options,
body: { code },
}),
@@ -473,8 +472,8 @@ export class StoreClient {
cartId: string,
giftCardId: string,
options?: RequestOptions
- ): Promise<CartResponse> =>
- this.request<CartResponse>(
+ ): Promise<Cart> =>
+ this.request<Cart>(
'DELETE',
`/carts/${cartId}/gift_cards/${giftCardId}`,
options
@@ -495,8 +494,8 @@ export class StoreClient {
fulfillmentId: string,
params: { selected_delivery_rate_id: string },
options?: RequestOptions
- ): Promise<CartResponse> =>
- this.request<CartResponse>(
+ ): Promise<Cart> =>
+ this.request<Cart>(
'PATCH',
`/carts/${cartId}/fulfillments/${fulfillmentId}`,
{ ...options, body: params }
@@ -606,8 +605,8 @@ export class StoreClient {
cartId: string,
amount?: number,
options?: RequestOptions
- ): Promise<CartResponse> =>
- this.request<CartResponse>('POST', `/carts/${cartId}/store_credits`, {
+ ): Promise<Cart> =>
+ this.request<Cart>('POST', `/carts/${cartId}/store_credits`, {
...options,
body: amount ? { amount } : undefined,
}),
@@ -616,8 +615,8 @@ export class StoreClient {
* Remove store credit from the cart
* @param cartId - Cart prefixed ID
*/
- remove: (cartId: string, options?: RequestOptions): Promise<CartResponse> =>
- this.request<CartResponse>(
+ remove: (cartId: string, options?: RequestOptions): Promise<Cart> =>
+ this.request<Cart>(
'DELETE',
`/carts/${cartId}/store_credits`,
options
diff --git a/packages/sdk/src/types/generated/Cart.ts b/packages/sdk/src/types/generated/Cart.ts
index b34e5dc7a7..28034dc497 100644
--- a/packages/sdk/src/types/generated/Cart.ts
+++ b/packages/sdk/src/types/generated/Cart.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 9357c6093adf24fde9493c823cdaa56f
+// Typelizer digest 4b818c3ed4e47fe243930784069d5626
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {Discount, LineItem, Fulfillment, Payment, Address, PaymentMethod, GiftCard} from '@/types'
@@ -41,6 +41,7 @@ interface Cart {
completed_steps: Array<string>;
requirements: Array<{step: string, field: string, message: string}>;
shipping_eq_billing_address: boolean;
+ warnings: Array<{code: string, message: string, line_item_id?: string, variant_id?: string}>;
discounts: Array<Discount>;
items: Array<LineItem>;
fulfillments: Array<Fulfillment>;
diff --git a/packages/sdk/src/types/index.ts b/packages/sdk/src/types/index.ts
index 24a8b5747c..71dd86799a 100644
--- a/packages/sdk/src/types/index.ts
+++ b/packages/sdk/src/types/index.ts
@@ -99,6 +99,9 @@ export interface CheckoutRequirement {
message: string;
}
+// Cart warning type — convenience alias for the inline type from the generated Cart
+export type CartWarning = Cart['warnings'][number];
+
// Hand-written domain types
export type {
LocaleDefaults,
diff --git a/packages/sdk/src/zod/generated/Cart.ts b/packages/sdk/src/zod/generated/Cart.ts
index bd95c675ba..bb3474b99f 100644
--- a/packages/sdk/src/zod/generated/Cart.ts
+++ b/packages/sdk/src/zod/generated/Cart.ts
@@ -46,6 +46,7 @@ export const CartSchema = z.object({
completed_steps: z.array(z.string()),
requirements: z.array(z.object({ step: z.string(), field: z.string(), message: z.string() })),
shipping_eq_billing_address: z.boolean(),
+ warnings: z.array(z.any()),
discounts: z.array(DiscountSchema),
items: z.array(LineItemSchema),
fulfillments: z.array(FulfillmentSchema),
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/cart_resolvable.rb b/spree/api/app/controllers/concerns/spree/api/v3/cart_resolvable.rb
index 4d2f98c2b5..d318b90b54 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/cart_resolvable.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/cart_resolvable.rb
@@ -26,7 +26,8 @@ module Spree
# Render the cart as JSON using the cart serializer.
def render_cart(status: :ok)
- render json: Spree.api.cart_serializer.new(@cart.reload, params: serializer_params).to_h, status: status
+ @cart = @cart.remove_out_of_stock_items!
+ render json: Spree.api.cart_serializer.new(@cart, params: serializer_params).to_h, status: status
end
# Render the order as JSON using the order serializer (for complete action).
diff --git a/spree/api/app/serializers/spree/api/v3/cart_serializer.rb b/spree/api/app/serializers/spree/api/v3/cart_serializer.rb
index 4a0efa425a..9a7da246cc 100644
--- a/spree/api/app/serializers/spree/api/v3/cart_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/cart_serializer.rb
@@ -20,6 +20,7 @@ module Spree
total: :string, display_total: :string,
amount_due: :string, display_amount_due: :string,
shipping_eq_billing_address: :boolean,
+ warnings: 'Array<{code: string, message: string, line_item_id?: string, variant_id?: string}>',
billing_address: { nullable: true }, shipping_address: { nullable: true },
gift_card: { nullable: true }
@@ -37,7 +38,7 @@ module Spree
:additional_tax_total, :display_additional_tax_total, :total, :display_total,
:gift_card_total, :display_gift_card_total,
:amount_due, :display_amount_due,
- :delivery_total, :display_delivery_total,
+ :delivery_total, :display_delivery_total, :warnings,
created_at: :iso8601, updated_at: :iso8601
attribute :store_credit_total do |order|
diff --git a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb
index 2261485a32..d0290a433c 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb
@@ -380,6 +380,54 @@ RSpec.describe Spree::Api::V3::Store::CartsController, type: :controller do
end
end
+ context 'warnings' do
+ let(:cart) { create(:order_with_line_items, store: store) }
+
+ before { request.headers['x-spree-token'] = cart.token }
+
+ it 'returns empty warnings array when cart is valid' do
+ get :show, params: { id: cart.prefixed_id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['warnings']).to eq([])
+ end
+
+ it 'returns warnings when a line item is out of stock' do
+ cart.line_items.first.variant.stock_items.update_all(count_on_hand: 0, backorderable: false)
+
+ get :show, params: { id: cart.prefixed_id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['warnings']).to be_an(Array)
+ expect(json_response['warnings'].length).to eq(1)
+
+ warning = json_response['warnings'].first
+ expect(warning['code']).to eq('line_item_removed')
+ expect(warning['message']).to be_present
+ expect(warning['variant_id']).to start_with('variant_')
+ expect(warning['line_item_id']).to start_with('li_')
+ end
+
+ it 'returns warnings when a product is discontinued' do
+ cart.line_items.first.product.update_columns(status: 'discontinued')
+
+ get :show, params: { id: cart.prefixed_id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['warnings'].length).to eq(1)
+ expect(json_response['warnings'].first['code']).to eq('line_item_removed')
+ end
+
+ it 'removes the line item when returning a warning' do
+ cart.line_items.first.variant.stock_items.update_all(count_on_hand: 0, backorderable: false)
+ original_item_count = cart.line_items.count
+
+ get :show, params: { id: cart.prefixed_id }
+
+ expect(json_response['items'].length).to eq(original_item_count - 1)
+ end
+ end
+
context 'auto-advance' do
let(:user) { create(:user_with_addresses) }
let(:cart) { create(:order_with_line_items, store: store, user: user) }
diff --git a/spree/api/spec/integration/spree/api/v3/store/carts_spec.rb b/spree/api/spec/integration/spree/api/v3/store/carts_spec.rb
index f54a9d92d4..7c53f58756 100644
--- a/spree/api/spec/integration/spree/api/v3/store/carts_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/store/carts_spec.rb
@@ -22,6 +22,18 @@ RSpec.describe 'Carts API', type: :request, swagger_doc: 'api-reference/store.ya
parameter name: 'x-spree-api-key', in: :header, type: :string, required: true
parameter name: 'Authorization', in: :header, type: :string, required: true
+ parameter name: :page, in: :query, type: :integer, required: false, description: 'Page number (default: 1)'
+ parameter name: :limit, in: :query, type: :integer, required: false, description: 'Number of results per page (default: 25, max: 100)'
+ parameter name: :sort, in: :query, type: :string, required: false,
+ description: 'Sort order. Prefix with - for descending. Values: created_at, -created_at, updated_at, -updated_at'
+ parameter name: 'q[created_at_gt]', in: :query, type: :string, required: false,
+ description: 'Filter by created after date (ISO 8601)'
+ parameter name: 'q[updated_at_gt]', in: :query, type: :string, required: false,
+ description: 'Filter by updated after date (ISO 8601)'
+ parameter name: :expand, in: :query, type: :string, required: false,
+ description: 'Comma-separated associations to expand (items, fulfillments, payments, discounts, billing_address, shipping_address, gift_card, payment_methods). Use "none" to skip associations.'
+ parameter name: :fields, in: :query, type: :string, required: false,
+ description: 'Comma-separated list of fields to include (e.g., total,amount_due,item_count). id is always included.'
response '200', 'carts listed' do
let!(:cart1) { create(:order_with_line_items, store: store, user: user) }
@@ -133,6 +145,10 @@ RSpec.describe 'Carts API', type: :request, swagger_doc: 'api-reference/store.ya
parameter name: 'Authorization', in: :header, type: :string, required: false
parameter name: 'x-spree-token', in: :header, type: :string, required: false
parameter name: :id, in: :path, type: :string, required: true, description: 'Cart prefixed ID (e.g., cart_abc123)'
+ parameter name: :expand, in: :query, type: :string, required: false,
+ description: 'Comma-separated associations to expand (items, fulfillments, payments, discounts, billing_address, shipping_address, gift_card, payment_methods). Use "none" to skip associations.'
+ parameter name: :fields, in: :query, type: :string, required: false,
+ description: 'Comma-separated list of fields to include (e.g., total,amount_due,item_count). id is always included.'
response '200', 'cart found' do
let(:cart) { create(:order_with_line_items, store: store) }
@@ -146,6 +162,27 @@ RSpec.describe 'Carts API', type: :request, swagger_doc: 'api-reference/store.ya
data = JSON.parse(response.body)
expect(data['id']).to start_with('cart_')
expect(data['number']).to eq(cart.number)
+ expect(data['warnings']).to eq([])
+ end
+ end
+
+ response '200', 'cart with out-of-stock item removed (warnings returned)' do
+ let(:cart) { create(:order_with_line_items, store: store) }
+ let(:'x-spree-api-key') { api_key.token }
+ let(:'x-spree-token') { cart.token }
+ let(:id) { cart.prefixed_id }
+
+ before do
+ cart.products.first.stock_items.update_all(count_on_hand: 0, backorderable: false)
+ end
+
+ run_test! do |response|
+ data = JSON.parse(response.body)
+ expect(data['warnings']).to be_an(Array)
+ expect(data['warnings'].length).to eq(1)
+ expect(data['warnings'].first['code']).to eq('line_item_removed')
+ expect(data['warnings'].first['message']).to be_present
+ expect(data['warnings'].first['variant_id']).to be_present
end
end
diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rb
index 6371335aef..6ab0c9d647 100644
--- a/spree/core/app/models/spree/order.rb
+++ b/spree/core/app/models/spree/order.rb
@@ -55,6 +55,20 @@ module Spree
alias_attribute :ship_total, :shipment_total
alias amount_due total_minus_store_credits
+ # Transient warnings populated by remove_out_of_stock_items!
+ attribute :warnings, default: -> { [] }
+
+ # Removes out-of-stock/discontinued items and populates warnings.
+ # Returns self (reloaded if items were removed) with warnings set.
+ def remove_out_of_stock_items!
+ result = Spree::Cart::RemoveOutOfStockItems.call(order: self)
+ return self unless result.success?
+
+ order, _messages, warnings = result.value
+ order.warnings = warnings || []
+ order
+ end
+
# 5.5 API naming bridges (DB columns rename in 6.0)
alias_attribute :discount_total, :promo_total
alias display_discount_total display_promo_total
diff --git a/spree/core/app/services/spree/cart/remove_out_of_stock_items.rb b/spree/core/app/services/spree/cart/remove_out_of_stock_items.rb
index 4f5c35d5bd..258648e8eb 100644
--- a/spree/core/app/services/spree/cart/remove_out_of_stock_items.rb
+++ b/spree/core/app/services/spree/cart/remove_out_of_stock_items.rb
@@ -5,8 +5,9 @@ module Spree
def call(order:)
@messages = []
+ @warnings = []
- return success([order, @messages]) if order.item_count.zero? || order.line_items.none?
+ return success([order, @messages, @warnings]) if order.item_count.zero? || order.line_items.none?
line_items = order.line_items.includes(variant: [:product, :stock_items, :stock_locations, { stock_items: :stock_location }])
@@ -17,9 +18,9 @@ module Spree
end
if @messages.any? # If any line item was removed, reload the order
- success([order.reload, @messages])
+ success([order.reload, @messages, @warnings])
else
- success([order, @messages])
+ success([order, @messages, @warnings])
end
end
@@ -28,7 +29,14 @@ module Spree
def valid_status?(line_item)
product = line_item.product
if !product.active? || product.deleted? || product.discontinued? || line_item.variant.discontinued?
- @messages << Spree.t('cart_line_item.discontinued', li_name: line_item.name)
+ message = Spree.t('cart_line_item.discontinued', li_name: line_item.name)
+ @messages << message
+ @warnings << {
+ code: 'line_item_removed',
+ message: message,
+ line_item_id: line_item.prefixed_id,
+ variant_id: line_item.variant.prefixed_id
+ }
return false
end
true
@@ -36,7 +44,14 @@ module Spree
def stock_available?(line_item)
if line_item.insufficient_stock?
- @messages << Spree.t('cart_line_item.out_of_stock', li_name: line_item.name)
+ message = Spree.t('cart_line_item.out_of_stock', li_name: line_item.name)
+ @messages << message
+ @warnings << {
+ code: 'line_item_removed',
+ message: message,
+ line_item_id: line_item.prefixed_id,
+ variant_id: line_item.variant.prefixed_id
+ }
return false
end
true
diff --git a/spree/core/spec/models/spree/order_spec.rb b/spree/core/spec/models/spree/order_spec.rb
index 50dab536c6..f087255d8d 100644
--- a/spree/core/spec/models/spree/order_spec.rb
+++ b/spree/core/spec/models/spree/order_spec.rb
@@ -2684,4 +2684,71 @@ describe Spree::Order, type: :model do
end
end
end
+
+ describe '#warnings' do
+ it 'defaults to empty array' do
+ expect(order.warnings).to eq([])
+ end
+
+ it 'is a transient attribute (not persisted)' do
+ order.warnings = [{ code: 'test', message: 'test' }]
+ order.save!
+ expect(order.reload.warnings).to eq([])
+ end
+ end
+
+ describe '#remove_out_of_stock_items!' do
+ let(:order) { create(:order_with_totals, store: store, user: user) }
+
+ context 'when all items are in stock' do
+ it 'does not remove any items' do
+ expect { order.remove_out_of_stock_items! }.not_to change { order.line_items.count }
+ end
+
+ it 'sets warnings to empty array' do
+ order.remove_out_of_stock_items!
+ expect(order.warnings).to eq([])
+ end
+ end
+
+ context 'when an item is out of stock' do
+ before do
+ order.line_items.first.variant.stock_items.update_all(count_on_hand: 0, backorderable: false)
+ end
+
+ it 'removes the out of stock item' do
+ expect { order.remove_out_of_stock_items! }.to change { order.reload.line_items.count }.by(-1)
+ end
+
+ it 'populates warnings with structured data' do
+ order.remove_out_of_stock_items!
+ expect(order.warnings.length).to eq(1)
+ expect(order.warnings.first[:code]).to eq('line_item_removed')
+ expect(order.warnings.first[:message]).to be_present
+ expect(order.warnings.first[:variant_id]).to start_with('variant_')
+ expect(order.warnings.first[:line_item_id]).to start_with('li_')
+ end
+ end
+
+ context 'when a product is discontinued' do
+ before do
+ order.line_items.first.product.update_columns(status: 'discontinued')
+ end
+
+ it 'removes the item and populates warnings' do
+ order.remove_out_of_stock_items!
+ expect(order.warnings.length).to eq(1)
+ expect(order.warnings.first[:code]).to eq('line_item_removed')
+ end
+ end
+
+ context 'when cart is empty' do
+ let(:order) { create(:order, store: store, user: user) }
+
+ it 'does nothing' do
+ order.remove_out_of_stock_items!
+ expect(order.warnings).to eq([])
+ end
+ end
+ end
end
diff --git a/spree/core/spec/services/spree/cart/remove_out_of_stock_items_spec.rb b/spree/core/spec/services/spree/cart/remove_out_of_stock_items_spec.rb
index f28905502b..ca31d2188b 100644
--- a/spree/core/spec/services/spree/cart/remove_out_of_stock_items_spec.rb
+++ b/spree/core/spec/services/spree/cart/remove_out_of_stock_items_spec.rb
@@ -6,39 +6,75 @@ RSpec.describe Spree::Cart::RemoveOutOfStockItems do
let(:store) { @default_store }
let(:user) { create(:user) }
let(:order) { create(:order_with_totals, store: store, user: user) }
+ let(:product) { order.products.first }
+ let(:variant) { order.variants.first }
let(:execute) { subject.call(order: order) }
it 'evaluate service to success' do
expect(execute).to be_success
end
- it 'removes line item and render discontinued flash message' do
- product = order.products.first
- product.update_columns(status: 'archive')
- expect(execute.value.last.to_sentence).to eq(Spree.t('cart_line_item.discontinued', li_name: product.name))
+ it 'returns empty messages and warnings when cart is valid' do
+ _order, messages, warnings = execute.value
+ expect(messages).to be_empty
+ expect(warnings).to be_empty
end
- it 'removes line item and render out of stock flash message' do
- product = order.products.first
- product.stock_items.update_all(count_on_hand: 0, backorderable: false)
- expect(execute.value.last.to_sentence).to eq(Spree.t('cart_line_item.out_of_stock', li_name: product.name))
+ context 'when product is archived' do
+ before { product.update_columns(status: 'archive') }
+
+ it 'removes line item and returns discontinued message' do
+ _order, messages, _warnings = execute.value
+ expect(messages.to_sentence).to eq(Spree.t('cart_line_item.discontinued', li_name: product.name))
+ end
+
+ it 'returns structured warning with line_item_removed code' do
+ _order, _messages, warnings = execute.value
+ expect(warnings.length).to eq(1)
+ expect(warnings.first[:code]).to eq('line_item_removed')
+ expect(warnings.first[:variant_id]).to be_present
+ end
end
- it 'renders discontinued flash message when line item is deleted' do
- product = order.products.first
- product.delete
- expect(execute.value.last.to_sentence).to eq(Spree.t('cart_line_item.discontinued', li_name: product.name))
+ context 'when product is out of stock' do
+ before { product.stock_items.update_all(count_on_hand: 0, backorderable: false) }
+
+ it 'removes line item and returns out of stock message' do
+ _order, messages, _warnings = execute.value
+ expect(messages.to_sentence).to eq(Spree.t('cart_line_item.out_of_stock', li_name: product.name))
+ end
+
+ it 'returns structured warning with line_item_removed code' do
+ _order, _messages, warnings = execute.value
+ expect(warnings.length).to eq(1)
+ expect(warnings.first[:code]).to eq('line_item_removed')
+ end
end
- it 'renders discontinued flash message when line item is discontinued' do
- product = order.products.first
- product.update_columns(status: 'discontinued')
- expect(execute.value.last.to_sentence).to eq(Spree.t('cart_line_item.discontinued', li_name: product.name))
+ context 'when product is deleted' do
+ before { product.delete }
+
+ it 'removes line item and returns discontinued message' do
+ _order, messages, _warnings = execute.value
+ expect(messages.to_sentence).to eq(Spree.t('cart_line_item.discontinued', li_name: product.name))
+ end
end
- it 'renders discontinued flash message when a variant is discontinued' do
- variant = order.variants.first
- variant.discontinue!
- expect(execute.value.last.to_sentence).to eq(Spree.t('cart_line_item.discontinued', li_name: variant.product.name))
+ context 'when product is discontinued' do
+ before { product.update_columns(status: 'discontinued') }
+
+ it 'removes line item and returns discontinued message' do
+ _order, messages, _warnings = execute.value
+ expect(messages.to_sentence).to eq(Spree.t('cart_line_item.discontinued', li_name: product.name))
+ end
+ end
+
+ context 'when variant is discontinued' do
+ before { variant.discontinue! }
+
+ it 'removes line item and returns discontinued message' do
+ _order, messages, _warnings = execute.value
+ expect(messages.to_sentence).to eq(Spree.t('cart_line_item.discontinued', li_name: variant.product.name))
+ end
end
end