Magento 2 Produkteigenschaften in Price-Block abrufen

Task during the development of an online shop at our web agency: We want to hide the price of a product based on an option of a selected attribute, both in the list view (categories, search, etc.) and in the product detail view.

 

 We find what we are looking for here in the file (including the overwrite):

 

/app/design/frontend/PACKAGE_NAME/TEMPLATE_NAME/Magento_Catalog/templates/product/price/amount/default.phtml

 

So how can we load a product here, regardless of whether it is in the list or detail view? There are only a few methods available here, but at least the block class \Magento\Framework\Pricing\Render\Amount has a function getSaleableItem(), with which we can obtain a "stripped-down" product class and use getId() to retrieve the relevant product ID.

 

We can then use the ObjectManager to load the complete product:

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

saleableItemId = $block->getSaleableItem()->getId();

$product = $objectManager->get('Magento\Catalog\Model\Product')->load($saleableItemId);

 

and retrieve the attribute values, for example:

 

$ext_stat_id = $product->getResource()->getAttribute('ext_stat')->getSource()->getOptionId($product->getAttributeText('ext_stat'));

 

and then later use an If statement to hide the price if desired, e.g.:

 

if($ext_stat_id!=132) {

 

That's it! Observed at our web agency in an online shop running Magento 2.3.5.