0

i need to show an attribute to product page under the name of the product. The attribute is text and the code is plustile

I try to load it to a shopbybrand extension that has a block in the wanted area with the code

<?php
// check attribute exists (Where 'dimension' is the attribute code for your attribute)
if ($attribute = $this->getProduct()->getResource()->getAttribute('flooring_width')): ?>
    <?php
    // get label for the current store (in case you have multiple stores and languages)
    $_attribute_label = $attribute->getStoreLabel(Mage::app()->getStore()->getName());
    // get the attribute value (for other attributes change getDimension to getName where name is the name of your attribute );
    $_attribute_value = $this->getProduct()->getFlooringWidth();
    ?>
    <?php
    // check there is a value for the attribute
    if (isset($_attribute_value)): ?>
        <p>
            <?php echo $_attribute_label . ': ' . $_attribute_value; ?>
        </p>
    <?php endif; ?>
<?php endif; ?>

or

<?php
$_product = $block->getProduct();
$attrData = $_product->getData('plustitle');
$blockTitle = $this->getData('title');
if (trim($attrData) !== "") : ?>
    <div class="product-full-width-section">
        <h2 class="product-section-title"><?= $blockTitle; ?></h2>
        <?php echo $attrData; ?>
        <?php echo 'mplampla';?>
    </div>
<?php endif; ?>

i get Error: Call to a member function getResource() on null in

i try to make an extension like https://www.dckap.com/blog/display-custom-attribute-value-product-viewlist-page-magento-2/

but with no success

Can you advise for the correct way to do it or Do you know any extension to do this?

Thank you

0

first added this code on your phtml and then use the product object :

<?php
$_helper = $this->helper(Magento\Catalog\Helper\Output::class);
$_product = $block->getProduct();

if (!$_product instanceof \Magento\Catalog\Model\Product) {
    return;
}
2
  • It don't run anything after ``` if (!$_product instanceof \Magento\Catalog\Model\Product) { return; } ```
    – Pantelis
    Jan 15 '21 at 10:07
  • my full code i try to run is ``` <?php $_helper = $this->helper(Magento\Catalog\Helper\Output::class); $_product = $block->getProduct(); if (!$_product instanceof \Magento\Catalog\Model\Product) { return; } $attrData = $_product->getData('plustitle'); $blockTitle = $this->getData('title'); if (trim($attrData) !== "") : ?> <div class="product-full-width-section"> <h2 class="product-section-title"><?= $blockTitle; ?></h2> <?php echo $attrData; ?> <?php echo 'mplampla';?> </div> <?php endif; ?> ```
    – Pantelis
    Jan 15 '21 at 10:09
0

Try to get product data from your block i.e. $block->getProduct

0

I solve it with replacing the

$_product = $block->getProduct();

with

$_product = \Magento\Framework\App\ObjectManager::getInstance()
    ->get(\Magento\Framework\Registry::class)->registry('product');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.