PhoenixKit.Modules.Shop.CartItem (phoenix_kit v1.7.71)

Copy Markdown View Source

Cart item schema with price snapshot for consistency.

When a product is added to the cart, we snapshot the current price and product details. This ensures that:

  1. Price changes after adding don't affect the cart total unexpectedly
  2. If the product is deleted, we still have the title and other info
  3. We can show users when prices have changed since they added items

Fields

  • cart_uuid - Reference to the cart (required)
  • product_uuid - Reference to the product (nullable, ON DELETE SET NULL)
  • product_title - Product title snapshot (required)
  • product_slug - Product slug snapshot
  • product_sku - Product SKU snapshot
  • product_image - Product image URL snapshot
  • unit_price - Price per unit at time of adding (required)
  • compare_at_price - Original price for showing discounts
  • quantity - Number of items (required, > 0)
  • line_total - Calculated: unit_price * quantity
  • weight_grams - Weight for shipping calculation
  • taxable - Whether item is taxable
  • selected_specs - JSON object for specification-based pricing (e.g., {"material": "PETG", "color": "Gold"})

Summary

Functions

Changeset for cart item creation and updates.

Returns discount percentage for sale items.

Creates changeset attributes from a product.

Returns true if this item is on sale (has compare_at_price > unit_price).

Returns the price difference if the product price has changed. Positive = price increased, Negative = price decreased.

Returns true if product data has changed since the item was added. Useful for showing price change warnings.

Returns true if the product has been deleted (product_uuid is nil after SET NULL).

Functions

changeset(item, attrs)

Changeset for cart item creation and updates.

discount_percentage(item)

Returns discount percentage for sale items.

from_product(product, quantity \\ 1, language \\ nil)

Creates changeset attributes from a product.

Parameters

  • product - The Product struct
  • quantity - Number of items (default: 1)
  • language - Language code for localized fields (default: system default)

Examples

iex> from_product(product, 2)
%{
  product_uuid: "01234567-...",
  product_title: "Widget",
  product_slug: "widget",
  unit_price: Decimal.new("19.99"),
  quantity: 2,
  ...
}

iex> from_product(product, 1, "ru")
%{product_title: "Виджет", product_slug: "vidzhet", ...}

on_sale?(cart_item)

Returns true if this item is on sale (has compare_at_price > unit_price).

price_difference(item, product)

Returns the price difference if the product price has changed. Positive = price increased, Negative = price decreased.

product_changed?(item, product)

Returns true if product data has changed since the item was added. Useful for showing price change warnings.

product_deleted?(arg1)

Returns true if the product has been deleted (product_uuid is nil after SET NULL).