3

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

1
  • $attribute->getStoreLabel() for current store attribute label Dec 9 '19 at 5:33
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
+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!

3
  • won't this return the id of an option instead of the label?
    – Marius
    Dec 6 '19 at 12:52
  • No @Marius, It will return value of attribute not ID. :) Dec 6 '19 at 13:09
  • How to get item attribute values based on store-level on order summary section at checkout Magento 2 Mar 31 '20 at 10:20
2

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 Mar 31 '20 at 10:21
  • This answer is exactly what worked for me instead of using another object manager. Mar 5 at 9:40

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.