Post by Johnathan » Wed Nov 24, 2010 10:38 pm

I couldn't find a way to figure out the unit from the weight_class_id without making a database call, so I worked up this:

Code: Select all

<?php $table = $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') . "'"); ?><?php echo $this->weight->format($product_info['weight'], $table->row['unit']); ?>
You can add this to your product.tpl file where you want the weight to display. It breaks the MVC format by putting a database call in the View component, but at least it's just a single edit. I tested it and it seems to work fine -- however, use it at your own risk.

Image Image Image Image Image


User avatar
Administrator

Posts

Joined
Fri Dec 18, 2009 3:08 am


Post by opencartnovice » Sat Dec 04, 2010 8:33 am

I get a lot of helps from this forum. Thanks for every litle helps. In return we can show weight unit with this link

http://translate.google.co.uk/translate ... 81&act=url

In the above link, they use field title but I prefer field unit for short name.

rgrds,
Novice of OC

New member

Posts

Joined
Thu Oct 07, 2010 5:12 am

Post by Johnathan » Sat Dec 04, 2010 10:11 pm

From my understanding of the translated page, that basically works the same way except it splits the edit up into 3 or 4 files. That's fine, and follows the MVC structure, but I always prefer a single edit to a template file, even if it breaks MVC. It's faster, and won't be overwritten by upgrades.

Image Image Image Image Image


User avatar
Administrator

Posts

Joined
Fri Dec 18, 2009 3:08 am


Post by opencartnovice » Sun Dec 05, 2010 3:52 am

I agree that your solultion is one of shortcut, sometime I also stuggled with the MVC framework, but if we don't follow MVC then we probaly end up a new generation of OC :)

New member

Posts

Joined
Thu Oct 07, 2010 5:12 am

Post by Qphoria » Sun Dec 05, 2010 4:58 am

I will agree that the weight library class should needs a "getUnit()" method to be simpler.
For now you should use $this->weight->format() to get the unit added.

Example:

Code: Select all

$this->data['weight'] = $this->weight->format($weight, $this->config->get('config_weight_class'));
That will return "xx lbs" or whatever unit the weight class id is

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by Johnathan » Sun Dec 05, 2010 8:28 am

Qphoria wrote:I will agree that the weight library class should needs a "getUnit()" method to be simpler.
Yeah, I was actually surprised it didn't already have a getUnit() method.

Image Image Image Image Image


User avatar
Administrator

Posts

Joined
Fri Dec 18, 2009 3:08 am


Post by marc_cole » Thu Jan 20, 2011 4:18 am

Johnathan wrote:You can do this, but since he's looking at putting in a product's (and not the cart's) weight, it will give the wrong unit if the product doesn't use the same weight unit as the store.
I didn't think you could mix weight units in a store. How would the code know to add disparate weights in a cart if you've use pounds, ounces, and grams? Is there conversion code built-in?

OpenCart v1.4.9.4
VQMod | Categories Home | Cleaner By Default - 2 Column | Speak Good English


Active Member

Posts

Joined
Tue Dec 14, 2010 11:26 am
Location - Seattle, WA

Post by picobo » Thu Mar 08, 2012 8:45 am

Just as a heads up for anyone reading this in 2012 with OpenCart v1.5.3.1...

Jonathan's solution works, but I had to modify it a bit. The detailed process to display the weight along with the weight unit in the product display is as follows:

In catalog/controller/product/product.php

Find (around line 156):

Code: Select all

$this->data['text_tags'] = $this->language->get('text_tags');
Add right after:

Code: Select all

$this->data['text_weight'] = $this->language->get('text_weight');
Also find (around line 184):

Code: Select all

$this->data['points'] = $product_info['points'];
Add right after:

Code: Select all

$this->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') . "'");
$this->data['weightunit'] = $tablewunit->row['unit']; 

In catalog/language/english/product/product.php (and any other language you might have):
Add the following somewhere in the 'Text' area (between lines 3 and 27):

Code: Select all

$_['text_weight']       = 'Weight'; 
In catalog/view/theme/YOUR_THEME/template/product/product.tpl:
Find:

Code: Select all

            <tr>
              <td><b><?php echo $text_model; ?></b></td>
              <td><?php echo $model; ?></td>
            </tr>
Add right after:

Code: Select all

            <tr>
              <td><b><?php echo $text_weight; ?></b></td>
              <td><?php echo $weight . ' ' . $weightunit; ?></td>
            </tr>
Note: Some themes like 'Shoppica' will have those lines differently.
In that case, add them like this:

Code: Select all

          <dt><?php echo $text_weight; ?></dt>
          <dd><?php echo $weight . ' ' . $weightunit; ?></dd>
Hope somebody finds this helpfull, as it took me quite some time to go through all the threads and find which info would work for my version.
If anybody knows a better way, please explain in detail as it might help other people.
Peace out!

Newbie

Posts

Joined
Sat Feb 11, 2012 6:59 am

Post by sr.walid » Sun Jul 01, 2012 11:05 pm

Thanks picobo!

It works and is really useful.

Great work, keep it up!

Newbie

Posts

Joined
Sun Jul 01, 2012 11:01 pm

Post by SupraTwinTurbo » Thu Feb 21, 2013 4:08 pm

Cool, I been looking for this. Will this work for 1.5.1.3? Anyone try?

User avatar
New member

Posts

Joined
Thu Mar 24, 2011 12:43 pm
Location - NYC, NY USA

Post by Chris_UK » Sat Oct 07, 2017 11:27 am

I know this is an old post, but im using 2.1 and this is how ive done it.

There may be bugs here because its stripped out a much more complex module i am building. I'm adding product states (liquid, solid) into the system - (I will be uploading that to the market place at some point, but this is free small piece is free to use as is. I might add this too later).

Firstly i copied an admin localisation weight class method into a new front end localisation display weight model.

catalog/model/localisation/display_weight.php

Code: Select all

class ModelLocalisationDisplayWeight extends Model {
  
  public function getWeightClass($weight_class_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "weight_class wc LEFT JOIN " . DB_PREFIX . "weight_class_description wcd ON (wc.weight_class_id = wcd.weight_class_id) WHERE wc.weight_class_id = '" . (int)$weight_class_id . "' AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

		return $query->row;
	}
}
Next I made a vqmod xml file.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<modification>
    <id>Display Weight</id>
    <vqmver>2.6</vqmver>
    <version>1.0</version>
    <author>Chris Serella</author>
    
    <file path="catalog/language/*/product/" name="product.php">
        <operation error="log">
            <search><![CDATA[ignores this search but the element is required]]></search>
            <add position="bottom"><![CDATA[$_['text_weight'] = "Weight:";]]></add>
        </operation>
    </file>
    
    <file path="catalog/controller/product/" name="product.php">
        <operation error="log">
            <search><![CDATA[$data['text_loading'] = $this->language->get('text_loading');]]></search>
            <add position="after"><![CDATA[$data['text_weight'] = $this->language->get('text_weight');]]></add>
        </operation>
      
        <operation error="log">
            <search><![CDATA[$data['review_status'] = $this->config->get('config_review_status');]]></search>
            <add position="before"><![CDATA[      if ($product_info['weight']) {
        $data['product_weight'] = round($product_info['weight'], 2);
        $this->load->model('localisation/display_weight');
        $data['weight_sym'] = $this->model_localisation_display_weight->getWeightClass($product_info['weight_class_id']);
      }]]></add>
        </operation>
    </file>
    <file path="catalog/model/catalog/" name="product.php">
        <operation error="log">
            <search><![CDATA['viewed'           => $query->row['viewed']]]></search>
            <add position="iafter"><![CDATA[,
        'product_weight'          => $query->row['weight'],
        'product_weight_class_id' => $query->row['weight_class_id'],
            ]]></add>
        </operation>
    </file>
    
    <file path="catalog/view/theme/default/template/product/" name="product.tpl">
        <operation error="log">
            <search><![CDATA[<li><?php echo $text_model; ?> <?php echo $model; ?></li>]]></search>
            <add position="after"><![CDATA[<li><?php echo $text_weight; ?> <?php echo $product_weight . $weight_sym['unit']; ?></li> ]]></add>
        </operation>
    </file>
</modification>
I hope this helps somebody out, I have some experience coding, but don't do much these days, I posted this back here because thread pointed me in the right direction. You will notice its compatible with your admin localisation of weight classes so they will be picked up in the system language automatically.
Make your own adjustment to $text_weight in the first file block, so long as the file exists it will automatically add that line in all languages. you can add separate blocks if you need full translations.

Oh one final note you will need VQMOD, or you can convert the file to be ocmod compatible and create the archive for upload with all the needed stuff which ever suits you.

New member

Posts

Joined
Wed Jan 20, 2016 4:39 am
Who is online

Users browsing this forum: No registered users and 326 guests