6

I am using the following code to fetch the admin option value as well as the frontend label from an ID list of vales of a multiselect.

    $attribute = $this->eavConfig->getAttribute('catalog_product', self::ATTRIBUTE_CODE);
    $adminAttribute = clone $attribute;
    $adminAttribute->setStoreId(0);

    $result = [];
    foreach(array_unique($allIds) as $id) {
        $description = $attribute->getSource()->getOptionText($id);
        $fileName = $adminAttribute->getSource()->getOptionText($id);
        $result[$fileName] = $description;
    }

In a block on the product list this seems to work, while it does not look clean for me. Anyways, on a product detail page it does not (I get for $fileName still the frontend value, not the admin value).

Is there a proper way to do this?

EDIT I was thinking about loading the product in Admin store, still I get the non-admin-store value:

$adminProduct = $this->_productRepository->getById($product->getId(), false, 0);
$adminProduct->getAttributeText(self::ATTRIBUTE_CODE);
7
+250

Assuming your attribute options are coming from a table, and I assume they are because you reference admin and store values, we should be able to call \Magento\Eav\Model\Entity\Attribute\Source\Table::getAllOptions which depending on the arguments will return the store values or the default (admin) values.

With that you could try something like this:

protected function getAttributeOptions($attributeCode) {
    $merged = [];

    $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
    $source = $attribute->getSource();

    if($source instanceof \Magento\Eav\Model\Entity\Attribute\Source\Table) {
        $options = $this->optionArrayToHashmap($source->getAllOptions(false, false));
        $adminOptions = $this->optionArrayToHashmap($source->getAllOptions(false, true));

        foreach($adminOptions as $k => $v) {
            $merged[$v] = isset($options[$k]) ? $options[$k] : $v;
        }
    }

    return $merged;
}

protected function optionArrayToHashmap($array) {
    $hashMap = [];

    foreach($array as $option) {
        $hashMap[$option['value']] = (string) $option['label'];
    }

    return $hashMap;
}

I can't confirm if this works where you are trying it but it's worth a shot. When I run it against demo store data on the "color" attribute with some values specific to the store view ("Store " prepended) I get the following results:

Array (
    [Black] => Store Black
    [Blue] => Store Blue
    [Brown] => Store Brown
    [Gray] => Store Gray
    [Green] => Green
    [Lavender] => Lavender
    [Multi] => Multi
    [Orange] => Orange
    [Purple] => Purple
    [Red] => Red
    [White] => White
    [Yellow] => Yellow
)
0

I have just tried this for particular one attribute & it worked for me so I am posting this over here May be this can lead you to something you want to achieve.

<?php

class ClassName {

    public function __construct(
        \Magento\Eav\Model\Config $eavConfig
    ){
        $this->eavConfig = $eavConfig;
    }

    public function getAttributes(){
        $attributeCode = 'color';
        $attributeId = 4;
        $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
        $back = $attribute->setStoreId(0)->getSource()->getOptionText($attributeId);
        $front = $attribute->setStoreId(1)->getSource()->getOptionText($attributeId);
        echo "Frontend (User) Value = ".$front." & Backend (Admin) Value = ".$back;
    }
}

We also can achieve this for the all attributes of particular one product via product Id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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