Lena Lena - 2 months ago 10
PHP Question

Show unit of weight in product page for Opencart 2

Hi!

How to display the unit of weight (kg., gr., oz., ml.,) in product page in Opencart 2.0.2.0

Now only displays a number without units.

I'm Using the default template

in file template\product\product.tpl it looks like this:

...<li><?php echo $text_model; ?> <?php echo $model; ?></li>

<?php if ($weight) { ?>

<li><?php echo $text_weight; ?>
<?php
echo round($weight, 2)?></li>
<?php } ?> ...


in

catalog\controller\product\product.php

I have added the line

$data['text_weight'] = $this->language->get('text_weight');

$data['weight'] = $product_info['weight'];


what else need to add or perhaps change ?

Answer

Try something like in default OpenCart code.

Step 1

Open file catalog/controller/product/product.php

Find (around line 242):

$data['text_tags'] = $this->language->get('text_tags');

Add after :

$data['text_weight'] = $this->language->get('text_weight');


Step 2

In the same file catalog/controller/product/product.php

Find (around line 270):

$data['points'] = $product_info['points'];

Add after :

$data['weight'] = $product_info['weight'];
$tablewunit = $this->db->query("SELECT wcd.unit FROM " . DB_PREFIX . "weight_class_description wcd WHERE (wcd.weight_class_id = " . $product_info['weight_class_id'] . ") AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
$data['weightunit'] = $tablewunit->row['unit'];


Step 3

Open file catalog/language/english/product/product.php

Find (around line 6):

$_['text_model']               = 'Product Code:';

Add after :

$_['text_weight']       = 'Weight:';


Step 4

Open file catalog/view/theme/default(your theme)/template/product/product.tpl

Find (around line 141):

<li><?php echo $text_model; ?> <?php echo $model; ?></li>

Add after :

<li><?php echo $text_weight; ?> <?php echo $weight . ' ' . $weightunit; ?></li>

and then check it.

Comments