Frontend – Adding Weights To Products

During the creation of a food and drink vendors site, it became apparent that some products needed to be displayed with a weight in grammes or kilogrammes, but yet others were measured in volume so litres were the necessary values to display to the potential customers.

I am sure that there are numerous ways of doing this, however, let us consider 2 based on 2 different scenarios:
1) The default weight attribute is not used for calculating delivery costs and therefore can be used with as we see fit.
2) Weight is used to calculate delivery and a duplication value will be established for the benefit of visitors.

So firstly we consider:
Situation 1 – Weight attribute is not used for calculating delivery costs
Log into the Magento Administrator for the site in question. Catalog -> Attribute -> Manage Attributes.
Add New Attribute.

Admin -> Catalog -> Attributes -> Manage Attributes -> Add New Attribute -> Properties

Note that it is especially important to define the ‘Used in Product Listing’ option as Yes. This will allow us to make changes to the theme page to output this value.

Admin -> Catalog -> Attributes -> Manage Attributes -> Add New Attribute -> Manage Label / Options

This has created a measurement attribute that can be assigned to a product, and from that attribute we have defined 4 possible values to select from.

We need to assign this attribute to the particular Attribute Sets which have been defined in the Magento store backend, and are to be used for products that will display their weight on the frontend.

Catalog -> Attribute -> Manage Attribute Sets. Select the appropriate Attribute Set, in this example we will use Default.

Initially the new Measurement Attribute will appear in the Unassigned Attributes area and this simply needs to be dragged into the Groups section to the required point in the list of existing Attributes. Here we have added it below weight in the General section:

Admin -> Catalog -> Attributes -> Manage Attribute Set -> Default -> Groups

Now that the Attribute has been added to the Attribute Set, it only remains to define the Attribute in a product that belongs to the Attribute Set which has had the Attribute added.

Admin -> Catalog -> Manage Products -> {product} -> {general}

This has allowed for a measurement to be associated. However, this value will not be shown as the code is not aware of the existence of this Attribute. We will need to add this.

In the filesystem, open /app/design/frontend/default/{theme}/template/catalog/product/list.phtml. Note that if this file does not exist in the theme that is being used, copy this file from /app/design/frontend/default/base/template/catalog/product/list.phtml. Once this file has been copied, it can then be modified. Please note that the base file should never be altered. Also, be aware that numerous files will need to be altered to include this change, list.phtml has been selected as it shows a number of products quickly and simply.

In this file, you can retrieve the value of the {measurement} Attribute with the code:

    // Note that in this code sample {measurement} should be replaced with your Attribute name
    echo ($_product->getAttributeText('{measurement}'));

This can be paired with the weight Attribute that is part of the default installation, as shown in the following code sample:

    // Note that in this code sample {measurement} should be replaced with your Attribute name
    echo ('<div class="weight">' . $this->__('Weight') . ': ' . $_product->getWeight() . $_product->getAttributeText('{measurement}') . '</div>');

Note, it is possible to use $_product->getData(‘measurement’) to recover the numerical value associated this will retrieve a numerical value which is associated to the option and not the label that is associated to the Attribute.

Situation 2 – Weight is used to calculate delivery
With weight being required for calculating delivery costs we can simply create a new Attribute (please see the steps above for more information on achieving this), labelled display_weight or something similar.

Add the new Attirbute to the Attribute Sets associated to the products. In each of the appropriate products this attribute will need to be given a value.

Apply the steps above for creating the measurement Attribute. Once these have been created and assigned values in the products the only thing that remains to be changed is that in the front end files that display products will need to include code similar to the following:

    // Note that in this code sample {display_weight} and {measurement} should be replaced with your Attribute names
    echo ('<div class="weight">' . $this->__('Weight') . ': ' . $_product->getData('{display_weight}') . $_product->getAttributeText('{measurement}') . '</div>');

Additional information:
The weight Attribute may not show, or will show as a 0.00 value. This would probably be due to the fact that the Used in Product Listing, Attribute setting is set to No. Go to Admin -> Catalog -> Attributes -> Manage Attributes. Select weight and change this setting in the Properties tab.

If it is desired to interpret each of the various outputs for measurement it is possible to obtain the values in the following way:
Direct Database Access:
It is possible to either get the value directly from the database. This can be achieved with a couple of steps.
a) Get the attribute_id: SELECT * FROM `eav_attribute` WHERE attribute_code = ‘measurement’. Record the a attribute_id.
b) Use this attribute_id to get the option_id: SELECT * FROM `eav_attribute_option` WHERE attribute_id = ‘162’. Record the option_id values.
It is possible to do this in alternative ways, but this way helps more easily see the relationship between the tables in the database.

File Access:
Alternatively, it would be possible to set products to the various Attribute options and add the folioing to the top of the /app/design/frontend/default/{theme}/template/catalog/product/list.phtml file.

    // Note that in this code sample {display_weight} and {measurement} should be replaced with your Attribute names
    echo ('<br />measurement value: ' . $_product->getData('{measurement}'));


    // Note that in this code sample {display_weight} and {measurement} should be replaced with your Attribute names
    echo ('<br />measurement value: ' . $_product->getAttributeText('{measurement}'));

Then view each of the products that have had the measurement Attribute set, and record the output shown on the product page.

It is now possible to display each of the various measurement types independently.

Leave a Reply

Your email address will not be published. Required fields are marked *