4

I need a page where all products should be displayed. All products from all categories, also including filters by attributes.

I have tried to use the following block

{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" template="catalog/product/list.phtml"}}

However, it doesn't work.

When I create a parent category I get nested categories and they are displayed under the All categories category, this is not what I need.

Please suggest a way to display all products on a single page.

3 Answers 3

1

Add following line in your cms page.

{{block class="Vendor\Module\Block\ListProduct" name="custom.product.list" template="Vendor_Module::product/list.phtml"}}

Create your custom block and phtml file as given below.

Vendor/Module/Block/ListProduct.php

<?php
...
...

private $productCollection;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection
) {
    $this->productCollection = $productCollection;
    parent::__construct($context);
}

public function getProductCollection()
{
    return $this->productCollection->create();
}

Vendor/Module/view/frontend/templates/product/list.phtml

$collection = $block->getProductCollection();

foreach ($collection as $product) {
    print_r($product->getId());
}
0

Create Module OnePageProduct
Step 1: Create a new module named Magepow_OnePageProducts Create the file named registration.php in the path app/code/Magento/OnePageProducts

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Magento_OnePageProducts',
    __DIR__
);

Create the file named module.xml in the path
app/code/Magento/OnePageProducts/etc/module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magento_OnePageProducts" setup_version="2.0.0" />
</config>

Create the file named routes.xml in the path
app/code/Magento/OnePageProducts/etc/frontend/routes.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <!--Define a custom route with id and frontName-->
        <route frontName="magento" id="magento">
            <module name="Magento_OnePageProducts"/>
        </route>
    </router>
</config>

Create the Onepage.php file in the path
app/code/Magento/OnePageProducts/Controller/Product/Onepage.php

<?php

namespace Magento\OnePageProducts\Controller\Product;

class Onepage extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Catalog\Api\CategoryRepositoryInterface
     */
    protected $categoryRepository;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Registry $coreRegistry
     * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->_coreRegistry = $coreRegistry;
        $this->categoryRepository = $categoryRepository;
        $this->_storeManager = $storeManager;
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        $store = $this->_storeManager->getStore();
        $category = $this->categoryRepository->get(
            $store->getRootCategoryId()
        );

        $this->_coreRegistry->register('current_category', $category);

        $page = $this->resultPageFactory->create();
        $page->getLayout()->getBlock('page.main.title')->setPageTitle(__('One Page Products'));
        $page->getLayout()->getBlock('breadcrumbs')->addCrumb(
            'home',
            [
                'label' => __('Home'),
                'title' => __('Go to Home Page'),
                'link' => $store->getBaseUrl()
            ]
        )->addCrumb(
            'product-tag',
            [
                'label' => __('One Page Products'),
                'title' => __('One Page Products')
            ]
        );
        $page->getConfig()->addBodyClass('page-products');
        $page->getConfig()->getTitle()->set(__('One Page Products'));
        $page->getConfig()->setDescription(__('One Page Products'));
        $page->getConfig()->setKeywords(__('One Page Products'));
        $page->getConfig()->addRemotePageAsset($this->_url->getUrl('magento/product/onepage'), 'canonical', ['attributes' => ['rel' => 'canonical']]);

        return $page;
    }
}

Create the layout named magento_product_onepage.xml
app/code/Magento/OnePageProducts/view/frontend/layout/magento_product_onepage.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="catalog_category_view" />
    <update handle="catalog_category_view_type_layered" />
</page>

Run the below commands and check.
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

0

Step:-1 Call block in your cms page where you want show products.

{{block class="{vendorname}\{modulename}\Block\AllProductsOnePage" name="block_name" template="vendorname_modulename::product/list.phtml"}}

Step:-2 Create Block vendorname/modulename/Block/AllProductsOnePage.php file.

<?php
namespace vendorname\modulename\Block;

use Magento\Framework\View\Element\Template;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class AllProductsOnePage extends \Magento\Framework\View\Element\Template
{
    private $productCollection;

    public function __construct(
        Template\Context $context,
        CollectionFactory $productCollection,
        array $data = []
    ) {
        $this->productCollection = $productCollection;
        parent::__construct($context, $data);
    }

    public function getProductsCollection()
    {
        return $this->productCollection->create();
    }
}

Step:-3 Create phtml vendorname/modulename/view/frontend/templates/product/list.phtml file.

<?php
/**
 * @var \vendorname\modulename\Block\ListProduct $block
 */
$productsCollections = $block->getProductsCollection();
foreach ($productsCollections as $productsCollection) {
    echo "<pre>";
    print_r($productsCollection->getData());
}

After the above changes run the setup:upgrade command.
php bin/magento setup:upgrade

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.