Would you tell me the steps to call a function within a Model in a custom module, from a catalog page?
You can call as shared below link:
Just define in
__construct
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
$this->_productCollectionFactory = $productCollectionFactory;
http://www.blogtreat.com/get-product-collection-magento-2/
Or Using OM:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
https://magehit.com/blog/getting-product-collection-in-magento-2/
Hello @tvgarden,
Here is the example code to get the list of all products in Magento 2 using dependency injection.
In this, we might need to inject the object of \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory class in the constructor of our module’s block class and access it from the view ( .phtml ) file.
Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php
<?php namespace YourCompanyName\YourModuleName\Block; class YourCustomBlock extends \Magento\Framework\View\Element\Template { protected $_productCollectionFactory; protected $_productVisibility; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Catalog\Model\Product\Visibility $productVisibility, array $data = [] ) { $this->_productCollectionFactory = $productCollectionFactory; $this->_productVisibility = $productVisibility; parent::__construct($context, $data); } public function getProductCollection() { $collection = $this->_productCollectionFactory->create(); $collection->addAttributeToSelect('*'); // filter current website products $collection->addWebsiteFilter(); // filter current store products $collection->addStoreFilter(); // set visibility filter $collection->setVisibility($this->productVisibility->getVisibleInSiteIds()); // fetching only 5 products $collection->setPageSize(5); return $collection; } }
--
If my answer is useful, please Accept as Solution & give Kudos
hi @gelanivishal @Manish Mittal
Thank you for your replies. They are very useful for my learning path.
I must say sorry for I was not clear about my question.
What I want to call is a function in a custom module.
I want to display a manufacturer logo saved in a custom module table, call the same function from the module.
I would like to call the function from product page and catalog page.
Hello @tvgarden
For your issue first of all you have to override the catalog phtml files where you want to get data by your custom function.
After that you can call your custom function in that files same as below attached link:
https://magento.stackexchange.com/questions/121364/magento-2-how-to-call-any-block-function-in-phtml
Hope it will be helpful for you.
Problem solved? Click Kudos & Accept as Solution!
Thank you for your reply.
Sorry I was not clear enough.
I would like to do something like this:
But the problem is my custom module do not have a helper.
Still facing issue or fixed already? As I shared in the previous post how you can call the function. Need to define an object in _construct and you can call that object in any function.
If you want to show manufacturer, please take help with below link:
Thanks for the reply. It works.
I am using a third-party's module so our clients can upload manufacturer logos.
I thought it would be nice to display these logos on the category/ product pages, but it looks a little complicated than I thought.