5

I want to get attributeText of product depending on what storeId is passed.

1
  • $attribute->getStoreLabel() for current store attribute label Commented Dec 9, 2019 at 5:33

3 Answers 3

5
+50

You can try this..

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productId = 2; //Product ID
    $storeId = 1 //Store ID
    $product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
    $attributeValue = $product->getResource()->getAttributeRawValue($product->getId(),'attribute_code',$storeId); //change attribute_code
    var_dump($attributeValue);
?>

Hope this will work for you!

5
  • won't this return the id of an option instead of the label?
    – Marius
    Commented Dec 6, 2019 at 12:52
  • No @Marius, It will return value of attribute not ID. :) Commented Dec 6, 2019 at 13:09
  • How to get item attribute values based on store-level on order summary section at checkout Magento 2 Commented Mar 31, 2020 at 10:20
  • The question asked for the attributeText, not the raw value. Commented Jun 15, 2023 at 14:32
  • It's will return default store value if specific store does not have value for that field. Commented Sep 5, 2023 at 17:04
4

You can get AttributeText by passing storeId from product collection:

$stores = $this->_storeManager->getStores();
foreach ($stores as $key => $store) {
    $store_id          = $store->getId();
    $productCollection = $this->getProductCollection($store_id);
    foreach ($productCollection as $_product) {
        $productdetails    = $_product->getData();
        $_conditionDefault = $_product->getResource()
                                      ->getAttribute('condition')
                                      ->setStoreId($store_id)
                                      ->getFrontend()
                                      ->getValue($_product);
        echo $_conditionDefault;
    }
}

protected function getProductCollection($store_id)
{
    $collection = $this->_productCollectionFactory
        ->create()
        ->addAttributeToSelect('*')->addStoreFilter($store_id)
        ->setVisibility($this->_productVisibility->getVisibleInSiteIds())
        ->addAttributeToFilter(
            'status',
            array('eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
        );

    return $collection;
}
3

Haven't tested, but this might work:

Let's say $product is the product instance.
and $code is the attribute code for which you need the label. and $storeId the id of the store you need.

$attribute = $product->getResource()->getAttribute($code);
$attribute->setStoreId($storeId);
$text = $attribute->getSource()->getOptionText($product->getData($code));
2
  • How to get item attribute values based on store-level on order summary section at checkout Magento 2 Commented Mar 31, 2020 at 10:21
  • This answer is exactly what worked for me instead of using another object manager. Commented Mar 5, 2021 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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