How to display units for opencart? - opencart

Tell me how to display units for opencart 2.3. When the goods were measured in grams, I simply added the "gr" after the weight was displayed. And now I need to display "liters" as well, as now this crutch has outlived itself. How to address the variable that is responsible for the output of units (gr, kg, l) ??
This is how I used to get weight
<?php if($product['weight']>0) { ?>
<div class="weight">
<?php echo $product['weight']; ?>гр.
</div>
<?php } ?>
In the “popular” module and in the categories and in the product’s card itself, I derive the weight using the code above. But this code does not display the units (grams, kilograms, tons) that I choose in the admin panel when creating the product.
Everything that is highlighted with red colour needs to be displayed automatically from the admin panel.

Based on your example weight php code, I assume you mean how to display the weight with unit in the product page.
The code I am attaching below will help you do that easily.
Open file:
catalog/model/catalog/product.php
Find line:
public function getProduct($product_id) {
Before the line mentioned above add the following code:
public function getProductWeightWithUnit($product_id) {
$product_info = $this->getProduct($product_id);
$query = $this->db->query("SELECT unit FROM `" . DB_PREFIX . "weight_class_description` WHERE
weight_class_id='".$product_info['weight_class_id']."'");
if ($query->num_rows) {
return number_format($product_info['weight'],2) . " " . $query->row['unit'];
} else {
return false;
}
}
Save changes and close the file.
Now, open file:
catalog/controller/product/product.php
Find line:
if ($product_info['minimum']) {
Before the line mentioned above add the following code:
if ($product_info['weight']) {
$data['weight'] = $this->model_catalog_product->getProductWeightWithUnit($this->request->get['product_id']);
} else {
$data['weight'] = false;
}
Now, the backend code is ready. Based on the theme that you use, you need to edit the correct product.tpl file from your theme. For example if you use the default theme, then the file to edit is the following:
catalog/view/theme/default/template/product/product.tpl
Find the line:
<li><?php echo $text_stock; ?> <?php echo $stock; ?></li>
and add the following code after:
<li><?php echo $weight; ?></li>
The above example will show the weight like this:

Related

Opencart Check if current page is product

Does OpenCart Version 2.3.0.2 have some methods to check in header or footer .tpl files that current page is product? Example if (is_product) { // do something }
You can try to check if your session has product. $this->session->data['product_id'] You can check it anywhere. You can write the product to the session... for example:
$this->session->data['product_id'] = /*your product_id*/;
And now you can retrieve this product anywhere.
No, But you can create it.
File:
catalog/controller/common/header.php
Find:
return $this->load->view('common/header', $data);
Add Before:
$data['is_product'] = isset($this->request->get['route']) && $this->request->get['route'] == 'product/product';
File:
catalog/view/theme/default/template/common/header.tpl
Add where you need:
<?php if (!empty($is_product)) { ?>
<p>This is the product page</p>
<?php } else { ?>
<p>This is not the product page</p>
<?php } ?>

Move "Submitted By" from node.tpl to page.tpl

I'm looking to move "Submitted By" info from node.tpl to page.tpl however when I add the following from node.tpl I get errors. Im assuming I dont have access to those variables, but would like to know how I can set up a pre-proccess to get it to display as it does in the node.tpl
<?php if ($display_submitted): ?>
<div class="submitted">
<?php print $submitted; ?>
</div>
<?php endif; ?>
You can either use a preprocess function in the template.php of your theme, as explained here:
https://drupal.stackexchange.com/questions/40222/how-can-i-print-node-authors-last-login-date-on-page-tpl-php
In your case it would look like this (tested on Drupal 7):
function yourtheme_preprocess_page(&$variables) {
$variables['author'] = "";
if (isset($variables['node']) && ($account = user_load($variables['node']->uid))) {
$variables['author'] = $account->name;
}
}
And then in your page.tpl.php use this:
Submitted by: <?php print $author; ?>
If you don’t want to touch any of your theme’s files, but you need to output the author’s name in another region as the node content, you could create a view (block display) containing the node author, and assign it to the region.
While normally done in node.tpl.php, if the page is a node view page, the $node variable is also available in page.tpl.php
You can then use something like:
if (isset($node)) {
// Check if display submitted variable is set for this node type
if (variable_get('node_submitted_'. $node->type, 0)) {
// Do stuff
}
}
An alternative approach would be adding the required logic instead to an implementation of
hook_preprocess_page
Bonus update: You can see the $node variable added to page.tpl.php in core template_preprocess_page
if ($node = menu_get_object()) {
$variables['node'] = $node;
}
In page.tpl.php:
<div class="submitted">
<?php echo format_date($node->created, 'custom','d.m.Y'); ?><br />
<?php echo 'by ' . $node->name; ?>
</div>

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.
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']);
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
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.

how can we create a position for the 2.0.1.1 opencart version

Opencart has a four layout left ,right Top and a Bottom.Adding new custom positions manually is not impossible of course, but ideally that needs to be done at the core.
Something like "header-bottom" or "footer-top" would be very useful for many modules, like "Menus" and "Slideshows".
how can we create a position like as above for the 2.0.1.1 opencart version
please help me thanks
I m making
footer_top POSITION similarly you can make any position
Add code in
opencart2\catalog\controller\common\home.php line 15(you can
understand where to put this line or ask it)
$data['footer_top'] = $this->load->controller('common/footer_top');
And Add this code in opencart2\catalog\view\theme\default\template\common\home.tpl
on line 13
<div id="content" class="col-sm-12"><?php echo $footer_top; ?></div>
copy -> opencart2\catalog\controller\common\content_top.php
to ----> opencart2\catalog\controller\common\footer_top.php
And change line 44
from
$modules = $this->model_design_layout->getLayoutModules($layout_id, 'content_top');
to
$modules = $this->model_design_layout->getLayoutModules($layout_id, 'footer_top');
Open opencart2\admin\view\template\design\layout_form.tpl and add this
code In select position dropdown options
<?php if ($layout_module['position'] == 'footer_top') { ?>
<option value="footer_top" selected="selected">footer top</option>
<?php } else { ?>
<option value="footer_top">footer top</option>
<?php } ?>
And Add below code after
html += ' < option value="content_top"><?php echo $text_content_top; ?>< /option>';
line in addmodule() function
html += ' < option value="footer_top">footer top< /option>';
And check in Position
opencart2/admin/index.php?route=design/layout/edit&token=7ddf86dsfdsfsdfdsfsdfdsfsdf5df&layout_id=1

Change page data based on currency choice

I've been trying to create simple script that swaps table data based on the active currency code. It's for a size chart (on product.tpl).
PREMISE:
IF "USD" is active then display table "A" (Inches), ELSE display table "B" (Centimeters).
I'm a total hack with php/js, but enough to find my way into trouble. :)
I followed this thread to bring the currency code variable into "product.tpl": How to get currency to show on product page in opencart?
Now I'm just banging my head, I can't find the right variable to create the swap... I've tried multiple variations of a simple If/Else script, basically like this:
<?php if ($currency['code'] == $currency_code) { ?>
<td>15.5"</td>
<td>25"</td>
<?php } else { ?>
<td >40cm</td>
<td >64cm</td>
<?php } ?>
Anyone have a clue what the script might look like?
Or am I totally barking at the wrong cat here?? Thx
Just use the session value for the currency code...
<?php if($this->session->data['currency'] == 'USD') : ?>
<td>15.5"</td>
<td>25"</td>
<?php else: ?>
<td>40cm</td>
<td>60cm</td>
<?php endif; ?>