如何解决Magento 2:获取自定义属性会返回“未捕获的错误:在null上调用成员函数getResource”
我正在尝试在商品详细信息页面上的商品价格旁边显示自定义属性的值。该属性的名称为“ measurement_unit”,是Magento 2管理员中的下拉菜单。 我尝试了几种方法调用属性,但是没有任何效果。现在我收到此错误:
'[09-Sep-2020 11:52:31 UTC] PHP致命错误:未捕获错误:在public_html / vendor / magento / module-catalog / view / base / templates中以空值调用成员函数getResource() /product/price/final_price.phtml:50'
<?PHP
$_product = $block->getProduct();
$attvalue = $_product->getResource()->getAttribute('measurement_unit')->getFrontend()->getValue($_product);
echo $attvalue;exit;
?>
整个文件如下:
final_price.html<?PHP
/**
* copyright © Magento,Inc. All rights reserved.
* See copYING.txt for license details.
*/
?>
<?PHP
/** @var \Magento\Catalog\Pricing\Render\FinalPriceBox $block */
/** 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 if ($block->hasspecialPrice()) :?>
<span class="special-price">
<?= /* @noEscape */ $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">
<?= /* @noEscape */ $block->renderAmount($priceModel->getAmount(),[
'display_label' => __('Regular Price'),'price_id' => $block->getPriceId('old-price-' . $idSuffix),'price_type' => 'oldPrice','skip_adjustments' => true
]); ?>
</span>
<?PHP else :?>
<?= /* @noEscape */ $block->renderAmount($finalPriceModel->getAmount(),[
'price_id' => $block->getPriceId('product-price-' . $idSuffix),'schema' => $schema
]);?>
<?PHP
$_product = $block->getProduct();
$attvalue = $_product->getResource()->getAttribute('measurement_unit')->getFrontend()->getValue($_product);
echo $attvalue;exit;
?>
<?PHP endif; ?>
<?PHP if ($block->showMinimalPrice()) :?>
<?PHP if ($block->getUseLinkForAsLowAs()) :?>
<a href="<?= $block->escapeUrl($block->getSaleableItem()->getProductUrl()) ?>" class="minimal-price-link">
<?= /* @noEscape */ $block->renderAmountMinimal() ?>
</a>
<?PHP else :?>
<span class="minimal-price-link">
<?= /* @noEscape */ $block->renderAmountMinimal() ?>
</span>
<?PHP endif?>
<?PHP endif; ?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。