Shopping Basket

 x 
Cart empty


Popular Items

Find Us Elsewhere

Custom Fields With Virtuemart 2

fieldsCreating custom fields with Virtuemart 2 can be (thankfully) quite a lot more straightforward than with previous versions. But there are also far more options available with custom fields, including the scope for creating specialist plugin fields.

The disadvantage is that there is no longer any support for 'product types', which could be a useful feature of Virtuemart 1.1x, however the option for creating custom plugins may offer more flexibility as developers produce applications in the future. We will discuss plugin fields in more depth in a later article, for now we will look at simpler usage of custom fields.

 

 

Simple Custom Fields

Firstly, we will consider the process of creating a simple custom field to convey extra information about the product, such as the age group it is aimed at. Our age group field will be displayed on the product page to give shoppers that information in a standard format.

Creating a Custom Field Type

To do this, in your Virtuemart admin, you go to products->custom fields, and click the 'new' button. You will first need to select the custom field type, the types available are: string, integer, parent, boolean, date, time, image, cart variant, generic cart variant, editor, textarea, plugins. For a simple information field such as 'age group', choosing the 'string' field type will be sufficient. We will discuss some of the complex field types below.

Give the field a title ('age group' for example). It helps if the title is meaningful, because this is the field name that will be shown when a custom field is selected for a product. You can leave the rest of the options as they are, although you may find it helpful to add a description and a default value. These will be visible on the product details page as well as in the product admin.

If you are creating a page that will be of use only to administrators you can set the 'hidden' option. For example you can use custom fields to supply additional product information with our GooglebaseXML component, to create a product feed that can be uploaded to Google. You may not necessarily want to display the information on the product page, using a hidden field can allow you to tailor the information you submit to Google without having to display it to customers.

Leave the cart attribute option at 'no' for now, we will discuss cart attributes below.

Click 'save and close'. You have now created a simple custom field type, you just need to add it to your products.

Adding a Custom Field to a Product

Go to products->products in your Virtuemart admin, and click on the name of the product in order to open its editor. Click on the 'custom fields' tab. Go down to the box labelled 'custom field type'. Click on the selector, and select the name of the field that you want to add (if you have not created any other fields yet there will only be one name). The new field should now be automatically created. Type in its value in the value box (eg 0 to 4 in our age group example), and click 'save and close'.

You have now created a custom field type, and added an instance of the field to a product. You can add the same field type (such as our 'age group' example) to as many products as you wish, you only need to create it once.

Creating more complex field types simply involves modifying the above process, such as creating list options.

Creating List Options

Rather than simply creating the age group field type as a single value that will be added for each product, you might want to specify a list of predefined age groups (0 to 4, 5 to 9, 9 to 14 etc), from which the relevant age group will be selected for a particular product. To do this, you can modify the custom field type. Go to products->custom fields in your Virtuemart admin, and click on the 'age group' field type. Change the 'is a list?' option to 'yes', and change the default value to a list of the possible values, separated by semi-colons, for example '0 to 4; 5 to 9; 9 to 14; 14+'. Click 'save and close'.

Now go back to the products list, find your product, open the product details editor, and click on the custom field tab. You will need to delete the existing age group field (click the x button next to it). Now click on the custom field type box and add the field again, as described above in 'adding a custom field to a product'. This time you should see that the value of the field now appears as a drop-down list, from which you can select the value.

Cart Attributes

So far we have looked at custom fields which are not cart attributes. The distinction is important. Ordinary, non-cart attribute fields will simply appear as extra information on the product page. Cart attribute fields on the other hand will be displayed when the customer views his or her shopping cart, and also near the add to cart button on the product page. They can optionally have a custom price attached to them, providing a way to create a set of selectable options that will cause the final price to vary.

For example, suppose your product comes in three sizes: small, medium and large, and the price varies according to the size. You want the add to cart button to include a selector that allows the customer to choose the preferred size, and display the amended price.

First you need to create your size field type. As before, go to products->custom fields, and click the 'new' button. Give the field the title 'size'. This time we need to give the custom field the type 'cart variant'. Set the 'is a cart attribute' and 'is a list' options to 'yes'. Set the default value to 'small;medium;large'. Click 'save and close'.

Open your product in the product editor and click on the 'custom fields' tab as before. Now in the custom field type selector box click on 'size' to add the size field. You should see a drop down list of the available sizes in the value column, and a text box in the 'price' column. Select a size value (eg small), and add the appropriate price.

Now add another size field using the custom field type selector box, give it a size value (eg medium) and a price. You can do this as for as many times as you have available size options. When you have finished click on the 'save and close' button. Now when the product is viewed by a shopper they should see a drop down list box with the available sizes near to the add to cart button. Selecting a size should result in the product price being updated.

Note that the custom price values will be added to the product base price, so you may find it convenient to set the base price equal to the lowest value option, and give that option a custom price of zero. For example, if your prices are 25 for size small, 30 for medium and 40 for the large size, your base price would be 25. You would give the small size a custom price of zero, the medium size a price of 5, and the large size a price of 15.

Other Field Types

As we saw above, there are many other options available for field types. Some, such as the integer and boolean types allow you to restrict the value of the field to particular data types, such as numbers or yes/no values. Parent fields allow you to group other fields together and add them to products as a group. The most interesting is the 'plugin' field type since this allows for the use of 3rd party extensions to create additional product attributes. Because it is such a large subject area we will discuss plugins separately in a future article.

Author Profile