5

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 ?.

  • Did you resolve your issue? – Shoaib Munir Jun 27 '19 at 4:29
2

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();
  • no changes , its returns always null value – Shafeel Sha Jun 26 '19 at 10:12
  • on which file you are using this? – Shoaib Munir Jun 26 '19 at 10:16
  • this is my file "app/code/Wac/DropPin/view/frontend/templates/cart/item/default.phtml" check below code,please $_item = $block->getItem(); $product = $_item->getProduct(); $isVisibleProduct = $product->isVisibleInSiteVisibility(); echo '<pre>'; print_r(get_class_methods($_item->getProduct())); die('******************************'); – Shafeel Sha Jun 26 '19 at 10:21
  • 1
    Further updated my answer, brother, this is the last solution that I can give, hope it will work for you :) – Shoaib Munir Jun 26 '19 at 10:45
  • 1
    Welcome. please check the answer last 3 lines would surely work for you – Shoaib Munir Jun 26 '19 at 10:47
2

Use the following code

$weight = $product->getResource()->getAttribute('weight_spices')->getFrontend()->getValue($product);

1

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);
  • +1 on your explanation :) – Shoaib Munir Aug 14 '19 at 13:04

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.