0

New to Magento 2 and hit a bit of a hurdle I can't seem to find the answer to.

I'm trying to add the product description into a custom module that's called inside of the product page.

I've tried copying the line inside of description.phtml

<?= /* @escapeNotVerified */ $this->helper('Magento\Catalog\Helper\Output')->productAttribute($block->getProduct(), $block->getProduct()->getDescription(), 'description') ?>

however I get a white page error with no error message when I try this.

I feel like I'm missing something pretty obvious.

Any help appreciated.

3
0

In Store > Attributes > Product > Description > Storefront Properties set Visible on Catalog Pages on Storefront and Used in Product Listing to TRUE.

Then you can use in your phtml file:

<?php echo $this->helper('Magento\Catalog\Helper\Output')->productAttribute($_product, $_product->getDescription(), 'description') ?>

or

<?php echo $_product->getDescription() ?>

Let me know if this works for you.

Thank you.

6
  • Exception #0 (Exception): Notice: Undefined variable: _product
    – Jamie
    Oct 31 '19 at 14:42
  • $_product is product object, which should be part of: \Magento\Catalog\Model\ProductFactory Oct 31 '19 at 15:23
  • Okay this is the part I'm missing, My block just extends \Magento\Framework\View\Element\Template
    – Jamie
    Oct 31 '19 at 16:22
  • is there documentation on how to make a module for Products descriptions etc?
    – Jamie
    Oct 31 '19 at 16:22
  • can you explain what you are doing exactly. so I can tell u precisely what to do. Oct 31 '19 at 17:19
0

In your custom module create a new Helper class that can return product data based on the product SKU something like

namespace CustomModule\Page\Helper;
use Magento\Framework\App\Helper\AbstractHelper;

class Product extends AbstractHelper
{
     protected $_productFactory;
     protected $_productRepository;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Catalog\Model\ProductRepository $productRepository
    ) {
        parent::__construct($context);
        $this->_productFactory = $productFactory;
        $this->_productRepository = $productRepository;
    }

    public function getProductDataBySku($sku)
    {
        $product=$this->_productRepository->get($sku);
        return $product;
    }
}

You can access the product data in your template using

$_productHelper = $this->helper('CustomModule\Page\Helper\Product');
$_product=$_productHelper->getProductDataBySku('24-UG06');
$_description=$_product->getDescription();

Where 24-UG06 is the SKU.

2
  • I created this in a /Helper/ProductHelper.php once I did this I used <?php use \CompanyName\ModuleName\Helper\ProductHelper; class Product { public function __construct(Product $helper) { $this->helper = $helper; } public function newFunction() { $this->helper->getStoreConfig(); } } ?> I then tried to call the description however no luck!
    – Jamie
    Oct 31 '19 at 14:49
  • I updated the answer for a helper class, the previous answer was for a block class. You did however say you wanted to access the data from a module not a template which is a bit confusing.
    – paj
    Oct 31 '19 at 17:06

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.