cancel
Showing results for 
Search instead for 
Did you mean: 

reuse list.phtml in custom module

Highlighted

reuse list.phtml in custom module

Hello guys,

 

I have a custom module that gets a product collection of "favorite products" for the home page:

class HomePageOurFavorites extends AbstractProduct implements BlockInterface
{
    protected $_template = 'widget/index.phtml';
    protected $productFactory;

    public function __construct(Context $context, array $data = [], ProductFactory $productFactory)
    {
        parent::__construct($context, $data);
        $this->productFactory = $productFactory;
    }

    /**
     * Get favorites products
     */
    public function getProducts()
    {
        $objectManager     = ObjectManager::getInstance();
        $factoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
        $collection        = $factoryCollection->create();
        $collection->addAttributeToFilter('is_favorite', 1);
        $collection->setPageSize(6);
        return $collection;
    }

// ....
}

Currently, the template used is the template's `widget/index.phtml`.

 

I'd like to show the products exactly as they are shown on the product page, so I'd like to use the `list.phtml` from `Magento_Catalog`. Any ideas how to do that?

3 REPLIES 3
Highlighted

Re: reuse list.phtml in custom module

Hi @marten_seemann 

 

You need to override list.phtml with your custom theme first.
Then you need to create object of your block withing html and then you can use your collection function.

Thank you.

Highlighted

Re: reuse list.phtml in custom module


You need to override list.phtml with your custom theme first.


I already did this. I just want to reuse whatever I customized there.

 


Then you need to create object of your block withing html and then you can use your collection function.

How do I create an object of that block in the html, and how does this object know to use the Magento_Catalog list.phtml?

Highlighted

Re: reuse list.phtml in custom module

Hi @marten_seemann 

You can create object of your model file by using below example:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$object = $_objectManager->create('Vendor\Model\Model\HomePageOurFavorites');
$object->
getProducts();

 It may help you!
Problem Solved? Please click on 'Kudos' & Accept as Solution!