How to get Attribute Text depending on storeId in magento2
-
18-03-2021 - |
Question
I want to get attributeText of product depending on what storeId is passed.
Solution
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;
}
OTHER TIPS
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!
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));
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange