James Collins
James Collins

Reputation: 45

Adding product attributes to opencart invoice

I am trying to show product attributes on the Print Invoice page in Opencart version 2.0.1.1.

So far I have edited admin/controller/sale/order.php by adding $this->load->model("catalog/product");

Then I have added 'attribute' => $this->model_catalog_product->getProductAttributes($product['product_id']) to the $product_data[] = array()

Now on the order_invoice.tpl page I have used <?php print_r ($attribute);?> to see what data it is pulling and it shows:

Array
(
[attribute_id] => 1
[product_attribute_description] => Array
    (
        [1] => Array
            (
                [text] => 240
            )

    )

)

The [text] => 240 is the value of the attribute but I cannot for the life of me get the attribute name to show up, I have tried numerous attempts and as most commonly suggested I've tried $attribute['name']; but this brings up nothing.

Can someone please explain how I can properly retrieve the attribute name and attribute value.

Upvotes: 2

Views: 342

Answers (1)

Abdelrhman Adel
Abdelrhman Adel

Reputation: 1187

I've tried $attribute['name']; but this brings up nothing

So why do you think it should bring up something? obviously, there is no such an entry called name in $attribute (you can notice that in the dump you got through print_r)

Accordingly you need to change $this->model_catalog_product->getProductAttributes in such a way that it returns the name of the attribute, so in order to use $attribute['name'] correctly, your $attribute dump should be like:

Array
(
[attribute_id] => 1
[name] => 'bla bla bla'
[product_attribute_description] => Array
    (
        [1] => Array
            (
                [text] => 240
            )

    )

)

So here is what we are going to do (sorry for the long intro):

  1. open the file <OC_ROOT>/admin/model/catalog/product.php
  2. go to the function getProductAttributes($product_id) @ class ModelCatalogProduct
  3. Before this line $product_attribute_data[] = array(, we should add a query to get the attribute name:
// attribute name is stored in the table `attribute_description`
$attr_name_query = $this->db->query("SELECT `name` FROM `" . DB_PREFIX . "attribute_description` WHERE `attribute_id` = " . (int)$product_attribute['attribute_id'] . " and `language_id` = " . (int)$this->config->get('config_language_id'));         
$attr_name = $attr_name_query->row['name'];
  1. Finally, add $attr_name to the resultant attributes array, after the line 'attribute_id' => $product_attribute['attribute_id'], add 'name' => $attr_name

Upvotes: 1

Related Questions