Keyword

Extrafields on Backend Items list

  • Fábio Jordan
  • Fábio Jordan's Avatar Topic Author
  • Offline
  • Senior Member
  • Nothing like a good coffee to make a better web
More
2 weeks 1 day ago #181595 by Fábio Jordan
Extrafields on Backend Items list was created by Fábio Jordan
Hi.

I'm wondering if there's a better solution to retrieve Extrafields on K2 Backend in the items list view. The main objective is to show some quick infos about the items without have to oppening one by one. In my example, it retrieves a link and also the price for the item.

Right now, I'm using this code:
<?php $string = $row->extra_fields; $extra=json_decode($string); ?>
<?php if ($extra[2]->value): ?>
<?php if (substr($extra[1]->value, 0,5) === 'https'): ?>
<a style="border: 1px solid;padding: 5px;" href="<?php echo $extra[1]->value ?>" target="_blank"><img src="/images/amazon36.png" style="height:18px";> R$: <?php echo $extra[3]->value ?></a>
<?php endif; ?>
<?php if (substr($extra[0]->value, 0,5) === 'https'): ?>
<a style="border: 1px solid;padding: 5px;" href="<?php echo $extra[0]->value ?>" target="_blank"><img src="/images/amazon36.png" style="height:18px";> R$: <?php echo $extra[2]->value ?></a>
<?php endif; ?>
<?php endif; ?>

Everything works fine, BUT I have to use this workaround for check the first letters of the extrafield, since depending on the item, it might have more extrafields, and this will change the id of the extrafield retrieved (sometimes it's 0, but it can also be 1 for other articles).

The result is as shown in the attachment.

If anyone has some hint.

Thanks in advance!
Attachments:

Please Log in or Create an account to join the conversation.

More
1 week 5 days ago #181602 by Vincent Manning
Replied by Vincent Manning on topic Extrafields on Backend Items list
<?php
$string = $row->extra_fields;
$extra = json_decode($string);

// Define the labels of the Extrafields you want to display
$labelsToDisplay = array('Link', 'Price');

// Iterate through the Extrafields
foreach ($extra as $field) {
// Check if the current Extrafield's label is in the list of labels to display
if (in_array($field->name, $labelsToDisplay)) {
// Check if the value of the Extrafield starts with 'https'
if (substr($field->value, 0, 5) === 'https') {
// Display the Extrafield value as a link with the label and price
echo '<a style="border: 1px solid;padding: 5px;" href="' . $field->value . '" target="_blank"><img src="/images/" style="height:18px";>' . $field->name . ': R$ ' . $field->value . '</a>';
}
}
}
?>

Please Log in or Create an account to join the conversation.

  • Fábio Jordan
  • Fábio Jordan's Avatar Topic Author
  • Offline
  • Senior Member
  • Nothing like a good coffee to make a better web
More
6 days 17 hours ago #181611 by Fábio Jordan
Replied by Fábio Jordan on topic Extrafields on Backend Items list
Thanks for your suggestion, Vincent!
But it seems that there's no way to get the name of the extrafield.
I just did a var_dump and what I get is as it follows
{
 [0]=> object(stdClass)#605 (2)
{
 ["id"]=> string(3) "396" ["value"]=> string(0) ""
}
 [1]=> object(stdClass)#600 (2)
{
 ["id"]=> string(3) "350" ["value"]=> string(0) ""
}
 [2]=> object(stdClass)#601 (2)
{
 ["id"]=> string(2) "67" ["value"]=> string(23) "https://amzn.to/3W5u0OL"
}
 [3]=> object(stdClass)#604 (2)
{
 ["id"]=> string(2) "93" ["value"]=> string(10) "B0C9VQN8SL"
}
 [4]=> object(stdClass)#607 (2)
{
 ["id"]=> string(2) "70" ["value"]=> string(8) "5.499,00"...
 

It's a huge dump, so there's only a part of it.
But it was a good thing to do this, because now I know a better way to verify the IDs.
  <?php if ($extra[2]->id == '67'):?>
             <?php if ($extra[4]->value):?>
               <a style="border: 1px solid;padding: 5px;" href="<?php echo $extra[2]->value ?>" target="_blank"><img src="/images/amazon36.png" style="height:18px";> R$ <?php echo $extra[4]->value ?></a>
                <?php endif; ?>
                <?php else: ?>
                <?php if ($extra[1]->id == '67'):?>
                <a style="border: 1px solid;padding: 5px;" href="<?php echo $extra[1]->value ?>" target="_blank"><img src="/images/amazon36.png" style="height:18px";> R$ <?php echo $extra[3]->value ?></a>
                                <?php endif; ?>
                                <?php endif; ?>
                                <?php endif; ?>
 

I guess there's a better solution, but it feels better not having to check https...

Anyway, there's an example of the code in case someone needs it.

Regards.

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum