2
votes

Good day. Please help me to display units of measurement for goods. OcStore 2.3 engine. By default, units of measurement are not displayed next to the product, they helped me to display them on the product page in this way:

In

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>

Fine! Everything works on the product page. But you need it to work the same in modules (for example Featured Products).

I do all the same steps for the module

Backend:

catalog/controller/extension/module/featured.php

and front end:

catalog/view/theme/default/template/extension/module/featured.tpl

But I get the error:

Notice: Undefined index: product_id in C:\OSPanel\domains\mywebsite.com\catalog\controller\extension\module\featured.php on line 43

That's what it says:

    if ($product_info['weight']) {
LINE 43     $data['weight'] = $this->model_catalog_product->getProductWeightWithUnit($this->request->get['product_id']);
    } else {
        $data['weight'] = false;
    }

Why not seen product_id in ??

2

2 Answers

2
votes

Open catalog/controller/extension/module/featured.php.

Find

if ($this->config->get('config_tax')) {

Add before

if ($product_info['weight']) {
    $weight = $this->model_catalog_product->getProductWeightWithUnit($product_info['product_id']);
} else {
    $weight = false;
}

Basically, here I've replaced $this->request->get['product_id'] with $product_info['product_id'], like @K.B. said in his answer, but made more accurate example.

Then, in the same file find

'tax'         => $tax,

Add after

'weight'      => $weight,

Now go to catalog/view/theme/default/template/extension/module/featured.tpl

Find

<?php if ($product['tax']) { ?>
<span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
<?php } ?>

Add after

<?php if ($product['weight']) { ?>
<span><?php echo $product['weight']; ?></span>
<?php } ?>
2
votes

You must use $product_info['product_id'] or just $product_id instead $this->request->get['product_id'] Because you can't get product_id's for several products using this expression, which are in this loop foreach ($products as $product_id) {