1

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 = \Magento\Framework\App\ObjectManager::getInstance();

/** @var \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute */
$attribute = $om->get(\Magento\Catalog\Api\ProductAttributeRepositoryInterface::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>
2

3 Answers 3

1

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

0

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 \Mbs\HomeMaterial\Block\ActivityList

  {{block class="Mbs\HomeMaterial\Block\ActivityList" 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="Mbs\HomeMaterial\Block\ActivityList" name="catalog.activity"
                   template="Mbs_HomeMaterial::listactivity.phtml" before="-">
            </block>
        </referenceContainer>

    </body>
</page>

<?php


namespace Mbs\HomeMaterial\Block;


use Magento\Framework\View\Element\Template;

class ActivityList extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
     */
    private $attributeRepository;

    public function __construct(
        Template\Context $context,
        \Magento\Catalog\Api\ProductAttributeRepositoryInterface $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 \Mbs\HomeMaterial\Block\ActivityList */
?>

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> 
0

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 = \Magento\Framework\App\ObjectManager::getInstance();

/** @var \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute */
$attribute = $om->get(\Magento\Catalog\Api\ProductAttributeRepositoryInterface::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>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.