- Magento Beginner's Guide(Second Edition)
- Robbert Ravensbergen Sander Schoneville
- 1436字
- 2021-08-13 17:05:28
Time for action – creating your own attribute
We are going to create an attribute to be able to enter TV screen sizes, from our previous example.
- Click on the button in the top-right corner, Add New Attribute:
- A new form appears that is divided into two parts: the Attribute Properties and the Frontend Properties. We'll start with Attribute Properties:
- First, choose a name for your attribute, in the Attribute Code field. Use a name that makes sense: that describes what this attribute will be used for. The name must be unique. In our example, we use screensize for the screen sizes of the TVs we are going to sell. Do not use capitals or spaces in your name.
- Next, select the scope for your attribute. Now, at the first thought you would think, if the screen size of a TV is 42 inches on one website, it is likely that it will have the same size if I use the product on a different website, isn't it? Well, yes, but don't go too fast. If I'm selling my TVs in more than one country, it could be that in one of my stores I'll show the screen size in inches, whereas in another store I'll show it in centimeters. I don't want risk that now so I'll leave the Scope field to Store View:
- The third field, Catalog Input Type for Store Owner, is an important one. Using this setting you determine how the values for this field should be entered, once you're going to create products with this attribute in it. You may choose between a Text Field or Text Area types; for list prices you use the Price type. Or use a simple Yes/No (Boolean) type for your attribute.
Note
Note that not all types can be used for layered navigation. The Dropdown field that we've chosen here can be used for layered navigation. But even more important, it gives us the possibility to preenter the possible values, so that no typographical errors will be made. TV screens generally come in fixed sizes. A TV screen is, for instance, 32, 40, or 42 inches. I've never seen one of 35.5 inches. Because of this behavior the screensize attribute is an excellent candidate for the Dropdown field.
- Next, we set the Unique Value field to No. If you set it to Yes, every product using that attribute will need a unique value in that field, so no duplicates are allowed. A typical example of such a field is the item number (SKU).
- Values Required simply determines whether a field can be left empty when creating a product. If this field is set to Yes, the user must make a choice.
- Input Validation for Store Owner is not required in our example, but this field can be used to make sure that the entered value follows a certain rule. For example, if the value entered by the user needs to be a number, you may choose to check for Integer (a number without any decimals) or Decimal (a number that can include decimals) input validation.
- Finally, the Apply to field lets you decide for which kind of products this attribute could be useful. We'll explain more on different product types in Chapter 4, Simple Products, and Chapter 5, Beyond Adding Simple Products. For now, just keep the default value, which makes the attribute available for all kinds of products.
- The next part of our attribute form contains the frontend parameters. Using these we can set the behavior of our attribute and decide what the website visitor can and cannot do with it:
- To let the user search for this product attribute, set the fields Use in Quick Search and Use in Advanced Search both to Yes.
- Set the Comparable on Front-end field to Yes to let the user compare several products using this attribute. In our example, it makes sense to let the user compare various TVs, where the screen size is one of the fields that will be shown in the comparison.
- We also chose to set the Use in Layered Navigation to Filterable (with results) field. This means that the visitor of the website can use Magento's layered navigation function to select all products with a specific screen size, which makes the product selection user-friendly. There are two other possibilities for this field, which we will explain:
- Filterable (no results) has almost the same result. The difference being that, in our situation, the layered navigation would show all screen sizes available, even those for which we have no products.
- Filterable (with results) will only show screen sizes for which we actually have products. The third possibility for this field is No, which simply means that the attribute will not be shown in the layered navigation on the frontend.
- If Use In Search Results Layered Navigation is set to Yes, Magento adds this attribute to the layer navigation panel on the frontend when products bearing this attribute are displayed in the search results.
- We haven't spoken yet about Magento's possibilities to organize promotions/discounts. By setting the Use for promo Rule Conditions field to Yes, it would in our case give us the possibility to do a promotion that says, "10% Off on all 40" LED TVs!" We want to keep that option open, so we say Yes.
- The Position field can be used to sort the various attributes in the layered navigation in a particular order. Does it make sense to let a user first select the screen size, and then further drill down by price? Set a number in this field (the lowest number shows on top).
- Allow HTML Tags on Frontend is especially helpful if you have text fields that could be filled using HTML codes (such as a description, for example). In our situation we do not need it.
- Visible on Product View Page on Frontend will be set to Yes to make sure that the screen size is actually visible to users looking for specifications for our products. An example of those product details can be found in the following screenshot.
Note
Please note that setting this field to No means that the attribute can still be used, but the visitor of your website won't see it in the frontend. The following example shows three more product attributes: the Description, the Contrast Ratio and the Response Time for a computer monitor.
- The last two fields can be used to determine if your attribute will be visible in the List view and if your attribute can be used to sort products. We set the last field to Yes, because it sometimes makes sense for visitors to sort TVs from a smaller to a larger screen size.
Note
Please note that its working actually depends on your Magento theme. In the standard theme, it will be no problem of course, but in custom themes, this function (among others) must actually be added by the programmer of the theme.
- Aren't we there yet? No, although we just need to take the last steps. We've done everything for our attribute besides two things: we haven't specified the labels yet. The attribute label is the name that the user will see in the frontend. It makes sense that this can differ from store view to store view, especially when you are working with multiple languages. Besides that, we also need to specify the possible values for our drop-down list:
The preceding example shows what you should do. First, specify the labels. Of these, the label for the Admin area (that is the Magento backend) is mandatory. If you ever use this attribute in a store view for which no label has been defined, the Admin value would be used.
Underneath the labels, we can specify all options for our drop-down list. Click on the Add Option button to add a new row and fill all values according to your needs.
Note
Note that it is possible to enter the values per store view again. In the preceding example, this means that if the user creating the product selects a value of 22inches, our visitors in the frontend will actually see 22". And if I had a second store view, I could easily show 56cm to visitors from a country using that particular metric.
- That's it. Just click on Save Attribute on the top of the page.
Note
If we were to really start selling TVs, we probably also would want to create additional attributes such as dimensions, weight, type, and so on. In that case, just repeat the process mentioned to create the needed attributes.