2

I have a block class that extends \Magento\Catalog\Block\Product\ListProduct and tried to override the list.phtml with exactly the same file (say mylist.phtml), except it is located under my module. Then I tried to do var_dump in mylist.phtml like below:

$_productCollection = $block->getLoadedProductCollection();
var_dump($_productCollection->getData());exit;

The result was:

array (size=2)
  0 => 
    array (size=9)
      'entity_id' => string '1' (length=1)
      'attribute_set_id' => string '4' (length=1)
      'type_id' => string 'simple' (length=6)
      'sku' => string 'M-1' (length=3)
      'has_options' => string '0' (length=1)
      'required_options' => string '0' (length=1)
      'created_at' => string '2018-11-19 03:14:45' (length=19)
      'updated_at' => string '2018-11-19 03:14:45' (length=19)
      'is_salable' => string '1' (length=1)
  1 => 
    array (size=9)
      'entity_id' => string '2' (length=1)
      'attribute_set_id' => string '4' (length=1)
      'type_id' => string 'simple' (length=6)
      'sku' => string 'M-2' (length=3)
      'has_options' => string '0' (length=1)
      'required_options' => string '0' (length=1)
      'created_at' => string '2018-11-19 04:44:01' (length=19)
      'updated_at' => string '2018-11-19 04:44:01' (length=19)
      'is_salable' => string '1' (length=1)

I only have 2 products on 2 different sub categories.

However, if I disable my module (through config.php), flush cache, delete all folders under generated and do the same var_dump (this time the original /vendor/magento/module-catalog/view/frontend/templates/product/list.phtml), the result was:

array (size=1)
  0 => 
    array (size=17)
      'entity_id' => string '1' (length=1)
      'attribute_set_id' => string '4' (length=1)
      'type_id' => string 'simple' (length=6)
      'sku' => string 'M-1' (length=3)
      'has_options' => string '0' (length=1)
      'required_options' => string '0' (length=1)
      'created_at' => string '2018-11-19 03:14:45' (length=19)
      'updated_at' => string '2018-11-19 03:14:45' (length=19)
      'cat_index_position' => string '0' (length=1)
      'is_salable' => string '1' (length=1)
      'price' => string '100000.0000' (length=11)
      'tax_class_id' => string '2' (length=1)
      'final_price' => string '100000.0000' (length=11)
      'minimal_price' => string '100000.0000' (length=11)
      'min_price' => string '100000.0000' (length=11)
      'max_price' => string '100000.0000' (length=11)
      'tier_price' => null

Vendor/Module/Block/Product/ListProduct.php :

<?php

namespace Vendor\Module\Block\Product;

use Magento\Catalog\Block\Product\ListProduct as ProductListProduct;
use Magento\Catalog\Block\Product\Context;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\Url\Helper\Data as HelperData;
use Magento\Catalog\Model\ResourceModel\Product\Collection;

class ListProduct extends ProductListProduct
{

    protected $_productCollection;

    public function __construct(Context $context, 
        PostHelper $postDataHelper, 
        Resolver $layerResolver, 
        CategoryRepositoryInterface $categoryRepository, 
        HelperData $urlHelper,
        Collection $productCollection,
        array $data = [])
    {
        parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);
        $this->_productCollection = $productCollection;
    }
}

Vendor/Module/etc/frontend/di.xml :

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Block\Product\ListProduct" type="Vendor\Module\Block\Product\ListProduct" />
</config>

Vendor/Module/view/frontend/layout/catalog_category_view.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">
    <head>
        <css src="Vendor_Module::css/module.css"/>
    </head>
    <body>
        <referenceBlock name="category.products.list" template="Vendor_Module::mylist.phtml" />
    </body>
</page>

Your Answer

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

Browse other questions tagged or ask your own question.