APIDeveloperUserReleasesDemoContact
  • Core

    • Stores
    • Orders
    • Products
    • Payments
    • Shipments
    • Inventory
    • Promotions
    • Adjustments
    • Taxation
    • Calculators
    • Addresses
    • Preferences
  • Customization

    • Storefront
    • Templates
    • Assets (JS & CSS)
    • Images
    • Dependency system
    • Business Logic
    • Checkout Flow
    • Permissions
    • API v2
    • API v1
    • Authentication
    • Internationalization
    • Extensions
    • Emails
  • Security

    • Security
    • PCI Compliance
    • Authentication and Authorization
    • API
  • Source

    • About the Code
    • Getting Help
    • Navigating the Source
  • Tutorials

    • Getting Started with Spree
    • Add Spree to an existing Ruby on Rails application
    • Developing Spree
    • Improve SEO
    • Creating an Extension
    • Deface Overrides
    • Extend Product Attributes
    • Testing Spree Applications
    • Updating Extensions to Rails 6 and Spree 4
  • Upgrades

    • Upgrading Spree 4.1 to 4.2
    • Upgrading Spree 4.0 to 4.1
    • Upgrading Spree 3.7 to 4.0
    • Upgrading Spree from 3.6 to 3.7
    • Upgrading Spree from 3.5 to 3.6
    • Upgrading Spree from 3.4 to 3.5
    • Upgrading Spree from 3.3 to 3.4
    • Upgrading Spree from 3.2 to 3.3
    • Upgrading Spree from 3.1 to 3.2
    • Upgrading Spree from 3.0 to 3.1

Table Of Contents

OverviewExtend your ProductExtend Product AttributesMake it available in your APIAdd Fields to your admin interfaceGotchas, Known Issues, and Further Considerations

Extend Product Attributes

Overview

If you need to add more attributes to your product model you can easily extend spree. In this example you are going to extend your product with an short_descripton and make it manageable through admin.

Note: Replace RailsappName and railsapp_name with your actualy Rails App Name (sic!)

Extend your Product

Extend Product Attributes

Create a migration that extends your spree products table. rails g migration AddFieldsToSpreeProducts short_description:text should create this migration

class AddFieldsToSpreeProducts < ActiveRecord::Migration[6.0]
  def change
    add_column :spree_products, :short_description, :text
  end
end

This file extends your actual Product Serializer to make the attributes available in your APP.

module RailsappName
  module Spree
    module ProductDecorator
      def self.prepended(base)
        base.attributes :short_description
      end
    end
  end
end

::Spree::V2::Storefront::Product.prepend RailsappName::Spree::ProductDecorator if ::Spree::V2::Storefront::Product.included_modules.exclude?(RailsappName::Spree::ProductDecorator)

File Location: app/models/railsapp_name/spree/product_serializer_decorator.rb (Does not exist by default)

Make it available in your API

This file extends your actual Product Serializer to make the attributes available in your API.

module RailsappName
  module Spree
    module ProductSerializerDecorator
      def self.prepended(base)
        base.attributes :short_description
      end
    end
  end
end

::Spree::V2::Storefront::ProductSerializer.prepend RailsappName::Spree::ProductSerializerDecorator if ::Spree::V2::Storefront::ProductSerializer.included_modules.exclude?(RailsappName::Spree::ProductSerializerDecorator)

File Location: app/serializers/railsapp_name/spree/product_serializer_decorator.rb (Does not exist by default)

Add Fields to your admin interface

Create Template for Admin Form

<div data-hook="admin_product_form_short_description">
  <%= f.field_container :short_description, class: ['form-group'] do %>
    <%= f.label :short_description %>
    <%= f.error_message_on :short_description %>
    <%= f.text_area :short_description, class: 'form-control', placeholder: 'Am besten drei Bullet Points' %>
  <% end %>
</div>

File Location: app/views/spree/admin/products/_product_custom_fields.html (Does not exist by default)

Inject your Template to Admin Panel

Deface::Override.new(:virtual_path => "spree/admin/products/_form",
  :name => "product_custom_fields_admin_product_form_right",
  :insert_after => "[data-hook='admin_product_form_description']",
  :partial => "spree/admin/products/product_custom_fields",
  :original => "eb9ecf7015fa51bb0adf7dafd7e6fdf1d652025d",
  :disabled => false)

File Location: app/overrides/add_custom_fields_product_admin_tabs.rb (Does not exist by default)

Gotchas, Known Issues, and Further Considerations

This extends your product just with simple fields and makes them available on your master product. Please be advised for more complex extensions it might make sense to create new tabs in your admin panel or manage this through a relationship.

Propose changes to this page
Maintained bySpree Commerce & Ruby on Rails developers© Spree Commerce. 2021 All Rights Reserved.