Post by Qphoria » Thu Mar 11, 2010 6:32 am

The first 3 steps shown here are already included in the core as of 1.4.8 or later. So all you need to do is steps 4 and 5 for each field you want to show

The product admin area has a lot of fields that aren't displayed on the catalog side. Things like weight, sku, dimensions, location, etc.

This is in part due to too much micro-managing the data. The model function pulls all the data from the database for the product. But the controller then filters that and sets individual variables for each, instead of just formatting certain values and passing the rest through.

A better method, and one that is easily modded into the existing code, is to simply pass that initial array of data directly to the view and allow easy display of any field that exists in the database.

For example, if you want to display the "SKU" on the product page:

1. EDIT: catalog/controller/product/product.php

2. FIND:

Code: Select all

$product_info = $this->model_catalog_product->getProduct($product_id);
3. AFTER, ADD:

Code: Select all

$this->data['product_info'] = $product_info;
From now on, you can access any product variable for that product simply by referencing the column name in the view. For the "SKU", we will add:

4. EDIT: catalog/view/theme/YOURTHEME/template/product/product.tpl

5. FIND:

Code: Select all

<tr>
  <td><b><?php echo $text_model; ?></b></td>
  <td><?php echo $model; ?></td>
</tr>
6. AFTER, ADD:

Code: Select all

<tr>
  <td><b>SKU:</b></td>
  <td><?php echo $product_info['sku']; ?></td>
</tr>
Now the sku will appear under the model on the product page.
Do the same for all other fields you want to show. You only have to change the first 3 steps once to display all the fields you want.

You can also call conversion functions as well for things like weight

Code: Select all

<tr>
  <td><b>Weight:</b></td>
  <td><?php echo $this->weight->format(product_info['weight'], 4); ?></td>
</tr>
Last edited by Qphoria on Mon Apr 11, 2011 9:54 am, edited 4 times in total.
Reason: Topic moved

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by store » Tue Mar 30, 2010 4:49 am

I can usually get this once someone explains to me in simple terms, so could someone explain to me the above please?

New member

Posts

Joined
Sun Mar 28, 2010 10:16 pm

Post by The Alchemist » Tue Mar 30, 2010 12:43 pm

So if you wanted to display the weight you would have to change this

Code: Select all

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

Code: Select all

$this->data['product_weight'] = $product_weight;
Than use the weight display in the tpl?

Sound right?

User avatar
Active Member

Posts

Joined
Sun Nov 22, 2009 11:04 am

Post by store » Tue Mar 30, 2010 5:43 pm

The Alchemist wrote:Than use the weight display in the tpl?

Sound right?
That made it sound much easier.

But what do you mean by: then use the weight display in the tpl? Is the tpl the file? Or something else?

Also another question: what if I wanted to add a field e.g. Publisher:, and remove the model field from being cumpolsary?

New member

Posts

Joined
Sun Mar 28, 2010 10:16 pm

Post by The Alchemist » Wed Mar 31, 2010 9:37 am

I really cant tell you, you need Q to come in here and set things straight.

You can add additional fields and delete others but you need to edit the cart properly.

You can also hack other mods to get what you want.

TPL files are usually the visual part of the cart. They allow you to see whats on the screen. You can manipulate them to get what you want.

There are 8 different parts of this cart that you have to usually edit to create a new mod.

Simple put there is the View, this is what people see. Language is what things read on the view screen. You can manipulate this to change certain words to other parts. Controller, this is what what controls the action part of what ever you decide to process. And than there is the model. Cant tell you what that is, Im still understanding the way this system functions myself.

User avatar
Active Member

Posts

Joined
Sun Nov 22, 2009 11:04 am

Post by readyman » Wed Apr 07, 2010 3:11 pm

The Alchemist wrote:So if you wanted to display the weight you would have to change this

Code: Select all

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

Code: Select all

$this->data['product_weight'] = $product_weight;
Than use the weight display in the tpl?

Sound right?
This is not the case at all. You would then need to give $product_weight itself a value.

Look again at what the code is doing.
1. $product_info = $this->model_catalog_product->getProduct($product_id);
We are calling a function in the model and putting the value into $product_info.
It will usually be an array of values, not just one value.

2. To output anything into the views, you must do it in this format. $this->data['product_info']. In the view file, this is the same as $product_info, but it's still an array holding lots of values inside it.

3. To get stuff out of the variable, in the view, you have to get the value by a reference key. You can only get out what's already in there, so in this case you can get the $product_info['sku'] or $product_info['name'], $product_info['status'] etc...

http://www.alreadymade.com
Follow me on twitter.com/alreadymade


User avatar
Global Moderator

Posts

Joined
Wed May 20, 2009 5:16 am
Location - Sydney

Post by Qphoria » Wed Apr 07, 2010 7:56 pm

The Alchemist wrote:So if you wanted to display the weight you would have to change this

Code: Select all

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

Code: Select all

$this->data['product_weight'] = $product_weight;
Than use the weight display in the tpl?

Sound right?
You have a habit of quoting me as your own but often getting it wrong.

I added this

Code: Select all

$this->data['product_info'] = $product_info;
So that you will never have to edit the controller.
Then in your tpl you reference the array:
$product_info['weight']
$product_info['sku']
$product_info['location']
$product_info['length']
etc

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by The Alchemist » Sun Apr 11, 2010 9:18 pm

You have to read better, I was more or less asking you if that code sounds right.

Notice where I wrote "Sound Right?"

User avatar
Active Member

Posts

Joined
Sun Nov 22, 2009 11:04 am

Post by readyman » Tue Apr 13, 2010 11:52 pm

The way you put it actually sounded wrong. Because it looks like you simply changed product_info to product_weight. In your code, $product_weight doesn't have a value.

http://www.alreadymade.com
Follow me on twitter.com/alreadymade


User avatar
Global Moderator

Posts

Joined
Wed May 20, 2009 5:16 am
Location - Sydney

Post by Qphoria » Tue Apr 13, 2010 11:55 pm

btw this one line was added to the product controller in the core in 1.4.7

Code: Select all

$this->data['product_info'] = $product_info;
so from now on, in the product.tpl, you just need to echo as shown above:

Code: Select all

<?php echo $product_info['xxx']; ?>
Where "xxx" is the database field name.
e.g.:
$product_info['weight']
$product_info['sku']
$product_info['location']
$product_info['length']
etc

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by timkirtley » Fri Jun 04, 2010 7:09 am

Hi, this looks interesting.

How do you create extra fields in the back end which can then be used on the product pages?

New member

Posts

Joined
Fri Jun 04, 2010 7:01 am

Post by cashfile » Tue Jun 08, 2010 9:55 pm

Many thanks from russian users OpenCart !!! 8)

Newbie

Posts

Joined
Fri Jan 22, 2010 6:39 am

Post by musicofthehart » Fri Jul 23, 2010 1:00 am

Sorry to bump an old thread, but this was VERY helpful to me, and I had one other question.

I am changing things up and using the SKU field for my own purpose, to display a link in the product info. So as I have changed it, I can type in www.Qphoriaisawsome.com in sku field in the admin/product page, and it will display: www.Qphoriaisawesome.com exactly, not a clickable link. Is there a way to make it display an actual clickable link? Even better would be a way to make it where when I type in www.Qphoriaisawesome.com in admin, it displays a clickable link on the product page like "Q is awesome" that when clicked will take you to www.Qphoriaisawesome.com

Sort of like how manufacturer link works except that it takes you to an outside page.

New member

Posts

Joined
Fri Jul 16, 2010 3:42 pm

Post by totalitarian » Tue Sep 07, 2010 5:26 am

Hi,

I'm trying to do something similar for my invoice template (order_invoice.tpl). The code I want to edit is...

Code: Select all

<?php foreach ($order['product'] as $product) { ?>
    <tr>
      <td><?php echo $product['name']; ?>
        <?php foreach ($product['option'] as $option) { ?>
        <br />
        &nbsp;<small> - <?php echo $option['name']; ?> <?php echo $option['value']; ?></small>
        <?php } ?></td>
		
      <td><?php echo $product['model']; ?></td>
	  <td><?php echo 'test'; ?></td>
      <td align="right"><?php echo $product['quantity']; ?></td>
      <td align="right"><?php echo $product['price']; ?></td>
      <td align="right"><?php echo $product['total']; ?></td>
    </tr>
    <?php } ?>
 
I'm not quite sure how to the products 'location' field as in the code above, $product is actually part of the $order array. So somehow I need to be able to do a lookup against the database product to find the location field

Any advice would be most welcome :)

Newbie

Posts

Joined
Tue Sep 07, 2010 3:11 am

Post by danlanphear » Wed Sep 08, 2010 5:04 am

Qphoria wrote: A better method, and one that is easily modded into the existing code, is to simply pass that initial array of data directly to the view and allow easy display of any field that exists in the database.

For example, if you want to display the "SKU" on the product page:

1. EDIT: catalog/controller/product/product.php

2. FIND:

Code: Select all

$product_info = $this->model_catalog_product->getProduct($product_id);
3. AFTER, ADD:

Code: Select all

$this->data['product_info'] = $product_info;
From now on, you can access any product variable for that product simply by referencing the column name in the view. For the "SKU", we will add:

4. EDIT: catalog/view/theme/YOURTHEME/template/product/product.tpl

5. FIND:

Code: Select all

<tr>
  <td><b><?php echo $text_model; ?></b></td>
  <td><?php echo $model; ?></td>
</tr>
6. AFTER, ADD:

Code: Select all

<tr>
  <td><b>SKU:</b></td>
  <td><?php echo $product_info['sku']; ?></td>
</tr>
Now the sku will appear under the model on the product page.
Do the same for all other fields you want to show. You only have to change the first 3 steps once to display all the fields you want.
Question, Qphoria: Does the statement
Qphoria wrote:allow easy display of any field that exists in the database.
... suggest that I could implement your modification suggested here and then add my own fields to the product database for my own specific criteria, and that I would then be able to display those fields on the product pages using this technique?

For example, I am classifying auto parts, which have cross reference numbers, part numbers, model numbers, vehicle applications such as by year, 4x4, Truck/SUV, etc. Can I do that by creating my own fields and using this technique?

Also, what about searches? How can I make the cart search these fields?

Thanks,

-Dan

New member

Posts

Joined
Wed Jun 30, 2010 7:36 am

Post by Qphoria » Wed Sep 08, 2010 5:11 am

danlanphear wrote: Question, Qphoria: Does the statement
Qphoria wrote:allow easy display of any field that exists in the database.
... suggest that I could implement your modification suggested here and then add my own fields to the product database for my own specific criteria, and that I would then be able to display those fields on the product pages using this technique?

For example, I am classifying auto parts, which have cross reference numbers, part numbers, model numbers, vehicle applications such as by year, 4x4, Truck/SUV, etc. Can I do that by creating my own fields and using this technique?

Also, what about searches? How can I make the cart search these fields?

Thanks,

-Dan
Yes, you can add a field to the product table and it will pull it back and make it available to the view. As of 1.4.8, this is in the core by default.
1. Add a column to database: new_field_name
2. Edit the catalog/view/theme/YOURTHEME/template/product/product.tpl
3. Add:
<?php echo $product_info['new_field_name']; ?>

Where ever you want to show the new field

It's a bit more complicated to make them searchable

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by danlanphear » Wed Sep 08, 2010 5:25 am

Awesome Qphoria. Thanks for the quick reply.

I am going to send you a PM with some other questions if you don't mind...

New member

Posts

Joined
Wed Jun 30, 2010 7:36 am

Post by danlanphear » Sat Sep 11, 2010 1:08 am

Qphoria,

I added <?php echo $product_info['dimensions']; ?> to product.tpl.
I added a field to the product table in the database.
I entered a test value, and the value shows up in the product page. This is great.
I tried to follow the pattern of the rest of the template to get the field name to appear next to the field value by entering <?php echo $product_info['text_dimensions']; ?>, but this gave me an "undefined variable" error.

I realize I'm a noob here, but can you please clue me in on how to get it to show the field name dynamically as well.

Thanks -Dan

EDIT1: I'm looking at product.php and seeing the parts that list the text_ variables. I may be able to stumble through this.

EDIT2:
I added

Code: Select all

$this->data['text_manufacturer'] = $this->language->get('text_manufacturer');
to catalog/controller/product/product.php

Then I added

Code: Select all

$_['text_dimensions']     = 'Dimensions:';
to catalog/language/english/product/product.php.

Then, I also added

Code: Select all

<?php echo $text_dimensions; ?>
to product.tpl

Before, I got the dimensions value, but now I get a blank screen with the following error message:

Code: Select all

Parse error: syntax error, unexpected '[' in /home2/nepafouh/public_html/beta/catalog/controller/product/product.php on line 135,
I can't figure this one out.
Help?

EDIT 3:
Looks like maybe my SQL server puked for few minutes. I rebooted and now it comes up ok.

New member

Posts

Joined
Wed Jun 30, 2010 7:36 am

Post by twuncher » Wed Oct 06, 2010 6:15 am

will this ever be included into the main code, so we can add additional fields?

Newbie

Posts

Joined
Sat May 15, 2010 7:42 pm

Post by opencartnovice » Thu Oct 07, 2010 5:28 am

Hi all,
I'm a novice of either OC or PHP then need some helps.

I got some items like milk weighted by ml rather than kg or pound. I add one more unit say milliliter (ml) scaling 1000, i.e. 1000ml=1kg (assumed). The items now have to show correct units, for instance
"Weight: 500 ml" rather than "Weight: 500"

I did like the following instruction but it doesn't work. Unfortunately I cleared log file show forgot what message is (normally the error does not appear exactly where the message indicated).

Code: Select all

<tr>
  <td><b>Weight:</b></td>
  <td><?php echo $this->weight->format(product_info['weight'], 4); ?></td>
</tr>
[/quote]

I searched full codes and found a below function in weight.php (system folder)
Looking into product table, we have one field named "weight_class_id". I think this is unit field

Code: Select all

    
public function format($value, $unit, $decimal_point = '.', $thousand_point = ',') {
        if (isset($this->weights[strtolower($unit)])) {
            return number_format($value, 2, $decimal_point, $thousand_point) . $this->weights[strtolower($unit)]['unit'];
        } else {
            return number_format($value, 2, $decimal_point, $thousand_point);
        }
    } 
I change the above instruction like this

Code: Select all

<td><?php echo $this->weight->format(product_info['weight'], product_info['weight_class_id']); ?></td>
It didn't work either.

Can somebody help please?

Many thanks.
a novice of OC

New member

Posts

Joined
Thu Oct 07, 2010 5:12 am
Who is online

Users browsing this forum: No registered users and 4 guests