0

Currently my code is

use Magento\Eav\Model\Attribute;
use Magento\Eav\Model\AttributeRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;


 $searchCriteria = $this->searchCriteriaBuilder->create();

 /** @var Attribute[] $attributesList */
 $attributesList = $this->attributeRepository
                  ->getList('catalog_product', $searchCriteria)
                  ->getItems();

  foreach ($attributesList as $attribute) { 
      // For example; attribute code: sample (see below image)
      print_r($attribute->getStoreLabels()) //(array) Working: getting all the labels available per store scope
      foreach($attribute->getOptions() as $option){
           print_r($option->getLabel()) //(string) Working but getting the admin store scope
           print_r($option->getStoreLabels()) // NOT WORKING: gives only empty value; 
      }
  }

I want to get all the labels from all store view as it can be different. Many thanks for the help!

Example of one product attribute is below:

enter image description here

1

Try this way - in your foreach, before calling any method to retrieve the option labels, set the store ID of interest $attribute->setStoreId($storeId) and then try retrieving the option labels as you already tried. If this works properly, consider moving the code to a dedicated model and inside it loop through all the stores you need.

2
  • Thanks! for the response, I am trying to set the storeId but the method is not available. Then I found another article that say's it is using a magic method (setData()), thus storeId worked for me just like what you said. Do you also know how can I update these attribute option per store scope? Thanks – bryanxkier Apr 20 at 4:23
  • yes, the method is a magic method, there is no concrete definition of it. please consider marking this answer as solution and open another question regarding the option updates. thanks – Diana Botean Apr 20 at 7:42

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.