How to Display Attribute value in magento 2 using getProducts() Method
-
05-03-2021 - |
Question
When I Try to get data using below code,But return null value.
$_attributeValue = $block->getProduct()->getResource()->getAttribute('weight_spices')->getFrontend()->getValue($block->getProduct());
My Expected Result like (100g, 1Kg,1L etc ),I have weight_spices, weight_oil Attribute to identify spices and oil weight separately. I'm trying lots of code but not working.how can i print expected Result ?.
Solution
If it is a dropdown attribute then use this:
$_item->getProduct()->getAttributeText('weight_spices');
or
$_item->getProduct()->getResource()->getAttributeRawValue($this->getProduct()->getId(),'weight_spices',$this->_storeManager->getStore()->getId());
rest of the attributes could be get by this:
$_item->getProduct()->getWeightSpices();
If above code wont work for you then probably you have enabled flat table structure and you need to set "use in product listing" option from manage attributes
Store -> Attributes -> Products
Edit your attribute. In Storefront Properties tab select Used in Product Listing to "Yes"
then check again after reindex
If you still not getting your result, then use this. (Not a good approach, but your last solution)
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getProduct()->getId());
$product->getWeightSpices();
OTHER TIPS
Use the following code
$weight = $product->getResource()->getAttribute('weight_spices')->getFrontend()->getValue($product);
I got two solutions for this issue ! , i think 1st solution is better, because it reduce loading and make fast response in our application last solution is the final solution but it make your application slow .Thank you for the supporters to find this final solution
Solution 1 :
Create a Helper Class file and paste below code
app/code/{vendor}/{module}/Helper/Data.php
<?php
namespace Wac\DropPin\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $productRepository;
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
}
public function loadMyProduct($sku)
{
$productData = $this->productRepository->get($sku);
return $productWeight = $productData->getResource()->getAttribute('weight_spices')->getFrontend()->getValue($productData) ? $productData->getResource()->getAttribute('weight_spices')->getFrontend()->getValue($productData) : $productData->getResource()->getAttribute('weight_oil')->getFrontend()->getValue($productData);
}
}
?>
Then Edit default.phtml file and paste below code 'l'm simply pass the sku on my function in this section'
app/code/{vendor}/{module}/view/frontend/templates/cart/item/default.phtml
<?php
$sku = $_item->getProduct()->getSku();
$customHelper = $this->helper('Wac\DropPin\Helper\Data');
echo $product = $customHelper->loadMyProduct($sku);
?>
Finally Run all commands
Solution 2 :
(Not a good approach, but my last solution ) to print Product attribute values Separately like 100g,1 Litter,1kg etc.. using Object manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$item = $this->getItem();
$productFactory = $objectManager->create('\Magento\Catalog\Model\ProductRepository');
$productData = $productFactory->get($_item->getSku($item));
$productHsn = $productData->getHsn();
echo $productWeight = $productData->getResource()->getAttribute('weight_spices')->getFrontend()->getValue($productData) ? $productData->getResource()->getAttribute('weight_spices')->getFrontend()->getValue($productData) : $productData->getResource()->getAttribute('weight_oil')->getFrontend()->getValue($productData);