Get option label associated with admin store in Magento 2
-
03-10-2020 - |
Question
I want to go through all options of attributes configured for filtering and output the label of the Admin Store View. I only managed to get the Default Store View (store_id 1) label with the following code:
$coll = $this->_objectManager->create(\Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection::class);
$coll->addIsFilterableFilter();
$layeredNavAttributes = $coll->load()->getItems();
foreach ($layeredNavAttributes AS $attr) {
$options = $attr->getSource()->getAllOptions();
foreach ($options AS $option) {
echo $option['label'];
}
}
How can I get the labels of the Admin Store View (store_id 0) instead?
Solution
After some more trying, I came to the answer myself. Just call the setStoreId()
method on each attribute. So the code to get the Admin labels looks like this:
$coll = $this->_objectManager->create(\Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection::class);
$coll->addIsFilterableFilter();
$layeredNavAttributes = $coll->load()->getItems();
foreach ($layeredNavAttributes AS $attr) {
$attr->setStoreId(0);
$options = $attr->getSource()->getAllOptions();
foreach ($options AS $option) {
echo $option['label'];
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange