magento - Magento 2:获取自定义属性返回“未捕获的错误:在 null 上调用成员函数 getResource()”
问题描述
我正在尝试在产品详细信息页面上的产品价格旁边显示自定义属性的值。该属性的名称为“measurement_unit”,是 Magento 2 管理员中的下拉菜单。我尝试过几种方式调用该属性,但没有任何效果。现在我得到这个错误:
'[2020 年 9 月 9 日 11:52:31 UTC] PHP 致命错误:未捕获的错误:在 public_html/vendor/magento/module-catalog/view/base/templates/product/ 中调用成员函数 getResource() on null价格/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',
'include_container' => true,
'skip_adjustments' => true
]); ?>
</span>
<?php else :?>
<?= /* @noEscape */ $block->renderAmount($finalPriceModel->getAmount(), [
'price_id' => $block->getPriceId('product-price-' . $idSuffix),
'price_type' => 'finalPrice',
'include_container' => true,
'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; ?>
我想问是否有人可以就如何正确获取属性提供建议?
解决方案
推荐阅读
- html - 导航栏(国家/地区图像无响应)
- java - Spring @ComponentScan 不适用于 @Repository
- oracle - 如何在pl/sql中的过程中创建角色?
- matlab - 在 Matlab GUI 中加载多个图像
- excel - 如何使用两个单元格值作为行和列坐标来引用另一个单元格?
- c# - 如何在 C# 中以编程方式输入 DataGridView 单元格?
- azure - .Net Core MVC 中用于安全服务请求的两种方式 SSL X509 证书位置 - Azure 服务托管应用程序
- sql-server - 经典 ASP - SQL 错误停止代码,从不显示
- templates - 为什么在 Laravel 5 应用程序中使用 laravel-mail-preview 插件发送的电子邮件没有被接收者接收?
- android - 设置为墙纸意图对话框?