One of the most powerful features of Drupal Commerce is the ability to add custom fields to your products. This allows you to collect and display additional information about your products, such as dimensions, weight, and material. Furthermore, custom fields can be used to create complex pricing rules and product variations.
In this article, we’ll show you how to add custom fields to your Drupal Commerce products. We’ll cover the following topics:
- Understanding custom fields
- Creating custom fields
- Adding custom fields to product types
- Displaying custom fields on products
- Using custom fields in pricing rules
- Creating product variations with custom fields
By the end of this article, you’ll be able to create custom fields that improve your customers’ shopping experience and streamline your store management.
Table of Contents
Understanding Custom Fields
Before we dive into the technical details, let’s define what custom fields are and why they are useful. A custom field is a piece of information that is not included in the default product fields that Drupal Commerce provides. For example, a product may have a "SKU" field, but if you want to track a specific manufacturer’s product number, you may want to create a custom field for that.
Custom fields can be of various types, including text fields, date fields, select lists, checkboxes, and more. Each custom field can have its own settings, such as the maximum length for a text field or the available options for a select list.
Custom fields are created and managed through Drupal’s "Field UI" module, which is available in the core installation. The Field UI allows you to create and configure fields for different content types, including products.
Creating Custom Fields
To create a custom field, you’ll need to access the Field UI. To do so, go to "Structure" > "Content types" and click on "Manage fields" for the product type you want to add the custom field to.
Once you’re in the "Manage fields" screen, click on "Add field". You’ll be prompted to give your custom field a name and choose its type. For example, if you wanted to create a custom field to store a product’s material, you could name it "Material" and choose the "Text" type.
Next, you’ll see a screen where you can set additional settings for your custom field. Depending on the field type, you may be able to set a maximum length, default value, and allowed values.
Once you’ve configured your custom field, click "Save" to create it. Your custom field is now available to be used in any product of the type you’ve added it to. Let’s see how to add it to a product now.
Adding Custom Fields to Product Types
To add a custom field to a product type, you’ll need to go to "Structure" > "Content types" and click on "Manage form display" for the product type you want to add the custom field to.
In the "Manage form display" screen, you’ll see a list of available fields for your product type. Find your custom field in the list and drag it to the appropriate location on the form display. For example, if you’ve created a custom field for a product’s dimensions, you may want to place it near the "Weight" field.
Once you’ve added your custom field to the form display, click "Save" to update it. Your custom field is now visible for any new products of the type you’ve added it to. You’ll also be prompted to fill in the custom field values when creating or editing a product.
Displaying Custom Fields on Products
Now that you’ve created and added your custom field to your product type, you may want to display it on your product pages. To do so, you’ll need to go to "Structure" > "Display modes" and click on "Custom display settings" for the product type you want to add the custom field to.
In the "Custom display settings" screen, you can customize how your product is displayed for different view modes, such as "Full content" or "Teaser". Find the view mode you want to add your custom field to and click "Save".
Next, in the "Manage display" screen, you’ll see a list of available fields that can be displayed for your product type. Find your custom field in the list and drag it to the appropriate location on the display. For example, you may want to place your custom field near the "SKU" field.
After you’ve added your custom field to the display, click "Save" to update it. Your custom field is now visible on your product pages for the view mode you’ve added it to.
Using Custom Fields in Pricing Rules
Custom fields can be used to create complex pricing rules for your products. For example, if you have a custom field for a product’s color, you could create a pricing rule that discounts all "red" products by 10%.
To create a pricing rule that uses a custom field, go to "Store" > "Configuration" > "Pricing rules" and click on "Add pricing rule". Give your pricing rule a name and choose a trigger. For example, you could create a pricing rule that is triggered when a customer adds a product to their cart.
Next, in the "Conditions" section, click on "Add a condition". Choose "Data comparison" as the condition type and select your custom field as the data to compare. Define your comparison values as needed. For example, if you wanted to discount all products with a "color" field set to "red", you would compare the "color" field to the value "red".
Finally, in the "Actions" section, click on "Add an action". Choose "Apply percentage discount to product’s price" as the action type and define your discount value. For example, if you wanted to discount all "red" products by 10%, you would set the discount value to "10".
Click "Save" to create your pricing rule. Your custom field is now used in the pricing rule and will apply the discount to all products that meet the condition you’ve defined.
Creating Product Variations with Custom Fields
Custom fields can also be used to create product variations with different attribute values. For example, if you have a custom field for a product’s size and color, you could create product variations for different size and color combinations.
To create product variations with custom fields, go to "Structure" > "Content types" and click on "Manage fields" for the product type you want to add variations to.
In the "Manage fields" screen, click on "Add field" and choose "Product variation" as the type. Give your product variation field a name and click "Save".
Next, in the "Manage form display" screen, drag your product variation field to the appropriate location on the form display. You’ll see a list of available attributes for your product variations. Find your custom field and check the box next to it.
Now, when you create or edit a product, you’ll see a "Product variations" section where you can define the different attribute values for your variations.
Conclusion
Adding custom fields to your Drupal Commerce products is a powerful way to collect and display additional information, create complex pricing rules, and create product variations with different attribute values. In this article, we’ve covered how to create custom fields, add them to product types and displays, and use them in pricing rules and product variations.
By taking advantage of custom fields, you can effectively communicate your products’ unique attributes to your customers and streamline your store management.