Post by ahmadka » Tue May 28, 2013 4:01 am

Hi guys ..

I've seen that in OpenCart's PHP code, different aspects of a product are accessed like this:

$product['href']
$product['thumb']
$product['name']
$product['description']
$product['reviews']
$product['price']

etc ..

Suppose that for all the products in my Store, I have an Attribute Group named 'AttGrp', and this Attribute Group contains only 1 attribute named 'Att'. Thus, ALL products in my Store will have a value for the attribute 'Att'. So suppose I want to directly access the value of 'Att' for a given product (e.g. show it as a tooltip, etc ..), how would I do that ?

New member

Posts

Joined
Fri May 03, 2013 12:45 am

Post by ahmadka » Tue May 28, 2013 5:36 pm

Anyone has any idea on this ?

New member

Posts

Joined
Fri May 03, 2013 12:45 am

Post by midgette » Sun Jun 02, 2013 12:12 pm

In the admin product page, your attributes can be found in array $product_attributes.
And in the catalog product page, they can be found in array $attribute_groups.

So, If you just want to see it on your front end product page, I would suggest adding this in catalog/view/theme/default/template/product/product.tpl:

Code: Select all

<?php
if ($attribute_groups) {
   foreach ($attribute_groups as $attribute_group) {
      if ($attribute_group['name'] != "AttGrp") {
         continue;
      }
      foreach ($attribute_group['attribute'] as $attribute) {
         if ($attribute['name'] != "Att") {
            continue;
         }
         $AttValue = $attribute['text'];
      }
   }
}
?>
This would save the value of Att in variable $AttValue, for you to use anywhere in product.tpl

Code: Select all

<?php
if (isset($AttValue)) {
   echo "Att: " . $AttValue;
}
?>
Hope this helps.

Regards,
Midgette

Newbie

Posts

Joined
Sun Apr 28, 2013 4:16 pm

Post by jgsw » Sun Sep 29, 2013 8:35 pm

Hi Midgette,

I've just used this and it works great. Thanks!

jgsw

Quick Edit Admin Panel (40% off!) - CSS Unique (only $10!)
Sticky Admin Menu (Free) - Add Scripts & Styles to head (Free)


Active Member

Posts

Joined
Sat Mar 16, 2013 3:13 am

Post by comprido » Fri May 30, 2014 8:09 am

Hi! First of all thanks for your help. I´m trying to retrieve a value to show the author pic (Fotografía) so I made:

Code: Select all

 <?php if ($attribute_groups) { ?>
  <div id="tab-bio" class="tab-content" style="display:block">
    <table class="attribute">
      <?php foreach ($attribute_groups as $attribute_group) { ?>
      <?php if ($attribute_group['name'] == 'Sobre el Autor') { ?>
      <thead>
        <tr>
          <td colspan="2"><?php echo $attribute_group['name']; ?></td>
        </tr>
      </thead>
      <tbody>
      <tr>
        <?php foreach ($attribute_group['attribute'] as $attribute) { ?>
        <!-- <tr> 
          <td><?php echo $attribute['name']; ?></td> -->
            <td><?php if ($attribute['name'] != "Fotografía") {                         
            echo nl2br($attribute['text']); } ?><?php if ($attribute['name'] != "Biografía") {
            echo htmlspecialchars_decode($attribute['text']);  } ?></td>                         
       <!-- </tr> -->
        <?php } ?>
        </tr>
      </tbody>
      <?php } ?>
      <?php } ?>
    </table>
  </div>
  <?php } ?>
But when I put the <img src=""> code to show, something fail.
May you please give a hand?

Thanks

Pedro Martin - websitesbuilder.com.au
Checkout my free extensions: https://www.opencart.com/index.php?rout ... r=comprido


User avatar
New member

Posts

Joined
Mon Sep 10, 2012 11:46 pm

Post by comprido » Fri May 30, 2014 9:19 am

Solved.

Code: Select all

<?php if ($attribute_groups) { ?>
  <div id="tab-bio" class="tab-content" style="display:block">
    <table class="attribute">
      <?php foreach ($attribute_groups as $attribute_group) { ?>
      <?php if ($attribute_group['name'] == 'About the Author') { ?>
      <thead>
       <?php foreach ($attribute_group['attribute'] as $attribute) {
    if($attribute['name']=="Photo") { 
    ?>
     <tr>
          <td colspan="2"><?php echo $attribute_group['name']; ?></td>
        </tr>
       <tr>
      <td><?php echo "<img src='image/data/autores/".$attribute['text'].".jpg' style='max-width:200px;max-height:200px'; ";  ?></td>
    <?php } } ?>
     <?php foreach ($attribute_group['attribute'] as $attribute) {
    if($attribute['name']=="Biography") { 
    ?>
      <td><?php echo htmlspecialchars_decode($attribute['text']); ?></td>
    
    <?php } } ?> 
    </tr>      
      </tbody>
      <?php } ?>
      <?php } ?>
    </table>
  </div>
  <?php } ?>

Attachments

custom_attributes.png

Retrieve custom atributtes for opencart products. Show author photo in opencart tabs. - custom_attributes.png (204.35 KiB) Viewed 3728 times


Pedro Martin - websitesbuilder.com.au
Checkout my free extensions: https://www.opencart.com/index.php?rout ... r=comprido


User avatar
New member

Posts

Joined
Mon Sep 10, 2012 11:46 pm

Post by webs_bd » Tue Feb 20, 2018 1:24 am

Would there be a way to add a single attribute into a category page rather than a product page?

Big Decision
Website Production, Ecommerce & Digital Marketing in the UK
http://www.bigdecision.co.uk


New member

Posts

Joined
Sat Nov 09, 2013 8:29 pm
Who is online

Users browsing this forum: No registered users and 17 guests