TransWikia.com

get options value of each options label of an attribute

Magento Asked by Toranto mina on October 2, 2021

I have an alphabetical sorted list of option label of an attribute, I want to get option value of each option label
this is my code:

<?php
$om = MagentoFrameworkAppObjectManager::getInstance();

/** @var MagentoCatalogApiDataProductAttributeInterface $attribute */
$attribute = $om->get(MagentoCatalogApiProductAttributeRepositoryInterface::class)->get("manufacturer");
$image_source_files = [];
?>

<div id="main_content_wrap" class="outer">
    <section id="main_content" class="inner">

        <ul id="demoOne" class="demo">
<?php foreach ($attribute->getOptions() as $option) {

    $src =  $option->getLabel() ;
    $image_source_files[] = $src;
    
    
}
sort($image_source_files);


foreach ($image_source_files as $value) { 
    $srca = $value ;
    $srcb = $this->getViewFileUrl("images/" . $srca . ".png");
?>  
<li>                


<a href="<?php echo $option->getValue()?>  ">
<p style="display:none;">
<?php echo $srca; ?></p>
<img src="<?php echo $srcb; ?>"  onerror="this.onerror=null; this.remove(); " style="padding: 10px 20px;"  width="200" height="200"/>
</a>
</li>
    <?php

}

?>
</ul>   
</section>
</div>

3 Answers

Your code is 99% right only change this

sort($image_source_files);

Replace with this line

$image_source_files = sort($image_source_files);

Click here to show reference

Your code after change

<div id="main_content_wrap" class="outer">
    <section id="main_content" class="inner">
        <ul id="demoOne" class="demo">
            <?php
            foreach ($attribute->getOptions() as $option) {
                $src =  $option->getLabel() ;
                $image_source_files[] = $src;
            }

            $image_source_files = sort($image_source_files);  // Only One Change

            foreach ($image_source_files as $value) {
                $srca = $value ;
                $srcb = $this->getViewFileUrl("images/" . $srca . ".png");
                ?>
                <li>
                    <a href="<?php echo $option->getValue()?>  ">
                        <p style="display:none;">
                        <?php echo $srca; ?></p>
                        <img src="<?php echo $srcb; ?>"  onerror="this.onerror=null; this.remove(); " style="padding: 10px 20px;"  width="200" height="200"/>
                    </a>
                </li>
                <?php
            }
            ?>
        </ul>
    </section>
</div>

I Hope This Helps You

Answered by Msquare on October 2, 2021

Did you try

<?php foreach ($attribute->getOptions() as $option) {

$src =  $option->getLabel() ;
$image_source_files[] = $src;
 $attributeValue =  $option->getValue() ; // this
}

Edited, cant run this but hopefully it works

<?php
$om = MagentoFrameworkAppObjectManager::getInstance();

/** @var MagentoCatalogApiDataProductAttributeInterface $attribute */
$attribute = $om->get(MagentoCatalogApiProductAttributeRepositoryInterface::class)->get("manufacturer");
$image_source_files = [];
// create an array to hold values
$image_values = [];
?>

<div id="main_content_wrap" class="outer">
    <section id="main_content" class="inner">

        <ul id="demoOne" class="demo">
<?php foreach ($attribute->getOptions() as $option) {

    $src =  $option->getLabel() ;
    $image_source_files[] = $src;
    // make an assoc array 
    $image_values[$src] = $option->getValue();
    
}
sort($image_source_files);


foreach ($image_source_files as $value) { 
    $srca = $value ;
    // pull out correct value based on key matching srca
    $srcval = $image_values[$value];
    $srcb = $this->getViewFileUrl("images/" . $srca . ".png");
?>  
<li>                


<a href="<?php echo  $srcval;?>  ">
<p style="display:none;">
<?php echo $srca; ?></p>
<img src="<?php echo $srcb; ?>"  onerror="this.onerror=null; this.remove(); " style="padding: 10px 20px;"  width="200" height="200"/>
</a>
</li>
    <?php

}

?>
</ul>   
</section>
</div>

Answered by bjornredemption on October 2, 2021

I have made a module (minimalistic) that implements your requirement:

the module is at https://bitbucket.org/magstaging/homematerial/src/master/

your code already shows attribute label and attribute value as it is.

However, the module will enable you to use the code within your system either using normal layout syntax or directly from a cms block.

This module was developed on Magento demo and therefore I have used the attribute 'activity' but you can change this in the Block MbsHomeMaterialBlockActivityList

  {{block class="MbsHomeMaterialBlockActivityList" name="catalog.activity"
    template="Mbs_HomeMaterial::listactivity.phtml" }}

or

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="MbsHomeMaterialBlockActivityList" name="catalog.activity"
                   template="Mbs_HomeMaterial::listactivity.phtml" before="-">
            </block>
        </referenceContainer>

    </body>
</page>

<?php


namespace MbsHomeMaterialBlock;


use MagentoFrameworkViewElementTemplate;

class ActivityList extends MagentoFrameworkViewElementTemplate
{
    /**
     * @var MagentoCatalogApiProductAttributeRepositoryInterface
     */
    private $attributeRepository;

    public function __construct(
        TemplateContext $context,
        MagentoCatalogApiProductAttributeRepositoryInterface $attributeRepository,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->attributeRepository = $attributeRepository;
    }

    public function getActivityAttribute()
    {
        return $this->attributeRepository->get('activity');
    }

    public function getActivityListOrderedAlphabetically()
    {
        $options = $this->attributeRepository->get('activity')->getOptions();

        $result = [];
        foreach ($options as $info) {
            $result[$info->getlabel()] = $info;
        }

        ksort($result);

        return $result;
    }
}
<?php

/* @var $block MbsHomeMaterialBlockActivityList */
?>

list activity


<div id="main_content_wrap" class="outer">
<section id="main_content" class="inner">

<ul id="demoOne" class="demo">
  <?php foreach ($block->getActivityListOrderedAlphabetically() as $option): ?>
  <li>label: <?php echo $option->getLabel(); ?>, value: <?php echo $option->getValue(); ?></li>

  <?php

  $src = $option->getLabel() ;


  $image_source_files[] = $src;

  ?>  

<?php endforeach; ?>
</ul>
</section>
</div> 

Answered by Herve Tribouilloy on October 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP