2

I'm trying to get store label for admin (store 0 - zero) but since I have something in "Default Store Label" I'm getting only that value. I need to find the solution to get always only the admin (unique) value. If solution will be like override/plugin for core - that will be applicable too.

Code which I'm using:

//($this->attributeOptionManagement = \Magento\Eav\Api\AttributeOptionManagementInterface)

$existingOptions = $this->attributeOptionManagement->getItems(Product::ENTITY, $attributeCode);
foreach ($existingOptions as $existingOption) {
    $this->existingOptions[$attributeCode][] = $existingOption->getLabel();
}

So if I have this:

enter image description here

I will have this output based on my code:

Array
(
    [0] =>  
    [1] => Black
    [2] => Charcoal
    [3] => Cream
    [4] => Gray
    [5] => Green
    [6] => Lightning
    [7] => Natural
    [8] => Olive
)

And if I have this:

enter image description here

The output will be like that:

Array
(
    [0] =>  
    [1] => BLACK
    [2] => CHARC
    [3] => CRBBL
    [4] => GR/NA
    [5] => GYCHR
    [6] => LTHTG
    [7] => NATUR
    [8] => OLIVE
)
3

I have tried this code and it works fine for me:

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
    $attribute = $eavConfig->getAttribute('catalog_product', 'color')->setStoreId(0);
    $options = $attribute->getSource()->getAllOptions();
    foreach ($options as $existingOption) {
        echo $existingOption['label'];
    }
?>

Can you inject the class \Magento\Eav\Model\Config and try this solution. Above code is tested. Also you can try the below code but its not tested.

$existingOptions = $this->attributeOptionManagement->getItems(Product::ENTITY, $attributeCode)->setStoreId(0);
| improve this answer | |
  • The first method works fine! Thank you very much! I will test second and reply. P.S.: For people looking for an answer - don't use ObjectManager directly in your code. It's good for a quick demo but not good for production code. – Fred Orosko Dias Sep 12 '18 at 18:00
  • Unfortunately second method doesn't work. There are no such method as setStoreId(). – Fred Orosko Dias Sep 12 '18 at 18:02
  • I have used object manager just to guide you how to get admin values but yes its not a good practice. Thanks @Fred. Happy Coding. – Sukumar Gorai Sep 12 '18 at 18:13
  • @SukumarGorai, setOptions is not working for me, storing only for Admin view, can you please help on this – jafar pinjar Dec 14 '18 at 15:28
  • @SukumarGorai, I am getting below error after creating boolean attribute Class Attribute name does not exist, here is my new question magento.stackexchange.com/questions/261542/… – jafar pinjar Feb 13 '19 at 12:07
0

you can use below to get default attribute text

$_condition = $_product->getAttributeText('condition');
$prod= $_product->setStoreId(0);
$_coditionDefault =$_product->getResource()->getAttribute('condition')->getFrontend()->getValue($_product);
| improve this answer | |

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.