0

In Magento 1, I was able to get attibute value with following code:

Mage::getResourceModel('catalog/product')->getAttributeRawValue($_id, 'att_price', $_storeId)

How can I get the value in Magento 2, in tempalte

vendor/magento/module-catalog/view/base/templates/product/price/amount/default.phtml  

I copied it to my theme:

/app/design/frontend/xxx/ddd/Magento_Catalog/templates/product/price/amount/default.phtml

Thanks for the help! Martin

2 Answers 2

0

In Magento 2 Get Attribute Raw Value Like This :-

$protected $_resourceProduct;

public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product $resourceProduct)
         {
             $this->_resourceProduct = $resourceProduct;
         }

         public function getData()
         {

            $value = $this->_resourceProduct->getAttributeRawValue($productId,'attribute_name',$store_id);
         }
8
  • Thanks. But from where do I get $productId?
    – matin
    Commented Jun 21, 2019 at 8:18
  • in your phtml file you can get product id using object manager Commented Jun 21, 2019 at 8:22
  • 1
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); Commented Jun 21, 2019 at 8:22
  • or another option is to override the amount.php file and add your custom function for get current product data Commented Jun 21, 2019 at 8:23
  • Thanks! But echo $product->getId(); returns error?
    – matin
    Commented Jun 21, 2019 at 8:26
0

You can use this

echo $block->getSaleableItem()->getResource()->getAttribute('attribute_name')->getFrontend()->getValue($block->getSaleableItem());
1
  • $product = $block->getSaleableItem(); echo $product->getData('attribute_name'); Work for me
    – iskander
    Commented Apr 2 at 10:35

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.