質問

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();

役に立ちましたか?

解決

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

他のヒント

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();

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.

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.

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top