7

I have collection loaded in variable $collection and I want to pass collection variable in my list.phtml

$block = $viewBlock->getLayout()
                ->createBlock('Vendor\Module\Block\ProductList')
                ->setTemplate('Magento_Catalog::product/list.phtml')
                ->toHtml();

So that list.phtml create product list according to my collection. My query is how can I pass collection variable to list.phtml

Note: I don't want to change any thing in list phtml it also include calling any custom function from block object. My product collection must be loaded by default function which is already in list phtml i.e: $_productCollection = $block->getLoadedProductCollection();

7

First you need to create block Vendor\Module\Block\ProductList.php by overriding this function _getProductCollection() by extending your class from this Magento\Catalog\Block\Product\ListProduct.

protected function _getProductCollection()
{   
        /* Do whatever you want to do with your collection */  

        $this->_productCollection = $yourcollection;
        return $this->_productCollection;
}

Now you can call your ptml file. Your collection has been passed to that phtml

$block = $viewBlock->getLayout()
                ->createBlock('Vendor\Module\Block\ProductList')
                ->setTemplate('Magento_Catalog::product/list.phtml')
                ->toHtml();

I hope this will help others

3

try this way....

   $block = $viewBlock->getLayout()
                    ->createBlock('Vendor\Module\Block\ProductList')
                    ->setCustomvalue('111111')
                    ->setTemplate('Magento_Catalog::product/list.phtml')
                    ->toHtml();

and you can get value

$block->getCustomvalue();
  • Thanks for the answer, but I don't want to pass any custom value to list.phtml. I want to pass product collection so that magento default list.phtml will show product list with that collection. – Muhammad Hasham Mar 14 at 8:13
  • you can pass in variable collection also try that – Rakesh Donga Mar 14 at 8:16
  • tried to pass it using ->setCollection($collection) but it wasn't working. What I am trying to say I don't need change any thing in list phtml it include the calling as well. – Muhammad Hasham Mar 14 at 8:18
2

I used below code to send Product data to phtml file using plugin. and it is working fine.

namespace Namespace\Modulename\Plugin\Catalog\Product;
use Namespace\Modulename\Helper\Data;

class ListProduct
{

    /**
     * @var Data
     */
    private $dataHelper;

    /**
     * @var \Magento\Framework\View\LayoutInterface
     */
    protected $layout;   
    public function __construct(        
        \Magento\Framework\View\LayoutInterface $layout,
        Data $dataHelper
    ) {        
        $this->layout = $layout;         
        $this->dataHelper = $dataHelper;
    }

    public function afterGetProductDetailsHtml(
        \Magento\Catalog\Block\Product\ListProduct $subject,
        $result,
        \Magento\Catalog\Model\Product $product
    ) {
        if($this->dataHelper->isEnabled()){
            $result .= $this->layout->createBlock('Namespace\Modulename\Block\Label')->setProduct($product)->setTemplate('Namespace_Modulename::product/test.phtml')->toHtml();
        }
        return $result;        
    }
}

Note : In above code ->setProduct($product) i used to set product data.i think you can use ->setCollection() to achieve your thing. you can try with this way it might be help.

1

Your $block variable contains the content of list.phtml so here you just have called it.

That list.phtml contains $_productCollection that it gets it from the module block, in your $block you can do anything in that collection because the collection has already been loaded, it's too late to update it at this level.

What you can to do is to rewrite that block Magento_Catalog\Block\Product\ListProduct.php and after that, you can update the collection.

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.