ROZZ
ROZZ

Reputation: 1454

OpenCart show weight units in product page 1.5.6

How can I show the the weight (with units) in product page in 1.5.6. Example: Weight - 2.5 kg; Weight - 250 mg ... I've been reading some articles, but nothing working correctly.

Upvotes: 2

Views: 1295

Answers (3)

Aarif Nabi Roshangar
Aarif Nabi Roshangar

Reputation: 29

go to catalog/language/english/product/product.php,Find:

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

Add below code after it

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

open catalog/controller/product/product.php, Find:

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

Add below code after it:

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

In same file search for the code:

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

Paste below code after it:

$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['weight_unit'] = $tablewunit->row['unit'];

Now open catalog/view/theme/---yourtheme---/template/product/product.tpl, and find:

<li><?php echo $text_stock; ?> <?php echo $stock; ?></li>

Add below code after it:

<td><b><?php echo $text_weight; ?></b></td>
 <td><?php echo round ($weight, 2) . ' ' . $weight_unit; ?></td> 
<tr> 
<?php if ($weight) { ?>
<tr>
<?php } ?>

Thats all

Upvotes: 1

You Old Fool
You Old Fool

Reputation: 22959

You can format weight with Opencart's weight class format() method like this:

$this->weight->format($weight,$weight_class_id);

The getProduct() method already supplies both values you need so you can easily call it from the product controller like so:

$this->data['product_weight'] = $this->weight->format($product_info['weight'],$product_info['weight_class_id']);

Then display $product_weight wherever you want in the tpl as you would any other variable. This will give you exactly what you want but with no space (i.e., '250mg').

If you want more control over the formatting, you can also get only the unit abbreviation (e.g., 'kg', 'oz', 'lb', etc) with the getUnit() method like this:

$this->weight->getUnit($product_info['weight_class_id']);

You could then put them together however you want. If you want a space for instance:

$this->data['product_weight'] = $product_info['weight'] . ' ' .  $this->weight->getUnit($product_info['weight_class_id']);

Upvotes: 3

Frosty
Frosty

Reputation: 334

Hello fellow Bulgarian i assume by your name.

If you want to display weight on products it's very simple.

Open your FTP and go to catalog/view/theme/YOURTHEME/template/product and download product.tpl

find this line

<span class="reward"><small><?php echo $text_points; ?> <?php echo $points; ?></small></span><br />
<?php } ?>

Right after it paste this

<?php 
   echo round($weight,2). "kg";
?>

(Or paste it where ever you want to see it on the product page.)

after that go to /catalog/controller/product and open product.php

find this line

 $this->data['model'] = $product_info['model'];

and right after it paste this line:

$this->data['weight'] = $product_info['weight'];

and you are pretty much done with that.

Hopefully i understood your question correctly.

Upvotes: 0

Related Questions