how i can get custom attribute value in magento 2 final_price.phtml
-
08-03-2021 - |
题
I have created a product attribute in Magento 2 admin
I want to get custom attribute price in magento 2 final_price.phtml
How I can get this??
I tried this code but
showing null in $_product
<?php
$_product = $block->getProduct();
$attvalue = $_product->getResource()->getAttribute('attribute_id')->getFrontend()->getValue($_product);
echo $attvalue;
?>
final_price.phtml
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php
/** @var \Magento\Catalog\Pricing\Render\FinalPriceBox $block */
// Daily deal Helper
$helper=$this->helper('Smartwave\Dailydeals\Helper\Data');
$productId = $block->getSaleableItem()->getId();
/** ex: \Magento\Catalog\Pricing\Price\RegularPrice */
/** @var \Magento\Framework\Pricing\Price\PriceInterface $priceModel */
$priceModel = $block->getPriceType('regular_price');
/** ex: \Magento\Catalog\Pricing\Price\FinalPrice */
/** @var \Magento\Framework\Pricing\Price\PriceInterface $finalPriceModel */
$finalPriceModel = $block->getPriceType('final_price');
$idSuffix = $block->getIdSuffix() ? $block->getIdSuffix() : '';
$schema = ($block->getZone() == 'item_view') ? true : false;
?>
<?php
//echo "TT001";
?>
<?php
$_product = $block->getProduct();
//$attvalue = $priceModel->getResource()->getAttribute('mrp_price')->getFrontend()->getValue($_product);
//echo $attvalue;
?>
<?php if($helper->isDealProduct($productId)) : ?>
?>
<?php
$deal_final_price_amount = number_format($helper->getDealproductbyId($productId),2);
?>
<span class="special-price">
<span class="price-container price-final_price tax weee">
<span class="price-label">Special Price</span>
<span id="product-price-<?php echo $idSuffix; ?>" data-price-amount="<?php echo $deal_final_price_amount; ?>" data-price-type="finalPrice" class="price-wrapper ">
<span class="price"><?php echo $helper->getcurrencySymbol().''.$deal_final_price_amount; ?></span>
</span>
</span>
</span>
<span class="old-price">
<?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
'display_label' => __('Regular Price'),
'price_id' => $block->getPriceId('old-price-' . $idSuffix),
'price_type' => 'oldPrice',
'include_container' => true,
'skip_adjustments' => true
]); ?>
</span>
<?php elseif ($block->hasSpecialPrice()): ?>
<span class="special-price">
<?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
'display_label' => __('Special Price'),
'price_id' => $block->getPriceId('product-price-' . $idSuffix),
'price_type' => 'finalPrice',
'include_container' => true,
'schema' => $schema
]); ?>
</span>
<span class="old-price">
<?php /* @escapeNotVerified */ echo $block->renderAmount($priceModel->getAmount(), [
'display_label' => __('Regular Price'),
'price_id' => $block->getPriceId('old-price-' . $idSuffix),
'price_type' => 'oldPrice',
'include_container' => true,
'skip_adjustments' => true
]); ?>
</span>
<?php else : ?>
<?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
'price_id' => $block->getPriceId('product-price-' . $idSuffix),
'price_type' => 'finalPrice',
'include_container' => true,
'schema' => $schema
]); ?>
<?php endif; ?>
</span>
<?php if ($block->showMinimalPrice()): ?>
<?php if ($block->getUseLinkForAsLowAs()):?>
<a href="<?php /* @escapeNotVerified */ echo $block->getSaleableItem()->getProductUrl(); ?>" class="minimal-price-link">
<?php /* @escapeNotVerified */ echo $block->renderAmountMinimal(); ?>
</a>
<?php else:?>
<span class="minimal-price-link">
<?php /* @escapeNotVerified */ echo $block->renderAmountMinimal(); ?>
</span>
<?php endif?>
<?php endif; ?>
解决方案
You can get custom product attribute value in final_price.phtml using following code
<?php
$_product = $block->getPriceType('final_price')->getProduct();
$attvalue = $_product->getCustomAttribute('attribute_id')->getValue();
?>