elixir - Product Line Item Varient Schema -
my end goal here when admin creates product, can choose variants available selection end user when adding cart, multiple choices, 1 choice. example, 1 size only, small, medium, etc.
here's product schema:
schema "products" field :category, :string field :description, :string field :image, qcrafts.images.type field :name, :string field :price, :decimal field :slug, productslug.type timestamps() end and line_item:
embedded_schema field :product_id, :integer field :image, qcrafts.images.type field :product_name, :string field :quantity, :integer field :unit_price, :decimal field :total, :decimal field :delete, :boolean, virtual: true end which relationships need suits best , whats best way handle on template?
without changing stuff much, think this:
defmodule product use ecto.schema schema "products" field :category, :string field :description, :string field :name, :string field :slug, productslug.type has_many :variants, productvariant belongs_to :default_variant, productvariant timestamps() end end defmodule productvariant use ecto.schema schema "product_variants" field :product_id, :id field :description, :string field :image, qcrafts.images.type field :name, :string field :price, :decimal field :slug, productslug.type timestamps() end end defmodule lineitem use ecto.schema embedded_schema field :product_variant_id, :id field :image, qcrafts.images.type field :product_name, :string field :quantity, :integer field :unit_price, :decimal field :total, :decimal field :delete, :boolean, virtual: true end end in template like: (make sure preload)
form example:
multiple_select(form, :variants, enum.map(@product.variants, &({&1.name, &1.id})), selected: enum.map(@product.variants, &(&1.id))) or list example:
<ul> <%= variant <- @product.variants %> <li><%= variant.name %></li> <% end %> </ul>
Comments
Post a Comment