Add A Color Picker Attribute

In this post we will see how you can add a color picker attribute in Magento 2. Useful in case you want to personalise products or category pages. Lets' check it together.

We are gonna create a category attribute using a data patch like below. The attribute is defined as text.

After that we have to add it in category form. There is where we define that formElement and componentType of the field to be ColorPicker. The ColorPicker component uses the Spectrum and tinycolor.js libraries to make it easier to choose and implement color values. ColorPicker options that can be defined are the colorFormat, colorPickerMode, label and the elementTmpl which has the default value of ui/form/element/color-picker. Option colorFormat can have one of the values ex, rgb, hsl, hsv, name and none. Option colorPickerMode can have one of the values simple, full, noalpha and palette. In our example as you can see below, we set colorFormat to hex and colorPickerMode to full.

As we added the attribute in the group Content, in you navigate to a category in admin you will see our attribute like below.

The full example as extension can be found here.

Feel free to share this post and ask your questions in the comments below.

Till next time!

DigitalOcean Referral Badge