2

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?

3

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'];
    }
}
2
  • Argh... Crazy Magento with it's magic methods! I thought it would be something like this, but Magento\Catalog\Model\ResourceModel\Eav\Attribute doesn't have a setStoreId()-method. However, it has a setData() / magic methods so your solution is correct. But I sure hope that those magic methods get deprecated soon. – Giel Berkers Dec 23 '16 at 9:19
  • how to use setter here? I am using $attr->setOptions([$option]); , it is storing only to admin – jafar pinjar Dec 14 '18 at 14:18

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.