What i'm trying to do is edit the product_list.tpl file within Opencart (admin panel) to show the product attribute in a column.
Here is a before and after screenshot of what Im trying to achieve. (done badly in photoshop)
Now, im kinda ok with PHP. i don't need the exact code for laying out in a table.
Currently to make this list is the code below.
<?php foreach ($products as $product) { ?>
<tr>
<td style="text-align: center;"><?php if ($product['selected']) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $product['product_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $product['product_id']; ?>" />
<?php } ?></td>
<td class="center"><img src="<?php echo $product['image']; ?>" alt="<?php echo $product['name']; ?>" /></td>
<td class="left"><?php echo $product['name']; ?></td>
<td class="left"><?php echo $product['model']; ?></td>
<td class="left"><?php if ($product['special']) { ?>
<span style="text-decoration: line-through;"><?php echo $product['price']; ?></span><br/>
<span style="color: #b00;"><?php echo $product['special']; ?></span>
<?php } else { ?>
<?php echo substr($product['price'], 0, -2); ?> (ex. VAT)
<?php } ?></td>
<td class="right"><?php if ($product['quantity'] <= 0) { ?>
<span style="color: #FF0000;"><?php echo $product['quantity']; ?></span>
<?php } elseif ($product['quantity'] <= 5) { ?>
<span style="color: #FFA500;"><?php echo $product['quantity']; ?></span>
<?php } else { ?>
<span style="color: #008000;"><?php echo $product['quantity']; ?></span>
<?php } ?></td>
<td class="left"><?php echo $product['status']; ?></td>
<td class="right"><?php foreach ($product['action'] as $action) { ?>
<a class="editbutton" href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a>
<?php } ?></td>
</tr>
<?php } ?>
This is the loop that it does foreach product in the list. now within editing a product, you can see the attributes, the handle to get the attributes is
<?php echo $product_attribute['name']; ?>
but if i put this within the Product_list.tpl file, I get php errors, im guessing i have to somehow modify the controller file that serves data to the product_list.tpl and add in a function to get the product attribute, but im unsure on how to do this?
Can anyone help?
Yeah you're right you need to pull the product attributes into the controller (/admin/controller/catalog/product.php) to make them available to the view.
The slight complication is that a product can have multiple attributes so you'll need to pull them into an array and then iterate over the array in the view.
The product model (admin/model/catalog/product.php) has a method getProductAttributes($product_id), if you call this from the controller you should be able to pull in the product attributes.
NB. This controller pulls in all the products in the list so you need to be pulling in the product attributes within the loop that iterates over the list of all the products... in my version this loop starts around line 350 with:
$results = $this->model_catalog_product->getProducts($data);
foreach ($results as $result) {
Inside this loop you should see the code that loads all the data for each product (around line 375):
$this->data['products'][] = array(
'product_id' => $result['product_id'],
'name' => $result['name'],
'model' => $result['model'],
'price' => $result['price'],
'special' => $special,
'image' => $image,
'quantity' => $result['quantity'],
'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
'selected' => isset($this->request->post['selected']) && in_array($result['product_id'], $this->request->post['selected']),
'action' => $action
);
If you add the call to the model to get the attributes into this:
$this->data['products'][] = array(
'product_id' => $result['product_id'],
'name' => $result['name'],
'model' => $result['model'],
'price' => $result['price'],
'special' => $special,
'image' => $image,
'quantity' => $result['quantity'],
'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
'selected' => isset($this->request->post['selected']) && in_array($result['product_id'], $this->request->post['selected']),
'action' => $action,
'attributes' => $this->model_catalog_product->getProductAttributes($result['product_id']);
);
You should then have the array $product['attributes'] available to iterate over in the view. Within the loop that iterates over each product:
<?php
foreach ($products as $product) {
foreach ($product['attributes'] as $attribute) {
echo "$attribute<br>";
}
}
?>
NB. I haven't actaully tested this so copy and pasting might not work but it should get you on the right track.