首页 > 解决方案 > 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; ?>

我想问是否有人可以就如何正确获取属性提供建议?

标签: magentomagento2

解决方案


推荐阅读