Post by POS » Wed May 30, 2012 6:59 pm

It's for latest OpenCart 1.5.2

I need to be able to get a certain product'a value for attribute_id=12 - and show it in my code. Is this easy?

You see, I need a number I've input into the product's attribute section - to calculate a new number that is important for the client. The number is used in a javascript calculation.

From before (in OC 1.4.9.1) I have used a module called "Extra fields", and got the needed number from this code:

Code: Select all

$first_field = reset($extra_fields);
$first_field_val = $first_field['value'];
But if there is a easy way to get a product's value for a specific attribute, that will make my day very good!

something like this maybe?

Code: Select all

[code]
$myvalue = $attributevalue(12);

example-attribute-calculation.png

example-attribute-calculation.png (93.86 KiB) Viewed 1863 times


POS
Newbie

Posts

Joined
Tue Mar 22, 2011 11:34 pm

Post by POS » Wed May 30, 2012 8:27 pm

I found a solution, but am not sure if this is the best...

Code: Select all

<?php if ($attribute_groups) { ?>
    <?php foreach ($attribute_groups as $attribute_group) { ?>
      <?php if ($attribute_group['name'] == 'myattributename') { ?>
        <?php foreach ($attribute_group['attribute'] as $attribute) { ?>
          <?php $first_field_val = $attribute['text']; ?>

<!-- Here I can put the code I need, and then use  <?php echo $first_field_val ?>  to write the value I want.-->

       <?php } ?>
      <?php } ?>
    <?php } ?>
  <?php } ?>

How to use:

1) Make a new attribute group if you don't have it
2) Make a new attribute. The attribute name will be used in the code above. Ex: "squarefeet"
3) Edit the code above to reflect the attribute name <?php if ($attribute_group['name'] == 'squarefeet') { ?>

4) Add the above code (in the code brackets) to the product.tpl file, where you want to use it.
5) In the product where ypu want to use this function, add an attribute value to "squarefeet"
6) Voila :-)

POS
Newbie

Posts

Joined
Tue Mar 22, 2011 11:34 pm

Post by Avvici » Wed May 30, 2012 8:29 pm

In your product.tpl you would do this:

Code: Select all

 <?php foreach ($attribute_group['attribute'] as $attribute) { ?>
        <tr>
          <td><?php echo $attribute['name']; ?></td>
          <td><?php echo $attribute['text']; ?></td>
          <td><?php echo $attribute['attribute_id']; ?></td>
        </tr>
        <?php } ?>
I just added the <?php echo $attribute['attribute_id']; ?>

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by POS » Wed May 30, 2012 8:42 pm

Thank you :-)

What I meant was to get the 'name' value for attribute_id=12 for instance.

Is there a short code for that, that looks better than mine? (I am not a coder, so I really don't know what I do ;-) )

POS
Newbie

Posts

Joined
Tue Mar 22, 2011 11:34 pm
Who is online

Users browsing this forum: Bing [Bot] and 83 guests